forked from mariano-fiorentino/amid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
62 lines (51 loc) · 1.27 KB
/
server.js
File metadata and controls
62 lines (51 loc) · 1.27 KB
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
/*
server.js
amid
Created by Tom de Grunt on 2010-10-03 in mongodb-rest
New version Copyright (c) 2013 Mariano Fiorentino, Andrea Negro
This file is part of amid.
*/
var fs = require("fs");
var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;
var config = { "db": {
'port': 27017,
'host': "localhost"
},
'server': {
'port': 3000,
'timeout': 120,
'address': "0.0.0.0"
},
'flavor': "regular",
'debug': true
};
try {
config = JSON.parse(fs.readFileSync(process.cwd()+"/config.json"));
} catch(e) {
// ignore
}
module.exports.config = config;
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('death', function(worker) {
console.log('worker ' + worker.pid + ' died');
});
} else {
// Worker processes have a http server.
var express = require('express');
var app = module.exports.app = express();
app.use(express.bodyParser());
// Bind to a port
require('./lib/main');
require('./lib/command');
require('./lib/rest');
app.listen(config.server.port, config.server.address);
app.on('connection', function(socket) {
socket.setTimeout(config.server.timeout * 1000);
});
}