From 4624f8b56e56b3a44109b7722c72a4528487a5ab Mon Sep 17 00:00:00 2001 From: Darren Date: Tue, 20 Jan 2015 12:42:26 +0000 Subject: [PATCH] Using the public_http config option when reading available locales --- server/httphandler.js | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/server/httphandler.js b/server/httphandler.js index f0e7e10..d89ecad 100644 --- a/server/httphandler.js +++ b/server/httphandler.js @@ -8,10 +8,18 @@ var url = require('url'), +// Cached list of available translations +var cached_available_locales = null; + + var HttpHandler = function (config) { var public_http = config.public_http || 'client/'; this.file_server = new node_static.Server(public_http); + + if (!cached_available_locales) { + updateLocalesCache(); + } }; module.exports.HttpHandler = HttpHandler; @@ -85,25 +93,30 @@ HttpHandler.prototype.serve = function (request, response) { }; -// Cached list of available translations -var cached_available_locales = []; -// Get a list of the available translations we have -fs.readdir('../client/assets/locales', function (err, files) { - if (err) { - if (err.code === 'ENOENT') { - winston.error('No locale files could be found at ' + err.path); - } else { - winston.error('Error reading locales.', err); - } - } - (files || []).forEach(function (file) { - if (file.substr(-5) === '.json') { - cached_available_locales.push(file.slice(0, -5)); +/** + * Cache the available locales we have so we don't read the same directory for each request + **/ +function updateLocalesCache() { + cached_available_locales = []; + + fs.readdir(global.config.public_http + '/assets/locales', function (err, files) { + if (err) { + if (err.code === 'ENOENT') { + winston.error('No locale files could be found at ' + err.path); + } else { + winston.error('Error reading locales.', err); + } } + + (files || []).forEach(function (file) { + if (file.substr(-5) === '.json') { + cached_available_locales.push(file.slice(0, -5)); + } + }); }); -}); +} -- 2.25.1