-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
executable file
·82 lines (65 loc) · 2.27 KB
/
index.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
#!/usr/bin/env node
var http = require('http');
var fs = require('fs');
var path = require('path');
var express = require('express');
var logginator = require("logginator");
var optimist = require("optimist");
var core = require('./core');
var subsystems = require('./subsystems');
var argv = optimist.default({
config: "config.json"
}).argv;
try {
var config = JSON.parse(fs.readFileSync(argv.config, "utf8"));
}
catch (err) {
console.error("Error reading config file " + argv.config);
if (err instanceof SyntaxError) {
console.error("" + err);
} else if (err.errno === 34) {
console.error("File not found. You can specify the config file name with --config <filename>");
}
process.exit(1);
}
var package = JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json')));
var log = logginator(config.log);
log.info("Version " + package.version + " (on node " + process.version + ")");
var app = core(log.createSublogger("core"), config.actions);
function terminateSignalHandler(signal) {
log.warn("Got signal " + signal + ". Terminating.");
app.subprocessManager.killAllRunning(function () {
process.exit(0);
});
}
process.on('SIGINT', function () {
terminateSignalHandler('SIGINT');
});
process.on('SIGTERM', function () {
terminateSignalHandler('SIGTERM');
});
process.on('uncaughtException', function(err) {
log.error("Uncaught exception: " + err);
if (err.stack) log.error(err.stack);
log.warn("Attempting to kill running subprocesses after uncaught exception");
app.subprocessManager.killAllRunning(function () {});
log.error("Terminating because of uncaught exception");
process.exit(1);
});
var expressApp = express();
var httpServer = expressApp.server = http.createServer(expressApp);
expressApp.config = config;
require('winston-tagged-http-logger')(httpServer, log.createSublogger("http"));
for (var subsystemName in subsystems) {
if (!subsystems.hasOwnProperty(subsystemName)) continue;
subsystems[subsystemName](
log.createSublogger(subsystemName),
app,
expressApp,
config[subsystemName]
);
}
var httpConf = config.http || {};
var port = argv.port || httpConf.port || 0;
var bind = argv.bind || httpConf.bind || undefined;
httpServer.listen(port, bind);