-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
78 lines (60 loc) · 1.96 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
77
78
'use strict';
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var path = require('path');
var config = require('./config');
var logger = require('./lib/logger');
var churchill = require('churchill');
var hof = require('hof');
var template = hof.template;
var i18n = hof.i18n;
var mixins = hof.mixins;
var fields = require('./fields');
process.title = 'levweb';
if (config.env !== 'acceptance') {
if (config.env !== 'development') {
churchill.options.logGetParams = false;
}
app.use(churchill(logger));
}
app.use('/public', express.static(path.resolve(__dirname, './public')));
app.use(function setAssetPath(req, res, next) {
res.locals.assetPath = '/public';
next();
});
app.use(function setBaseUrl(req, res, next) {
res.locals.baseUrl = req.baseUrl;
res.locals.absBaseUrl = req.baseUrl || '/';
next();
});
app.set('view engine', 'html');
app.set('views', path.resolve(__dirname, './views'));
template.setup(app);
app.use(require('express-partial-templates')(app));
app.engine('html', require('hogan-express-strict'));
app.use(i18n.middleware());
app.use(mixins(fields));
app.use((req, res, next) => {
res.set('Cache-Control', 'no-cache; no-store');
res.set('X-Frame-Options', 'DENY');
next();
});
// add health check endpoints
app.use(require('./lib/health'));
require('./routes')(app);
app.use(require('./middleware/not-found')());
app.use(require('./middleware/error')());
server.listen(config.port, config.listen_host);
logger.info('App listening on port', config.port);
// gracefully handle shutdowns -----------------------
const closeGracefully = (signal) => {
setTimeout(() => {
logger.warn('Forcefully shutting down from sig:', signal);
process.exit(0); // eslint-disable-line no-process-exit
}, 500);
server.close(() => process.exit(0)); // eslint-disable-line no-process-exit
};
['SIGINT', 'SIGTERM', 'SIGQUIT'].forEach(signal =>
process.on(signal, () => closeGracefully(signal))
);