forked from madbook/reddit-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (54 loc) · 1.84 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
// Register that we're using es6, so babel can compile import statements.
// The `ignore` set to false allows babel to compile npm modules, and the `only`
// forces it to only compile files with a `.es6.js` or `.jsx` extension.
require('babel/register')({
ignore: false,
only: /.+(?:(?:\.es6\.js)|(?:.jsx))$/,
extensions: ['.js', '.es6.js', '.jsx' ],
sourceMap: true,
});
// Require in the express server.
var Server = require('./src/server');
var cluster = require('cluster');
var numCPUs = process.env.PROCESSES || require('os').cpus().length;
// App config
var config = require('./src/config');
// Import built-asset manifests for passing to layouts
var jsManifest = require('./build/js/client-manifest.json');
var cssManifest = require('./build/css/css-manifest.json');
var servers = [];
var failedProcesses = 0;
// Then merge them into a single object for ease of use later
config.manifest = {};
config.processes = numCPUs;
Object.assign(config.manifest, jsManifest, cssManifest);
function start(config) {
var server = new Server(config);
server.start();
return server;
}
// Private, server-only config that we don't put in config.js, which is shared
config.liveReload = process.env.LIVERELOAD || true;
config.oauth = {
clientId: process.env.OAUTH_CLIENT_ID || '',
secret: process.env.OAUTH_SECRET || '',
};
config.keys = [ process.env.SERVER_SIGNED_COOKIE_KEY || 'lambeosaurus' ];
if (cluster.isMaster) {
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
Server.info(config);
cluster.on('exit', function(worker, code, signal) {
if (failedProcesses < 20) {
console.log('Worker ' + worker.process.pid + ' died, restarting.');
cluster.fork();
failedProcesses++;
} else {
console.log('Workers died too many times, exiting.');
process.exit();
}
});
} else {
servers.push(start(config));
}