foxy-moxy/server.js

45 lines
1.1 KiB
JavaScript
Raw Normal View History

2017-06-13 01:39:14 +00:00
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "newrelic" }] */
2016-12-19 23:52:20 +00:00
try {
2017-06-13 01:39:14 +00:00
const newrelic = require('newrelic')
2016-12-19 23:52:20 +00:00
} catch (e) {
2017-06-13 01:39:14 +00:00
console.error('WARNING unable to load newrelic')
2016-12-19 23:52:20 +00:00
}
2017-06-13 01:39:14 +00:00
const express = require('express')
const uuid = require('uuid/v4')
const sanitize = require('sanitize-filename')
const Canvas = require('canvas')
2016-12-16 18:05:14 +00:00
2017-06-13 01:39:14 +00:00
const Fox = require('./js/fox.js')
const renderFox = require('./js/render-fox.js')
2016-12-15 19:24:22 +00:00
2017-06-13 01:39:14 +00:00
function composeImage (width, height, seed) {
seed = seed || uuid()
const fox = Fox(width, height, seed)
const canvas = new Canvas(width, height)
renderFox(canvas, fox)
return canvas
2016-12-17 02:54:21 +00:00
};
2017-06-13 01:39:14 +00:00
const cacheTimeout = 60 * 60 * 24 * 30
const app = express()
2017-05-03 23:06:47 +00:00
app.get('/healthcheck', (req, res) => {
2017-06-13 01:39:14 +00:00
res.status(200).end()
})
2017-05-03 23:06:47 +00:00
app.get('/:width/:seed', (req, res) => {
2017-06-13 01:39:14 +00:00
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')
})
module.exports = app