-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
59 lines (49 loc) · 1.38 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
/*jshint esversion: 6 */
'use strict'
const cluster = require('cluster');
const express = require('express');
const app = express();
const logger = require('./utls/logger.js');
const morgan = require('morgan');
const bodyParser = require('body-parser');
//get number of CPU's
const numCPUs = require('os').cpus().length;
// determine listening port
const listenPort = process.env.CLOUDSU_PORT || 3000;
// express configuration
// setup frontend static assets
app.use(express.static(`${__dirname}/dist`));
// json body parser
app.use(bodyParser.json({
type: 'application/*'
}));
// url encoding parser
app.use(bodyParser.urlencoded({
extended: true
}));
// setup winston logger
app.use(morgan('combined', {
'stream': logger.stream
}));
//setup api router
require('./api/router.js')(app)
if (cluster.isMaster) {
//init functions that only run on master
//queue rida (looks for new server messages in sqs)
require('./utls/queue_rider.js');
//start cleanup tool (cleans up expired resources)
require('./utls/cleanup_tool.js');
// fork cluster to get the most of a mutli-core server
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
} else {
// listen
app.listen(listenPort, (err, response) => {
if (err) {
logger.info(err);
return
};
logger.info(`App listening on port ${listenPort}`);
});
}