mirror of
https://github.com/samsonjs/mit-license.git
synced 2026-04-27 15:07:42 +00:00
feat: Make server concurrent (multi-process)
Signed-off-by: Richie Bendall <richiebendall@gmail.com>
This commit is contained in:
parent
26e1505e45
commit
3689c60f55
1 changed files with 25 additions and 7 deletions
32
server.js
32
server.js
|
|
@ -5,19 +5,22 @@ IMPORTANT: Set the `github_token` environment variable to a personal access to
|
||||||
Server port: The `PORT` environment variable can also be set to control the port the server
|
Server port: The `PORT` environment variable can also be set to control the port the server
|
||||||
should be hosted on.
|
should be hosted on.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const minify = require('express-minify');
|
const minify = require('express-minify');
|
||||||
const postcssMiddleware = require('postcss-middleware');
|
const postcssMiddleware = require('postcss-middleware');
|
||||||
const tmpdir = require('os').tmpdir();
|
const tmpdir = require('os').tmpdir();
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
const cluster = require('cluster');
|
||||||
|
const numCPUs = require('os').cpus().length;
|
||||||
|
|
||||||
// Server
|
// Server
|
||||||
var PORT = process.env.PORT || 8080;
|
var PORT = process.env.PORT || 8080;
|
||||||
|
|
||||||
// Prepare application
|
// Prepare application
|
||||||
const app = express();
|
const app = express();
|
||||||
app.use(minify({ cache: tmpdir }));
|
app.use(minify({
|
||||||
|
cache: tmpdir
|
||||||
|
}));
|
||||||
app.set('views', path.join(__dirname, '/licenses'));
|
app.set('views', path.join(__dirname, '/licenses'));
|
||||||
app.set('view engine', 'ejs');
|
app.set('view engine', 'ejs');
|
||||||
|
|
||||||
|
|
@ -45,7 +48,9 @@ app.use(
|
||||||
// CORS
|
// CORS
|
||||||
app.use(require('./middleware/cors'));
|
app.use(require('./middleware/cors'));
|
||||||
// Parse URL-encoded bodies (as sent by HTML forms)
|
// Parse URL-encoded bodies (as sent by HTML forms)
|
||||||
app.use(express.urlencoded({ extended: true }));
|
app.use(express.urlencoded({
|
||||||
|
extended: true
|
||||||
|
}));
|
||||||
// Parse JSON bodies (as sent by API clients)
|
// Parse JSON bodies (as sent by API clients)
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
|
|
@ -57,7 +62,20 @@ app.use(require('./middleware/load-options'));
|
||||||
app.post('/', require('./routes/post'));
|
app.post('/', require('./routes/post'));
|
||||||
app.get('/*', require('./routes/get'));
|
app.get('/*', require('./routes/get'));
|
||||||
|
|
||||||
// Start listening for HTTP requests
|
// If the current process is the main one
|
||||||
app.listen(PORT, () => {
|
if (cluster.isMaster) {
|
||||||
console.log(`🚀 on http://localhost:${PORT}`);
|
// Create processes relative to amount of CPUs
|
||||||
});
|
Array.from({
|
||||||
|
length: numCPUs
|
||||||
|
}, () => cluster.fork());
|
||||||
|
|
||||||
|
// When worker closed
|
||||||
|
cluster.on('exit', worker => {
|
||||||
|
console.log(`❌ Worker ${worker.process.pid} died.`);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// Start listening for HTTP requests
|
||||||
|
app.listen(PORT, () => {
|
||||||
|
console.log(`🚀 on http://localhost:${PORT}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue