From 5b8e8e4b6792222884357bfe3499b1d1fa43404a Mon Sep 17 00:00:00 2001 From: psychobunny Date: Mon, 3 Mar 2014 12:16:46 -0500 Subject: [PATCH] deprecating use of templates.setGlobal on server side in favour of passing in api.config into res.locals (still needs work) --- src/controllers/api.js | 21 ++++++++++----------- src/middleware/middleware.js | 19 ++++++++++++++++++- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/controllers/api.js b/src/controllers/api.js index 36f0032d76..64a7a89885 100644 --- a/src/controllers/api.js +++ b/src/controllers/api.js @@ -7,9 +7,8 @@ var pkg = require('./../../package.json'), var apiController = {}; - -apiController.getConfig = function(req, res, next, callback) { - var config = require('../../public/config.json'); +apiController.getConfig = function(req, res, next) { + var config = require('./../../public/config.json'); config.version = pkg.version; config.postDelay = meta.config.postDelay; @@ -35,23 +34,23 @@ apiController.getConfig = function(req, res, next, callback) { config.environment = process.env.NODE_ENV; if (!req.user) { - return res.json(200, config); + if (res.locals.isAPI) { + res.json(200, config); + } else { + next(null, config); + } } if(req.user) { user.getSettings(req.user.uid, function(err, settings) { - if(err) { - return next(err); - } - config.usePagination = settings.usePagination; config.topicsPerPage = settings.topicsPerPage; config.postsPerPage = settings.postsPerPage; - if (callback) { - callback(err, config); - } else { + if (res.locals.isAPI) { res.json(200, config); + } else { + next(err, config); } }); } diff --git a/src/middleware/middleware.js b/src/middleware/middleware.js index 5057944d85..fbcc5196c6 100644 --- a/src/middleware/middleware.js +++ b/src/middleware/middleware.js @@ -13,7 +13,12 @@ var app, plugins = require('./../plugins'), meta = require('./../meta'), translator = require('./../../public/src/translator'), - user = require('./../user'); + user = require('./../user'), + + controllers = { + api: require('./../controllers/api') + }; + middleware.authenticate = function(req, res, next) { if(!req.user) { @@ -87,6 +92,12 @@ middleware.buildHeader = function(req, res, next) { res.locals.renderHeader = true; next(); }, + function(next) { + controllers.api.getConfig(req, res, function(err, config) { + res.locals.config = config; + next(); + }); + }, function(next) { // this is slower than the original implementation because the rendered template is not cached // but I didn't bother to fix this because we will deprecate [filter:footer.build] in favour of the widgets system by 0.4x @@ -158,6 +169,12 @@ middleware.renderHeader = function (options, callback) { '"': '"' }; + for (var key in options.res.locals.config) { + if (options.res.locals.config.hasOwnProperty(key)) { + templateValues[key] = options.res.locals.config[key]; + } + } + var uid = '0'; templateValues.metaTags = defaultMetaTags.concat(options.res.locals.metaTags || []).map(function(tag) {