mirror of
https://git.sleeping.town/mirrors/foxy-moxy
synced 2025-06-16 20:31:29 -07:00
use express-cluster for drop-in cluster support
This commit is contained in:
parent
1a42d9dbc1
commit
7c34463cc4
2 changed files with 24 additions and 19 deletions
42
server.js
42
server.js
|
@ -5,6 +5,7 @@ try {
|
|||
}
|
||||
|
||||
const express = require('express');
|
||||
const cluster = require('express-cluster');
|
||||
const uuid = require('uuid/v4');
|
||||
const sanitize = require('sanitize-filename');
|
||||
const Canvas = require('canvas');
|
||||
|
@ -20,26 +21,29 @@ function composeImage(width, height, seed) {
|
|||
return canvas;
|
||||
};
|
||||
|
||||
const app = express();
|
||||
|
||||
const cacheTimeout = 60 * 60 * 24 * 30;
|
||||
|
||||
app.get('/healthcheck', (req, res) => {
|
||||
res.status(200).end();
|
||||
});
|
||||
cluster((worker) => {
|
||||
const app = express();
|
||||
|
||||
app.get('/:width/:seed', (req, res) => {
|
||||
let width = parseInt(req.params.width) || 400;
|
||||
if (width > 400) width = 400;
|
||||
const seed = sanitize(req.params.seed) || uuid();
|
||||
const canvas = composeImage(width, width, seed);
|
||||
const buffer = canvas.toBuffer();
|
||||
res.set('Cache-Control', 'max-age=' + cacheTimeout);
|
||||
res.set('Content-length', buffer.length);
|
||||
res.type('png');
|
||||
res.end(buffer, 'binary');
|
||||
});
|
||||
app.get('/healthcheck', (req, res) => {
|
||||
res.status(200).end();
|
||||
});
|
||||
|
||||
app.listen(process.env.PORT || 3000, () => {
|
||||
console.log('listening on http://localhost:3000');
|
||||
});
|
||||
app.get('/:width/:seed', (req, res) => {
|
||||
let width = parseInt(req.params.width) || 400;
|
||||
if (width > 400) width = 400;
|
||||
const seed = sanitize(req.params.seed) || uuid();
|
||||
const canvas = composeImage(width, width, seed);
|
||||
const buffer = canvas.toBuffer();
|
||||
res.set('Cache-Control', 'max-age=' + cacheTimeout);
|
||||
res.set('Content-length', buffer.length);
|
||||
res.type('png');
|
||||
res.end(buffer, 'binary');
|
||||
});
|
||||
|
||||
activePort = process.env.PORT || 3000;
|
||||
app.listen(activePort, () => {
|
||||
console.log('worker ' + worker.id + ' is listening on port ' + activePort);
|
||||
});
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue