|
|
@ -7,6 +7,7 @@ var Fs = require('fs');
|
|
|
|
var Package = require('./package.json');
|
|
|
|
var Package = require('./package.json');
|
|
|
|
var Path = require("path");
|
|
|
|
var Path = require("path");
|
|
|
|
var nThen = require("nthen");
|
|
|
|
var nThen = require("nthen");
|
|
|
|
|
|
|
|
var Util = require("./lib/common-util");
|
|
|
|
|
|
|
|
|
|
|
|
var config = require("./lib/load-config");
|
|
|
|
var config = require("./lib/load-config");
|
|
|
|
|
|
|
|
|
|
|
@ -34,7 +35,9 @@ if (process.env.PACKAGE) {
|
|
|
|
FRESH_KEY = +new Date();
|
|
|
|
FRESH_KEY = +new Date();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var configCache = {};
|
|
|
|
config.flushCache = function () {
|
|
|
|
config.flushCache = function () {
|
|
|
|
|
|
|
|
configCache = {};
|
|
|
|
FRESH_KEY = +new Date();
|
|
|
|
FRESH_KEY = +new Date();
|
|
|
|
if (!(DEV_MODE || FRESH_MODE)) { FRESH_MODE = true; }
|
|
|
|
if (!(DEV_MODE || FRESH_MODE)) { FRESH_MODE = true; }
|
|
|
|
if (!config.log) { return; }
|
|
|
|
if (!config.log) { return; }
|
|
|
@ -143,16 +146,19 @@ try {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} catch (e) { console.error("Can't parse admin keys"); }
|
|
|
|
} catch (e) { console.error("Can't parse admin keys"); }
|
|
|
|
|
|
|
|
|
|
|
|
// TODO, cache this /api/config responses instead of re-computing it each time
|
|
|
|
var serveConfig = (function () {
|
|
|
|
app.get('/api/config', function(req, res){
|
|
|
|
// if dev mode: never cache
|
|
|
|
// TODO precompute any data that isn't dynamic to save some CPU time
|
|
|
|
var cacheString = function () {
|
|
|
|
var host = req.headers.host.replace(/\:[0-9]+/, '');
|
|
|
|
return (FRESH_KEY? '-' + FRESH_KEY: '') + (DEV_MODE? '-' + (+new Date()): '');
|
|
|
|
res.setHeader('Content-Type', 'text/javascript');
|
|
|
|
};
|
|
|
|
res.send('define(function(){\n' + [
|
|
|
|
|
|
|
|
|
|
|
|
var template = function (host) {
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
|
|
|
'define(function(){',
|
|
|
|
'var obj = ' + JSON.stringify({
|
|
|
|
'var obj = ' + JSON.stringify({
|
|
|
|
requireConf: {
|
|
|
|
requireConf: {
|
|
|
|
waitSeconds: 600,
|
|
|
|
waitSeconds: 600,
|
|
|
|
urlArgs: 'ver=' + Package.version + (FRESH_KEY? '-' + FRESH_KEY: '') + (DEV_MODE? '-' + (+new Date()): ''),
|
|
|
|
urlArgs: 'ver=' + Package.version + cacheString(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
removeDonateButton: (config.removeDonateButton === true),
|
|
|
|
removeDonateButton: (config.removeDonateButton === true),
|
|
|
|
allowSubscriptions: (config.allowSubscriptions === true),
|
|
|
|
allowSubscriptions: (config.allowSubscriptions === true),
|
|
|
@ -173,8 +179,39 @@ app.get('/api/config', function(req, res){
|
|
|
|
}()),
|
|
|
|
}()),
|
|
|
|
'return obj',
|
|
|
|
'return obj',
|
|
|
|
'});'
|
|
|
|
'});'
|
|
|
|
].join(';\n'));
|
|
|
|
].join(';\n')
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var cleanUp = {};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return function (req, res) {
|
|
|
|
|
|
|
|
var host = req.headers.host.replace(/\:[0-9]+/, '');
|
|
|
|
|
|
|
|
res.setHeader('Content-Type', 'text/javascript');
|
|
|
|
|
|
|
|
// don't cache anything if you're in dev mode
|
|
|
|
|
|
|
|
if (DEV_MODE) {
|
|
|
|
|
|
|
|
return void res.send(template(host));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate a lookup key for the cache
|
|
|
|
|
|
|
|
var cacheKey = host + ':' + cacheString();
|
|
|
|
|
|
|
|
// if there's nothing cached for that key...
|
|
|
|
|
|
|
|
if (!configCache[cacheKey]) {
|
|
|
|
|
|
|
|
// generate the response and cache it in memory
|
|
|
|
|
|
|
|
configCache[cacheKey] = template(host);
|
|
|
|
|
|
|
|
// and create a function to conditionally evict cache entries
|
|
|
|
|
|
|
|
// which have not been accessed in the last 20 seconds
|
|
|
|
|
|
|
|
cleanUp[cacheKey] = Util.throttle(function () {
|
|
|
|
|
|
|
|
delete cleanUp[cacheKey];
|
|
|
|
|
|
|
|
delete configCache[cacheKey];
|
|
|
|
|
|
|
|
}, 20000);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// successive calls to this function
|
|
|
|
|
|
|
|
cleanUp[cacheKey]();
|
|
|
|
|
|
|
|
return void res.send(configCache[cacheKey]);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.get('/api/config', serveConfig);
|
|
|
|
|
|
|
|
|
|
|
|
var four04_path = Path.resolve(__dirname + '/customize.dist/404.html');
|
|
|
|
var four04_path = Path.resolve(__dirname + '/customize.dist/404.html');
|
|
|
|
var custom_four04_path = Path.resolve(__dirname + '/customize/404.html');
|
|
|
|
var custom_four04_path = Path.resolve(__dirname + '/customize/404.html');
|
|
|
|