diff --git a/index.js b/index.js index 39bce7f..e48f2df 100644 --- a/index.js +++ b/index.js @@ -6,11 +6,14 @@ var morgan = require('morgan') var ngn4 = require('ngn4'); var config = require('./config.json'); +var util = require('./util.js'); +var scriptMethods = {}; var app = express(); var accessLogStream = fs.createWriteStream(__dirname + '/access.log', {flags: 'a'}); app.use(require('compression')()); +app.use(require('cookie-parser')()); app.use(morgan('combined', {stream: accessLogStream})); // Load data @@ -23,12 +26,22 @@ fs.readdirSync(dir).forEach(function(file) { // Prepare merged data file for client to use. var data = set.export(); +var indexFile = null; + +// Get index.html +fs.readFile(path.join(__dirname + '/www-root/index.html'), 'utf8', function (err, data) { + if (err) { + throw err; + } + + indexFile = data; +}); // Load scripts dir = path.join(__dirname, 'scripts'); fs.readdirSync(dir).forEach(function(file) { console.log('Running ' + file + '...'); - require('./scripts/' + file)(set, app, data); + require('./scripts/' + file)(set, app, data, scriptMethods); }); // Prepare static directories @@ -38,8 +51,14 @@ app.use('/img/', express.static('www-root/img')); // Set up index function sendIndexHtml(req, res) { - res.header("Content-Type", "text/html; charset=utf-8"); - res.sendFile(path.join(__dirname + '/www-root/index.html')); + res.header("Content-Type", "text/html; charset=utf-8"); + + res.end(util.formatTemplate(indexFile, { + generatorStyle: scriptMethods.getBackgroundFromCookie(req.cookies), + inited: true, + initData: scriptMethods.getInitDataFromCookie(req.cookies), + genList: scriptMethods.getGeneratorListJsonData() + })); } app.get('/index.html', sendIndexHtml); app.get('/', sendIndexHtml); diff --git a/package.json b/package.json index 7dbdc7b..3d0d13e 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,8 @@ "author": "Gisle", "license": "MIT", "dependencies": { + "compression": "^1.6.1", + "cookie-parser": "^1.4.1", "express": "^4.13.3", "jsonpack": "^1.1.4", "morgan": "^1.7.0", diff --git a/scripts/bg.js b/scripts/bg.js index 0aee73a..a634d29 100644 --- a/scripts/bg.js +++ b/scripts/bg.js @@ -12,9 +12,10 @@ function existsSync(filename) { } } -module.exports = function(set, app, data) { +module.exports = function(set, app, data, scriptMethods) { var keys = Object.keys(set.generators); var cache = {}; + var fileNames = {}; for(var i = 0; i < keys.length; ++i) { var fileName = "bg_" + keys[i].split('/').join('_') + ".png"; @@ -44,5 +45,19 @@ module.exports = function(set, app, data) { }).bind(binding)); console.log('[INFO] bg for "'+keys[i]+'" = "'+fileName+'"'); + + fileNames[keys[i]] = fileName; + } + + scriptMethods.getBackgroundFromCookie = function(cookies) { + var key; + + if(Object.prototype.hasOwnProperty.call(cookies, 'last_generator')) { + key = cookies['last_generator']; + } else { + key = scriptMethods.getDefaultDataKey(); + } + + return 'body { background-image: url(' + fileNames[key] + '); }' } } diff --git a/scripts/data.js b/scripts/data.js index 247f40b..07b261b 100644 --- a/scripts/data.js +++ b/scripts/data.js @@ -1,6 +1,7 @@ -module.exports = function(set, app, data) { +module.exports = function(set, app, data, scriptMethods) { var keys = Object.keys(set.generators); var cache = {}; + var defaultKey = null; for(var i = 0; i < keys.length; ++i) { var bindObj = {key: keys[i]} @@ -22,6 +23,27 @@ module.exports = function(set, app, data) { if(i === 0) { app.get('/d/default', func); + defaultKey = keys[i]; } } + + 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]; + } } diff --git a/scripts/list.js b/scripts/list.js index dd853c9..4f651ef 100644 --- a/scripts/list.js +++ b/scripts/list.js @@ -1,4 +1,4 @@ -module.exports = function(set, app) { +module.exports = function(set, app, data, scriptMethods) { var setHeader = { categories: set.categoryNames, generators: {} @@ -10,8 +10,14 @@ module.exports = function(set, app) { setHeader.generators[genKeys[i]] = set.generators[genKeys[i]].name; } + var setHeaderJson = JSON.stringify(setHeader); + + scriptMethods.getGeneratorListJsonData = function() { + return setHeaderJson; + } + app.get('/l/generators', function (req, res) { res.header("Content-Type", "application/json; charset=utf-8"); - res.end(JSON.stringify(setHeader)); + res.end(setHeaderJson); }); } diff --git a/util.js b/util.js new file mode 100644 index 0000000..ab359cc --- /dev/null +++ b/util.js @@ -0,0 +1,11 @@ +module.exports = { + formatTemplate: function(text, data) { + var keys = Object.keys(data); + for(var i = 0; i < keys.length; ++i) { + var key = keys[i]; + text = text.replace('{' + key + '}', data[key]); + } + + return text; + } +} diff --git a/www-root/index.html b/www-root/index.html index acdc69b..2b5e4da 100644 --- a/www-root/index.html +++ b/www-root/index.html @@ -9,7 +9,7 @@ - +
@@ -29,4 +29,9 @@ +