-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
104 lines (88 loc) · 2.98 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
"use strict";
var conf = require("node-conf"),
http = require("http"),
https = require("https"),
fs = require("fs"),
path = require("path"),
express = require("express"),
cluster = require("cluster"),
Logging = require("./lib/logging"),
Filters = require("./lib/filters"),
Middlewares = require("./lib/middlewares"),
Datastore = require("./lib/datastore"),
Boomerang = require("./lib/boomerang");
var numCpus = require("os").cpus().length;
var config = conf.load(process.env.NODE_ENV);
var app;
if ( typeof config.server === "undefined" ) {
var error = new Error("Error could not parse configuration!");
console.error(error);
process.exit(1);
}
var logging = new Logging(config.log);
var filters = new Filters(config.filter, logging.filterLogger);
function handleError(exception) {
logging.datastoreLogger.fatal( { error: exception }, exception);
process.exit();
}
function main(dsInstance) {
var boomerang = new Boomerang(config, dsInstance, filters, logging.appLogger);
app = express();
app.use(boomerang.router());
app.use(logging.webLogger);
app.use(new Middlewares(config));
config.server.listeners.forEach(startListener);
}
function postStartup() {
if (typeof config.security !== "undefined") {
logging.appLogger.info({context: config.security}, "Dropping to security context " + (config.security.user || "boomerang") + ":" + (config.security.group || "boomerang"));
process.setgid(config.security.group || "boomerang");
process.setuid(config.security.user || "boomerang");
} else {
logging.appLogger.info({context: { user: "boomerang", group: "boomerang" }}, "Dropping to security context boomerang:boomerang");
process.setgid("boomerang");
process.setuid("boomerang");
}
}
function startListener(listener) {
if (listener.protocol === "http" ) {
logging.appLogger.info({ listener: listener }, "Starting HTTP Application Server");
httpListener(listener);
} else if (listener.protocol === "https" ) {
logging.appLogger.info({ listener: listener }, "Starting HTTPS Application Server");
httpsListener(listener);
}
}
function httpListener(listener) {
var server = http.createServer(app);
server.listen(listener.port, listener.listen, postStartup);
}
function httpsListener(listener) {
var server = https.createServer({
key: fs.readFileSync(path.resolve(listener.key)),
cert: fs.readFileSync(path.resolve(listener.cert))
}, app);
server.listen(listener.port, listener.listen, postStartup);
}
if (cluster.isMaster) {
var forks = 0;
if (config.server.threads) {
if (config.server.threads > numCpus) {
forks = numCpus;
} else {
forks = config.server.threads;
}
} else {
forks = 1;
}
for (var i = 0; i < forks; i++) {
cluster.fork();
}
} else {
var ds = new Datastore(config.datastore, logging.datastoreLogger);
if (ds.init(config.datastore.active)) {
main(ds);
} else {
handleError(new Error("Could not initialize the Datastore"));
}
}