module.exports = function(set, app, data, scriptMethods) { var keys = Object.keys(set.generators); var cache = {}; var defaultKey = null; var setJson = JSON.stringify(set.export()); for(var i = 0; i < keys.length; ++i) { var bindObj = {key: keys[i]} var func = (function (req, res) { var key = this.key; var gen = set.generators[key]; if(!cache.hasOwnProperty(key)) { data.generators[key].catId = key.split('/')[0]; cache[key] = JSON.stringify(data.generators[key]); } res.header("Content-Type", "application/json; charset=utf-8"); res.end(cache[key]); }).bind(bindObj); app.get('/d/' + keys[i], func); if(i === 0) { app.get('/d/default', func); defaultKey = keys[i]; } } app.get('/d/set', function(req, res) { res.header("Content-Type", "application/json; charset=utf-8"); res.end(setJson); }); scriptMethods.getDefaultDataKey = function() { return defaultKey; } scriptMethods.getInitDataFromCookie = function(cookies) { var key; if(Object.prototype.hasOwnProperty.call(cookies, 'last_generator')) { key = cookies['last_generator']; } else { key = defaultKey; } if(!cache.hasOwnProperty(key)) { data.generators[key].catId = key.split('/')[0]; cache[key] = JSON.stringify(data.generators[key]); } return cache[key]; } }