-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
68 lines (61 loc) · 1.95 KB
/
server.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
/* Title: Server app.js
*/
var cluster = require('cluster');
var farmhash = require('farmhash');
var num_processors = require('os').cpus().length;
var net = require('net');
var path = require('path');
const bodyParser = require('body-parser');
var cors = require('cors');
var forceSSL = require('force-ssl-heroku');
var jwt = require('jsonwebtoken');
require('dotenv').config();
var port = process.env.PORT || 8080;
var middleware = async function(app){
app.use(forceSSL);
app.use(cors());
app.use(express.static(__dirname + '/dist'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.get('*', function(req,res){
res.sendFile(path.resolve(__dirname, 'dist/index.html'));
});
}
if(cluster.isMaster){
console.log("Master",process.pid,"is running");
var workers = [];
var spawn = function(i){
workers[i] = cluster.fork();
console.log("Worker",workers[i].id,"listening on port",port,"with process",workers[i].process.pid);
workers[i].on('exit', (code,singal) =>{
console.log('respawning worker',i);
spawn(i);
});
};
var num_processors = process.env.WEB_CONCURRENCY || 1;
for(var i = 0; i < num_processors; i++){
spawn(i);
}
var workers_index = (ip,len) =>{
return farmhash.fingerprint32(ip) % len;
}
var server = net.createServer({pauseOnConnect:true}, (connection)=>{
var worker = workers[workers_index(connection.remoteAddress, num_processors)];
worker.send('connection',connection);
}).listen(port);
}
else{
var express = require('express');
var app = express();
var http = require('http').Server(app);
middleware(app);
app.listen(0);
process.on('message', (message,connection)=>{
if(message !== 'connection'){
return;
}
http.emit('connection',connection);
module.exports = app;
connection.resume();
});
}