-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
59 lines (46 loc) · 1.62 KB
/
app.js
File metadata and controls
59 lines (46 loc) · 1.62 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
const bodyParser = require('body-parser');
const cluster = require('cluster');
const dotenv = require('dotenv');
const express = require('express');
const http = require('http');
const path = require('path');
dotenv.config({ path: path.join(__dirname, '.env') });
const numCPUs = process.env.WEB_CONCURRENCY || require('os').cpus().length;
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
for (let i = 0; i < numCPUs; i++)
cluster.fork();
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
cluster.fork();
});
} else {
const app = express();
const server = http.createServer(app);
const MAX_SERVER_UPLOAD_LIMIT = 52428800;
const MAX_SERVER_PARAMETER_LIMIT = 50000;
const PORT = process.env.PORT || 10101;
const Job = require('./cron/Job');
const gossipGetController = require('./gossip/get');
app.use(bodyParser.json({ limit: MAX_SERVER_UPLOAD_LIMIT }));
app.use(bodyParser.urlencoded({
extended: true,
limit: MAX_SERVER_UPLOAD_LIMIT,
parameter: MAX_SERVER_PARAMETER_LIMIT
}));
app.use((req, res, next) => {
if (!req.query || typeof req.query != 'object')
req.query = {};
if (!req.body || typeof req.body != 'object')
req.body = {};
next();
});
app.get('/gossip', gossipGetController);
server.listen(PORT, () => {
if (cluster.worker.id == 1)
Job.start(() => {
console.log(`Cron Jobs are started on Worker ${cluster.worker.id}`);
});
console.log(`Server is on port ${PORT} as Worker ${cluster.worker.id} running @ process ${cluster.worker.process.pid}`);
});
}