mirror of
https://github.com/scratchfoundation/scratch-www.git
synced 2024-11-23 07:38:07 -05:00
36 lines
897 B
JavaScript
36 lines
897 B
JavaScript
|
var crypto = require('crypto');
|
||
|
var defaults = require('lodash.defaults');
|
||
|
var fs = require('fs');
|
||
|
var mustache = require('mustache');
|
||
|
var path = require('path');
|
||
|
|
||
|
/**
|
||
|
* Constructor
|
||
|
*/
|
||
|
function Handler (route) {
|
||
|
// Route definition
|
||
|
defaults(route, require('./defaults.json'));
|
||
|
|
||
|
// Render template
|
||
|
var location = path.resolve(__dirname, './template.html');
|
||
|
var template = fs.readFileSync(location, 'utf8');
|
||
|
var output = mustache.render(template, route);
|
||
|
var checksum = crypto.createHash('md5').update(output).digest('hex');
|
||
|
|
||
|
return function (req, res) {
|
||
|
res.set({
|
||
|
'Content-Type': 'text/html',
|
||
|
'Cache-Control': 'public, max-age=31536000',
|
||
|
'Etag': 'W/"' + checksum + '"'
|
||
|
});
|
||
|
res.send(output);
|
||
|
};
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Export a new instance
|
||
|
*/
|
||
|
module.exports = function (route) {
|
||
|
return new Handler(route);
|
||
|
};
|