mirror of
https://git.sleeping.town/mirrors/foxy-moxy
synced 2024-11-24 04:57:42 +00:00
factor our cluster into run.js
This commit is contained in:
parent
f5712fef77
commit
a5b32e9d53
2 changed files with 29 additions and 26 deletions
13
run.js
Normal file
13
run.js
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
const cluster = require('express-cluster');
|
||||||
|
const app = require('./server.js');
|
||||||
|
|
||||||
|
activePort = process.env.PORT || 3000;
|
||||||
|
|
||||||
|
cluster((worker) => {
|
||||||
|
app.listen(activePort, () => {
|
||||||
|
console.log('worker ' + worker.id + ' is listening on port ' + activePort);
|
||||||
|
});
|
||||||
|
}, {
|
||||||
|
'respawn': true, // workers will restart on failure
|
||||||
|
'verbose': true, // logs what happens to console
|
||||||
|
});
|
42
server.js
42
server.js
|
@ -5,7 +5,6 @@ try {
|
||||||
}
|
}
|
||||||
|
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const cluster = require('express-cluster');
|
|
||||||
const uuid = require('uuid/v4');
|
const uuid = require('uuid/v4');
|
||||||
const sanitize = require('sanitize-filename');
|
const sanitize = require('sanitize-filename');
|
||||||
const Canvas = require('canvas');
|
const Canvas = require('canvas');
|
||||||
|
@ -22,31 +21,22 @@ function composeImage(width, height, seed) {
|
||||||
};
|
};
|
||||||
|
|
||||||
const cacheTimeout = 60 * 60 * 24 * 30;
|
const cacheTimeout = 60 * 60 * 24 * 30;
|
||||||
|
const app = express();
|
||||||
|
|
||||||
cluster((worker) => {
|
app.get('/healthcheck', (req, res) => {
|
||||||
const app = express();
|
res.status(200).end();
|
||||||
|
});
|
||||||
|
|
||||||
app.get('/healthcheck', (req, res) => {
|
app.get('/:width/:seed', (req, res) => {
|
||||||
res.status(200).end();
|
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('/:width/:seed', (req, res) => {
|
module.exports = app;
|
||||||
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);
|
|
||||||
});
|
|
||||||
}, {
|
|
||||||
'respawn': true, // workers will restart on failure
|
|
||||||
'verbose': true, // logs what happens to console
|
|
||||||
})
|
|
||||||
|
|
Loading…
Reference in a new issue