-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
123 lines (86 loc) · 2.68 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* The MIT License (MIT)
* Copyright (c) 2016 GochoMugo <mugo@forfuture.co.ke>
* Copyright (c) 2016 Forfuture, LLC <we@forfuture.co.ke>
*
* Mobile Money Transaction Costs (MMTC).
*/
exports = module.exports = {
run,
};
// built-in modules
const path = require('path');
// npm-installed modules
const bodyParser = require('body-parser');
const config = require('config');
const Debug = require('debug');
const express = require('express');
const nunjucks = require('nunjucks');
// own modules
const engine = require('./engine');
const routes = require('./routes');
const webConfig = require('./web/config');
const pkg = require('./package.json');
// module variables
const app = express();
const debug = Debug('mmtc-ke:app');
const logger = engine.clients.getLogger();
let nunjucksEnv;
const devmode = app.get('env') === 'development';
debug('initializing engine');
engine.init();
debug('configuring nunjucks');
nunjucksEnv = nunjucks.configure('web', {
autoescape: true,
express: app,
noCache: devmode ? true : false,
});
debug('adding global variables for nunjucks templates');
nunjucksEnv.addGlobal('pkg', pkg);
nunjucksEnv.addGlobal('site', config.get('site'));
nunjucksEnv.addGlobal('env', process.env);
debug('setting up views');
app.set('views', path.join(__dirname, 'web'));
debug('setting up default engine extension');
app.set('view engine', 'html');
debug('disabling Express view cache');
app.set('view cache', false);
debug('mounting middleware for parsing request body');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
debug('mounting middleware for serving static files');
routes.utils.webMiddleware(app, webConfig);
debug('adding routes');
app.use(routes);
debug('mounting catch-all handler');
app.use(function(req, res) {
return routes.utils.renderPage(req, res, 'error', {
error: new engine.errors.PageNotFoundError(`page '${req.path}' not found`),
});
});
debug('mounting middleware for error handling');
app.use(function(err, req, res, next) { // eslint-disable-line no-unused-vars
logger.error(err);
return routes.utils.renderPage(req, res, 'error', {
error: err,
});
});
function run(options, done) {
options = options || {};
if (!options.host) {
options.host = config.get('server.ip');
}
if (!options.port) {
options.port = config.get('server.port');
}
debug('starting server');
app.listen(options.port, options.host, function() {
logger.info('server listening on http://%s:%s', options.host, options.port);
debug('server listening on http://%s:%s', options.host, options.port);
if (done) return done();
});
}
if (require.main === module) {
debug('running as script');
run();
}