diff --git a/run.js b/run.js new file mode 100644 index 0000000..52f9f22 --- /dev/null +++ b/run.js @@ -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 +}); diff --git a/server.js b/server.js index 7db56fc..912edb4 100644 --- a/server.js +++ b/server.js @@ -5,7 +5,6 @@ try { } const express = require('express'); -const cluster = require('express-cluster'); const uuid = require('uuid/v4'); const sanitize = require('sanitize-filename'); const Canvas = require('canvas'); @@ -22,31 +21,22 @@ function composeImage(width, height, seed) { }; const cacheTimeout = 60 * 60 * 24 * 30; +const app = express(); -cluster((worker) => { - const app = express(); +app.get('/healthcheck', (req, res) => { + res.status(200).end(); +}); - app.get('/healthcheck', (req, res) => { - res.status(200).end(); - }); +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('/: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); - }); -}, { - 'respawn': true, // workers will restart on failure - 'verbose': true, // logs what happens to console -}) +module.exports = app;