-
Notifications
You must be signed in to change notification settings - Fork 36
/
app.js
63 lines (52 loc) · 1.75 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
'use strict';
var cluster = require('cluster');
var config = require('./config');
var log = require('./services/logger');
var basicAuth = require('basic-auth-connect');
var express = require('express');
var app;
var server;
if (cluster.isMaster && config.env === 'production') {
var kue = require('./services/queue').kue;
app = express();
app.use(basicAuth(config.queueUIUsername, config.queueUIPassword));
app.use(kue.app);
server = app.listen(config.queueUIPort, function () {
var host = server.address().address;
var port = server.address().port;
log.info('Queue UI listening on host '+host+', port '+port+'!');
});
// Count the machine's CPUs
var cpuCount = require('os').cpus().length;
// Create a worker for each CPU
for (var i = 0; i < cpuCount; i += 1) {
cluster.fork();
}
// Listen for dying workers
cluster.on('exit', function (worker) {
// Replace the dead worker,
// we're not sentimental
log.info('Worker %d died', worker.id);
cluster.fork();
});
} else {
app = express();
var router = require('./routes');
var express_enforces_ssl = require('express-enforces-ssl');
if(config.trustProxy === 'yes'){
app.enable('trust proxy');
}
if(config.enforceSSL === 'yes'){
app.use(express_enforces_ssl());
}
app.use('/',router);
if(config.env === 'production'){
log.info('Worker %d running!', cluster.worker.id);
}
server = app.listen(config.port, function () {
var host = server.address().address;
var port = server.address().port;
log.info('API server listening on host '+host+', port '+port+'!');
});
}
// ToDo: Write a complete Documentation for this project