0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

Merge pull request #779 from ErisDS/middleware

Refactoring Ghost middleware
This commit is contained in:
Hannah Wolfe 2013-09-16 18:34:45 -07:00
commit 09ca5e6298
5 changed files with 170 additions and 96 deletions

View file

@ -323,61 +323,4 @@ Ghost.prototype.initPlugins = function (pluginsToLoad) {
}, errors.logAndThrowError);
};
// Initialise Theme or admin
Ghost.prototype.initTheme = function (app) {
var self = this,
oneYear = 31536000000;
app.set('view engine', 'hbs');
// return the correct mime type for woff files
express['static'].mime.define({'application/font-woff': ['woff']});
// Serve the assets of the current theme
app.use(express['static'](self.paths().activeTheme));
// Serve shared assets and images
app.use('/shared', express['static'](path.join(__dirname, '/shared')));
app.use('/content/images', express['static'](path.join(__dirname, '/../content/images')));
// Serve our built scripts; can't use /scripts here because themes already are
app.use("/built/scripts", express['static'](path.join(__dirname, '/built/scripts'), {
// Put a maxAge of one year on built scripts
maxAge: oneYear
}));
return function initTheme(req, res, next) {
var hbsOptions;
if (!res.isAdmin) {
// self.globals is a hack til we have a better way of getting combined settings & config
hbsOptions = {templateOptions: {data: {blog: self.blogGlobals()}}};
if (!self.themeDirectories.hasOwnProperty(self.settings('activeTheme'))) {
// Throw an error if the theme is not available...
// TODO: move this to happen on app start
errors.logAndThrowError('The currently active theme ' + self.settings('activeTheme') + ' is missing.');
} else if (self.themeDirectories[self.settings('activeTheme')].hasOwnProperty('partials')) {
// Check that the theme has a partials directory before trying to use it
hbsOptions.partialsDir = path.join(self.paths().activeTheme, 'partials');
}
app.engine('hbs', hbs.express3(hbsOptions));
app.set('views', self.paths().activeTheme);
} else {
app.engine('hbs', hbs.express3({partialsDir: self.paths().adminViews + 'partials'}));
app.set('views', self.paths().adminViews);
app.use('/public', express['static'](path.join(__dirname, '/client/assets')));
app.use('/public', express['static'](path.join(__dirname, '/client')));
}
next();
};
};
// TODO: Expose the defaults for other people to see/manipulate as a static value?
// Ghost.defaults = defaults;
module.exports = Ghost;
module.exports = Ghost;

View file

@ -8,6 +8,8 @@ var express = require('express'),
admin = require('./server/controllers/admin'),
frontend = require('./server/controllers/frontend'),
api = require('./server/api'),
path = require('path'),
hbs = require('express-hbs'),
Ghost = require('./ghost'),
I18n = require('./shared/lang/i18n'),
helpers = require('./server/helpers'),
@ -92,15 +94,6 @@ function authAPI(req, res, next) {
next();
}
// ### GhostAdmin Middleware
// 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
function isGhostAdmin(req, res, next) {
res.isAdmin = /(^\/ghost$|^\/ghost\/)/.test(req.url);
next();
}
// ### GhostLocals Middleware
// Expose the standard locals that every external page should have available,
// separating between the theme and the admin
@ -141,38 +134,162 @@ function disableCachedResult(req, res, next) {
next();
}
// ### whenEnabled Middleware
// Selectively use middleware
// From https://github.com/senchalabs/connect/issues/676#issuecomment-9569658
function whenEnabled(setting, fn) {
return function settingEnabled(req, res, next) {
if (server.enabled(setting)) {
fn(req, res, next);
} else {
next();
}
};
}
// ### InitViews Middleware
// Initialise Theme or Admin Views
function initViews(req, res, next) {
var hbsOptions;
if (!res.isAdmin) {
// self.globals is a hack til we have a better way of getting combined settings & config
hbsOptions = {templateOptions: {data: {blog: ghost.blogGlobals()}}};
if (ghost.themeDirectories[ghost.settings('activeTheme')].hasOwnProperty('partials')) {
// Check that the theme has a partials directory before trying to use it
hbsOptions.partialsDir = path.join(ghost.paths().activeTheme, 'partials');
}
server.engine('hbs', hbs.express3(hbsOptions));
server.set('views', ghost.paths().activeTheme);
} else {
server.engine('hbs', hbs.express3({partialsDir: ghost.paths().adminViews + 'partials'}));
server.set('views', ghost.paths().adminViews);
}
next();
}
// ### Activate Theme
// Helper for manageAdminAndTheme
function activateTheme() {
var stackLocation = _.indexOf(server.stack, _.find(server.stack, function (stackItem, key) {
return stackItem.route === '' && stackItem.handle.name === 'settingEnabled';
}));
// clear the view cache
server.cache = {};
server.disable(server.get('activeTheme'));
server.set('activeTheme', ghost.settings('activeTheme'));
server.enable(server.get('activeTheme'));
if (stackLocation) {
server.stack[stackLocation].handle = whenEnabled(server.get('activeTheme'), express['static'](ghost.paths().activeTheme));
}
}
// ### ManageAdminAndTheme Middleware
// 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
function manageAdminAndTheme(req, res, next) {
// TODO improve this regex
res.isAdmin = /(^\/ghost\/)/.test(req.url);
if (res.isAdmin) {
server.enable('admin');
server.disable(server.get('activeTheme'));
} else {
server.enable(server.get('activeTheme'));
server.disable('admin');
}
// Check if the theme changed
if (ghost.settings('activeTheme') !== server.get('activeTheme')) {
// Change theme
if (!ghost.themeDirectories.hasOwnProperty(ghost.settings('activeTheme'))) {
if (!res.isAdmin) {
// Throw an error if the theme is not available, but not on the admin UI
errors.logAndThrowError('The currently active theme ' + ghost.settings('activeTheme') + ' is missing.');
}
} else {
activateTheme();
}
}
next();
}
// Expose the promise we will resolve after our pre-loading
ghost.loaded = loading.promise;
when.all([ghost.init(), helpers.loadCoreHelpers(ghost)]).then(function () {
// ##Configuration
server.configure(function () {
server.use(isGhostAdmin);
server.use(express.favicon(__dirname + '/shared/favicon.ico'));
server.use(I18n.load(ghost));
server.use(express.bodyParser({}));
server.use(express.bodyParser({uploadDir: __dirname + '/content/images'}));
server.use(express.cookieParser(ghost.dbHash));
server.use(express.cookieSession({ cookie: { maxAge: 60000000 }}));
server.use(ghost.initTheme(server));
if (process.env.NODE_ENV !== "development") {
server.use(express.logger());
server.use(express.errorHandler({ dumpExceptions: false, showStack: false }));
}
});
var oneYear = 31536000000;
// Development only configuration
server.configure("development", function () {
server.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
// Logging configuration
if (server.get('env') !== "development") {
server.use(express.logger());
} else {
server.use(express.logger('dev'));
});
}
// post init config
// return the correct mime type for woff filess
express['static'].mime.define({'application/font-woff': ['woff']});
// Shared static config
server.use('/shared', express['static'](path.join(__dirname, '/shared')));
server.use('/content/images', express['static'](path.join(__dirname, '/../content/images')));
// Serve our built scripts; can't use /scripts here because themes already are
server.use("/built/scripts", express['static'](path.join(__dirname, '/built/scripts'), {
// Put a maxAge of one year on built scripts
maxAge: oneYear
}));
// First determine whether we're serving admin or theme content
server.use(manageAdminAndTheme);
// set the view engine
server.set('view engine', 'hbs');
// Admin only config
server.use('/ghost', whenEnabled('admin', express['static'](path.join(__dirname, '/client/assets'))));
// Theme only config
server.use(whenEnabled(server.get('activeTheme'), express['static'](ghost.paths().activeTheme)));
server.use(express.favicon(__dirname + '/shared/favicon.ico'));
// server.use(I18n.load(ghost));
server.use(express.bodyParser({}));
server.use(express.bodyParser({uploadDir: __dirname + '/content/images'}));
server.use(express.cookieParser(ghost.dbHash));
server.use(express.cookieSession({ cookie: { maxAge: 60000000 }}));
// local data
server.use(ghostLocals);
// So on every request we actually clean out reduntant passive notifications from the server side
server.use(cleanNotifications);
// Initialise the views
server.use(initViews);
// process the application routes
server.use(server.router);
// ### Error handling
// TODO: replace with proper 400 and 500 error pages
// 404's
server.use(function error404Handler(req, res, next) {
console.log('test', req.url);
next();
//res.send(404, {message: "Page not found"});
});
// All other errors
if (server.get('env') === "production") {
server.use(express.errorHandler({ dumpExceptions: false, showStack: false }));
} else {
server.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
}
// ## Routing
// ### API routes
@ -232,9 +349,10 @@ when.all([ghost.init(), helpers.loadCoreHelpers(ghost)]).then(function () {
/* TODO: dynamic routing, homepage generator, filters ETC ETC */
server.get('/rss/', frontend.rss);
server.get('/rss/:page/', frontend.rss);
server.get('/page/:page/', frontend.homepage);
server.get('/:slug', frontend.single);
server.get('/', frontend.homepage);
server.get('/page/:page/', frontend.homepage);
// ## Start Ghost App

View file

@ -8,12 +8,14 @@ var Ghost = require('../../ghost'),
api = require('../api'),
RSS = require('rss'),
_ = require('underscore'),
errors = require('../errorHandling'),
when = require('when'),
ghost = new Ghost(),
frontendControllers;
frontendControllers = {
'homepage': function (req, res) {
'homepage': function (req, res, next) {
// Parse the page number
var pageParam = req.params.page !== undefined ? parseInt(req.params.page, 10) : 1,
postsPerPage = parseInt(ghost.settings('postsPerPage'), 10),
@ -55,16 +57,25 @@ frontendControllers = {
ghost.doFilter('prePostsRender', page.posts, function (posts) {
res.render('index', {posts: posts, pagination: {page: page.page, prev: page.prev, next: page.next, limit: page.limit, total: page.total, pages: page.pages}});
});
}).otherwise(function (err) {
return next(new Error(err));
});
},
'single': function (req, res) {
'single': function (req, res, next) {
api.posts.read({'slug': req.params.slug}).then(function (post) {
ghost.doFilter('prePostsRender', post.toJSON(), function (post) {
res.render('post', {post: post});
});
if (post) {
ghost.doFilter('prePostsRender', post.toJSON(), function (post) {
res.render('post', {post: post});
});
} else {
next();
}
}).otherwise(function (err) {
return next(new Error(err));
});
},
'rss': function (req, res) {
'rss': function (req, res, next) {
// Initialize RSS
var siteUrl = ghost.config().url,
pageParam = req.params.page !== undefined ? parseInt(req.params.page, 10) : 1,
@ -123,6 +134,8 @@ frontendControllers = {
res.send(feed.xml());
});
});
}).otherwise(function (err) {
return next(new Error(err));
});
}
};

View file

@ -17,7 +17,7 @@
<meta http-equiv="cleartype" content="on">
<link rel="stylesheet" type='text/css' href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,700'>
<link rel="stylesheet" href="/public/css/screen.css">
<link rel="stylesheet" href="/ghost/css/screen.css">
{{{block "pageStyles"}}}
</head>
<body class="{{bodyClass}}">
@ -39,7 +39,7 @@
{{{ghostScriptTags}}}
{{{block "bodyScripts"}}}
<script>
Ghost.init();
</script>

View file

@ -9,7 +9,7 @@
<li id="usermenu" class="subnav">
<a href="#" data-toggle="ul" class="dropdown">
<img class="avatar" src="{{#if currentUser.profile}}{{currentUser.profile}}{{else}}/public/img/user.jpg{{/if}}" alt="Avatar" />
<img class="avatar" src="{{#if currentUser.profile}}{{currentUser.profile}}{{else}}/ghost/img/user.jpg{{/if}}" alt="Avatar" />
<span class="name">{{#if currentUser.name}}{{currentUser.name}}{{else}}Ghost{{/if}} v{{version}}</span>
</a>
<ul class="overlay">