This repository has been archived by the owner on Jul 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
95 lines (80 loc) · 2.68 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
/**
* GAIA result upload service. It receives files containing measures that need to be processed and pushed to metrics API. The files
* must be in format supported by GAIA result processors. The file is information discovered from external system - through REST API
* or as an event.
*/
'use strict';
var log4js = require('log4js');
// replaces console.log function with log4j
log4js.replaceConsole();
if (process.env.LOG_LEVEL) {
log4js.setGlobalLogLevel(process.env.LOG_LEVEL);
}
var logger = log4js.getLogger('server.js');
var grace = require('grace');
var express = require('express'), app = express();
var notification = require('./controllers/notification');
var errorReporter = require('./util/error-reporter');
var errorUtils = require('./util/error-utils.js');
var getFullError = errorUtils.getFullError;
var when = require('when');
var auth = require('./middlewares/auth');
var PORT = 8080;
var graceApp = grace.create();
exitOnSignal('SIGINT');
exitOnSignal('SIGTERM');
function exitOnSignal(signal) {
process.on(signal, function() {
logger.debug('Caught ' + signal + ', exiting');
graceApp.shutdown(0);
});
}
app.use(auth.authorise);
app.use(require('./controllers'));
app.use(auth.errorHandler);
app.use(defaultErrorHandler);
/**
* Default error handler for REST. Receives errors not caught by route handler.
*
* @param err instance of error
* @param req express request
* @param res express response
*/
function defaultErrorHandler(err, req, res, next) {
if (!(err instanceof Error)) {return next(err);}
logger.error('Unhandled exception in REST call \'' + req.path + '\'');
logger.error(getFullError(err));
errorReporter.reportError(res, err);
}
/**
* Initializes the server. Initializes any dependencies first before starting to listen on server socket.
*/
function initServer() {
// add any async initializations here
when.all([notification.initAmq(true)]).catch(function(err) {
logger.error(getFullError(err));
}).finally(function() {
// we listen also if connection to MQ is not available (disconnect can happen at any time after connect too
// and this would lead to the same situation)
app.listen(PORT, function() {
logger.info('Running on http://localhost:' + PORT);
});
});
}
graceApp.on('start', function () {
initServer();
});
graceApp.on('error', function(err){
logger.error(getFullError(err));
});
graceApp.on('shutdown', function(cb) {
notification.shutdown().done(function onOk() {
cb();
}, function onFailed(err) {
cb(err);
});
});
graceApp.on('exit', function(code){
logger.debug('Exiting with code ' + code);
});
graceApp.start();