-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.js
28 lines (23 loc) · 880 Bytes
/
main.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
import cluster from 'node:cluster';
import http from 'node:http';
import os from 'node:os';
import app from './src/app.js';
import makeLogger from './src/lib/logger.js';
import Settings from './src/lib/settings.js';
import makeSocket from './src/socket.js';
const { host, port, features } = Settings;
const logger = makeLogger('HTTP');
const cpus = Math.floor(os.cpus().length / 2);
if (cluster.isPrimary && cpus > 2) {
logger.info(`Master process ${process.pid} starting up with ${cpus} workers.`);
for (let i = 0; i < cpus; i += 1) cluster.fork();
cluster.on('exit', (worker) => {
logger.info(`Worker process ${worker.process.pid} died. Restarting...`);
cluster.fork();
});
} else {
const server = http.createServer(app);
if (features.includes('SOCKET')) makeSocket(server);
logger.info(`Listening to ${host}:${port}`);
server.listen(port, host);
}