-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.js
150 lines (138 loc) · 5.13 KB
/
server.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*!
* Copyright (c) 2014 Milo van der Linden ([email protected])
*
* This file is part of opendispatcher/safetymapsDBK
*
* opendispatcher is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* opendispatcher is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with opendispatcher. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* global global, __dirname, exports, process */
// To run in production: NODE_ENV=production
var express = require('express'),
errorhandler = require('errorhandler'),
//favicon = require('serve-favicon'),
bodyParser = require('body-parser'),
http = require('http'),
path = require('path'),
i18n = require('i18next'),
anyDB = require('any-db'),
fs = require('fs'),
compress = require('compression');
var env = process.env.NODE_ENV || 'development';
global.conf = require('nconf');
// First consider commandline arguments and environment variables, respectively.
global.conf.argv().env();
// Then load configuration from a designated file.
//check to see if config file exists, if not, default to config.default.json
if (!fs.existsSync('config.json')) {
console.log('Warning, no config.json present. Falling back to config.default.json');
//check to see if config file exists, if not, default to config.default.json
global.conf.file({file: 'config.default.json'});
} else {
global.conf.file({file: 'config.json'});
}
// Provide default values for settings not provided above.
global.conf.defaults({
'http': {
'port': 9999
}
});
var dbURL = 'postgres://' +
global.conf.get('database:user') + ':' +
global.conf.get('database:password') + '@' +
global.conf.get('database:host') + ':' +
global.conf.get('database:port') + '/' +
global.conf.get('database:dbname');
var bagURL = 'postgres://' +
global.conf.get('bag:user') + ':' +
global.conf.get('bag:password') + '@' +
global.conf.get('bag:host') + ':' +
global.conf.get('bag:port') + '/' +
global.conf.get('bag:dbname');
if (global.conf.get('infrastructure:user')) {
var infraURL = 'postgres://' +
global.conf.get('infrastructure:user') + ':' +
global.conf.get('infrastructure:password') + '@' +
global.conf.get('infrastructure:host') + ':' +
global.conf.get('infrastructure:port') + '/' +
global.conf.get('infrastructure:dbname');
global.infra = anyDB.createPool(infraURL, {min: 2, max: 20});
}
global.pool = anyDB.createPool(dbURL, {min: 2, max: 20});
global.bag = anyDB.createPool(bagURL, {min: 2, max: 20});
global.defaultLanguage = global.conf.get('default:language') || 'en';
i18n.init({
lngWhitelist: ['nl', 'en', 'dev'],
detectLngQS: 'l',
saveMissing: true,
useCookie: false,
debug: false,
fallbackLng: global.defaultLanguage
});
var app = express(
// {
// requestCert: true,
// // If specified as "true", no unauthenticated traffic
// // will make it to the route specified.
// rejectUnauthorized: true
// }
);
if ('development' == env) {
app.use(errorhandler({dumpExceptions: true, showStack: true}));
app.use('/build', express.static(__dirname + '/build'));
app.locals.pretty = true;
}
if ('production' == env) {
app.use(errorhandler());
app.locals.pretty = false;
}
// Configuration
app.set('port', process.env.PORT || global.conf.get('http:port'));
app.use(compress());
app.enable('trust proxy');
app.use(i18n.handle);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use('/locales', express.static(__dirname + '/locales'));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/media', express.static(global.conf.get('media:path')));
app.use('/symbols', express.static(global.conf.get('media:symbols')));
app.use('/web', express.static(__dirname + '/web'));
app.use(function (err, req, res, next) {
var errobj = {};
errobj.url = req.protocol + "://" + req.get('host') + req.url;
errobj.body = req.body;
errobj.query = req.query;
errobj.params = req.params;
res.status(500);
res.render('error', {title: 'error', request: JSON.stringify(errobj), error: err});
});
i18n.registerAppHelper(app);
i18n.serveClientScript(app)
.serveDynamicResources(app)
.serveMissingKeyRoute(app);
app.use(require('./controllers'));
function start() {
app.listen(app.get('port'), function () {
console.log("Express server listening on port %d in %s mode", app.get('port'), app.settings.env);
});
}
exports.start = start;
exports.app = app;
exports.pool = global.pool;
exports.bag = global.bag;