Skip to content

Commit

Permalink
Cleaning up res.locals
Browse files Browse the repository at this point in the history
- closes TryGhost#124
- added new middleware to detect if a route should be admin or not
- updated ghostLocals to use this and return different locals
- updated ghost.js#initTheme to use the same test
  • Loading branch information
ErisDS committed Jun 25, 2013
1 parent f7287cd commit fe0076e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 24 deletions.
67 changes: 44 additions & 23 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var express = require('express'),
// ## Custom Middleware
auth,
authAPI,
isGhostAdmin,
ghostLocals,
disableCachedResult,

Expand All @@ -30,18 +31,6 @@ var express = require('express'),
ghost = new Ghost();


ghost.app().configure('development', function () {
ghost.app().use(express.favicon(__dirname + '/content/images/favicon.ico'));
ghost.app().use(express.errorHandler({ dumpExceptions: true, showStack: true }));
ghost.app().use(express.logger('dev'));
ghost.app().use(I18n.load(ghost));
ghost.app().use(express.bodyParser({}));
ghost.app().use(express.cookieParser('try-ghost'));
ghost.app().use(express.cookieSession({ cookie: { maxAge: 60000000 }}));
ghost.app().use(ghost.initTheme(ghost.app()));
ghost.app().use(flash());
});

/**
* Authenticate a request by redirecting to login if not logged in
* We strip /ghost/ out of the redirect parameter for neatness
Expand Down Expand Up @@ -79,27 +68,45 @@ authAPI = function (req, res, next) {
next();
};

/**
* Expose the standard locals that every external page should have available;
* path, navItems and settingsCache
*/
ghostLocals = function (req, res, next) {
ghost.doFilter('ghostNavItems', {path: req.path, navItems: []}, function (navData) {
// Make sure we have a locals value.
res.locals = res.locals || {};
// #### isGhostAdmin
// Middleware which uses the URL to detect whether this response should be an admin response
// This is used to ensure the right content is served, and is not for security purposes
isGhostAdmin = function (req, res, next) {
res.isAdmin = /(^\/ghost$|^\/ghost\/)/.test(req.url);

next();
};

// Extend it with nav data and settings
_.extend(res.locals, navData, {
// Expose the standard locals that every external page should have available,
// separating between the frontend / theme and the admin
ghostLocals = function (req, res, next) {
// Make sure we have a locals value.
res.locals = res.locals || {};

if (!res.isAdmin) {
// filter the navigation items
ghost.doFilter('ghostNavItems', {path: req.path, navItems: []}, function (navData) {
// pass the theme navigation items and settings
_.extend(res.locals, navData, {
settings: ghost.settings()
});

next();
});
} else {
_.extend(res.locals, {
// pass the admin flash messages, settings and paths
messages: req.flash(),
settings: ghost.settings(),
availableThemes: ghost.paths().availableThemes,
availablePlugins: ghost.paths().availablePlugins
});

next();
});
}
};

// Disable any caching until it can be done properly
disableCachedResult = function (req, res, next) {
res.set({
"Cache-Control": "no-cache, must-revalidate",
Expand All @@ -109,6 +116,20 @@ disableCachedResult = function (req, res, next) {
next();
};

ghost.app().configure('development', function () {
ghost.app().use(isGhostAdmin);
ghost.app().use(express.favicon(__dirname + '/content/images/favicon.ico'));
ghost.app().use(express.errorHandler({ dumpExceptions: true, showStack: true }));
ghost.app().use(express.logger('dev'));
ghost.app().use(I18n.load(ghost));
ghost.app().use(express.bodyParser({}));
ghost.app().use(express.cookieParser('try-ghost'));
ghost.app().use(express.cookieSession({ cookie: { maxAge: 60000000 }}));
ghost.app().use(ghost.initTheme(ghost.app()));
ghost.app().use(flash());
});


// Expose the promise we will resolve after our pre-loading
ghost.loaded = loading.promise;

Expand Down
2 changes: 1 addition & 1 deletion core/ghost.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ Ghost.prototype.initTheme = function (app) {
var self = this;
return function initTheme(req, res, next) {
app.set('view engine', 'hbs');
if (/(^\/ghost$|^\/ghost\/)/.test(req.url) === false) {
if (!res.isAdmin) {
app.engine('hbs', hbs.express3(
{partialsDir: self.paths().activeTheme + 'partials'}
));
Expand Down

0 comments on commit fe0076e

Please sign in to comment.