-var fs = require('fs');
+var fs = require('fs'),
+ events = require('events'),
+ util = require('util');
var config_filename = 'config.js',
config_dirs = ['/etc/kiwiirc/', __dirname + '/../'],
environment = 'production',
loaded_config = Object.create(null);
+var Config = function () {
+ events.EventEmitter.call(this);
+};
+util.inherits(Config, events.EventEmitter);
-function loadConfig() {
+Config.prototype.loadConfig = function () {
var new_config,
conf_filepath,
i;
if (new_config) {
loaded_config = new_config;
global.config = new_config[environment] || {};
+ this.emit('loaded');
return loaded_config;
} else {
return false;
}
-}
+};
-module.exports.setEnvironment = function (new_environment) {
+Config.prototype.setEnvironment = function (new_environment) {
environment = new_environment;
};
// Get the current config. Optionally for a different environment than currently set
-module.exports.get = function (specific_environment) {
+Config.prototype.get = function (specific_environment) {
specific_environment = specific_environment || environment;
return loaded_config[specific_environment] || {};
};
-module.exports.loadConfig = loadConfig;
+module.exports = new Config();
this.file_server.serveFile('/assets/locales/en-gb.json', 200, {Vary: 'Accept-Language', 'Content-Language': 'en-gb'}, request, response);
};
-function serveSettings(request, response) {
+var cached_settings = {
+ hash: '',
+ settings: ''
+};
+
+config.on('loaded', function () {
+ cached_settings.settings = '';
+ cached_settings.hash = '';
+});
+
+function generateSettings(request, callback) {
var vars = {
server_settings: {},
client_plugins: [],
fs.readFile(__dirname + '/../client/assets/src/translations/translations.json', function (err, translations) {
if (err) {
- response.statusCode = 500;
- return response.end();
+ return callback(err);
}
var translation_files;
fs.readdir(__dirname + '/../client/assets/src/translations/', function (err, pofiles) {
var hash;
if (err) {
- response.statusCode = 500;
- return response.end();
+ return callback(err);
}
pofiles.forEach(function (file) {
}
});
- hash = crypto.createHash('md5').update(JSON.stringify(vars)).digest('hex');
+ cached_settings.settings = JSON.stringify(vars);
+ cached_settings.hash = crypto.createHash('md5').update(cached_settings.settings).digest('hex');
- if (request.headers['if-none-match'] && request.headers['if-none-match'] === hash) {
- response.writeHead(304, 'Not Modified');
- return response.end();
- }
+ return callback(null, cached_settings);
+ });
+ });
+}
- response.writeHead(200, {
- 'ETag': hash,
- 'Content-Type': 'application/json'
- });
- response.end(JSON.stringify(vars));
+function serveSettings(request, response) {
+ if (cached_settings.settings === '') {
+ generateSettings(request, function (err, settings) {
+ if (err) {
+ response.statusCode = 500;
+ response.end();
+ } else {
+ sendSettings.call(this, request, response, settings);
+ }
});
+ } else {
+ sendSettings.call(this, request, response, cached_settings);
+ }
+}
+
+function sendSettings(request, response, settings) {
+ if (request.headers['if-none-match'] && request.headers['if-none-match'] === settings.hash) {
+ response.writeHead(304, 'Not Modified');
+ return response.end();
+ }
+
+ response.writeHead(200, {
+ 'ETag': settings.hash,
+ 'Content-Type': 'application/json'
});
+ response.end(settings.settings);
}