-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
76 lines (60 loc) · 2.26 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const healthcheck = require('@hmcts/nodejs-healthcheck');
const express = require('express');
const logger = require('morgan');
const bodyParser = require('body-parser');
const config = require('config');
const debug = require('debug')('ccd-case-activity-api:app');
const enableAppInsights = require('./app/app-insights/app-insights');
enableAppInsights();
const storeCleanupJob = require('./app/job/store-cleanup-job');
const authCheckerUserOnlyFilter = require('./app/user/auth-checker-user-only-filter');
const corsHandler = require('./app/security/cors');
const redis = require('./app/redis/redis-client');
const ttlScoreGenerator = require('./app/service/ttl-score-generator');
const activityService = require('./app/service/activity-service')(config, redis, ttlScoreGenerator);
const activity = require('./app/routes/activity-route')(activityService, config);
const app = express();
const appHealth = express();
const poweredByHeader = 'x-powered-by';
app.disable(poweredByHeader);
appHealth.disable(poweredByHeader);
const healthConfig = {
checks: {},
};
healthcheck.addTo(appHealth, healthConfig);
app.use(appHealth);
if (config.util.getEnv('NODE_ENV') !== 'test') {
app.use(logger('dev'));
const Crontab = config.get('app.storeCleanupCrontab');
storeCleanupJob.start(Crontab);
}
if (config.util.getEnv('NODE_ENV') === 'test') {
app.forceStoreCleanup = storeCleanupJob.force;
}
debug(`starting application with environment: ${config.util.getEnv('NODE_ENV')}`);
app.use(corsHandler);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.text());
app.use(authCheckerUserOnlyFilter);
app.use('/', activity);
// catch 404 and forward to error handler
app.use((req, res, next) => {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
// FIXME `next` MUST be kept for error handling, even though it causes linting to fail
/* eslint-disable no-unused-vars */
app.use((err, req, res, next) => {
debug(`Error processing request: ${err}`);
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
res.status(err.status || 500);
res.json({
message: err.message,
});
});
module.exports = app;