-
Notifications
You must be signed in to change notification settings - Fork 34
/
application.js
63 lines (49 loc) · 1.72 KB
/
application.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
const express = require('express');
const cluster = require('cluster');
const os = require('os');
const app = express();
const ValidateBody = require('./middleware/validateBody');
const ErrorHandler = require('./middleware/errorHandler');
const NotFound = require('./middleware/notFound');
const Auctions = require('./routes/auctions');
const Bazaar = require('./routes/bazaar');
const Networth = require('./routes/networth');
const Leaderboard = require('./routes/leaderboard');
const ForgeProfits = require('./routes/forgeProfits');
const ApiStats = require('./routes/apiStats');
require('./jobs/updateAuctions');
require('./jobs/updateBazaar');
const createCluster = function () {
const clusters = process.env.CLUSTERS || os.cpus().length;
console.log(`Booting Maro's API with ${clusters} instances`);
for (let i = 0; i < clusters; i++) {
cluster.fork();
}
cluster.on('exit', worker => {
console.log(`Worker ${worker.id} had a heart attack, starting a new one`);
cluster.fork();
});
};
const startWebService = async function () {
app.use(express.static('public'));
app.use(express.json({ limit: '15mb' }));
app.use(express.urlencoded({ extended: true }));
app.use(ValidateBody);
app.use(ErrorHandler);
app.use('/api/auctions', Auctions);
app.use('/api/bazaar', Bazaar);
app.use('/api/networth', Networth);
app.use('/api/leaderboard', Leaderboard);
app.use('/api/forge', ForgeProfits);
app.use('/api/stats', ApiStats);
app.use(NotFound);
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Worker ${cluster.worker.id} with process id ${process.pid} is now listening on port ${port}`);
});
};
if (cluster.isMaster) {
createCluster();
} else {
startWebService();
}