Ghost/core/server/controllers/entry.js
Hannah Wolfe 98f5ae00fc
Introduced renderer to DRY up controllers (#9235)
refs #5091, #9192

- Renderer figures out templates, contexts, and does a render call
- Templating is now handled with a single function
- Context call is made in the renderer

Note:  to make this work, all controllers now define a little bit of config, currently stored in res._route. (That's a totally temporary location, as is res._template... when a sensible naming convention reveals itself I'll get rid of the weird _). This exposes a type and for custom routes a template name & default.
2017-11-10 12:44:29 +00:00

48 lines
1.6 KiB
JavaScript

var utils = require('../utils'),
filters = require('../filters'),
handleError = require('./frontend/error'),
postLookup = require('./frontend/post-lookup'),
renderEntry = require('./frontend/render-entry'),
setRequestIsSecure = require('./frontend/secure');
// This here is a controller.
// It renders entries = individual posts or pages
// The "route" is handled in site/routes.js
module.exports = function entryController(req, res, next) {
// Note: this is super similar to the config middleware used in channels
// @TODO refactor into to something explicit
res._route = {
type: 'entry'
};
// Query database to find post
return postLookup(req.path).then(function then(lookup) {
// Format data 1
var post = lookup ? lookup.post : false;
if (!post) {
return next();
}
// CASE: postlookup can detect options for example /edit, unknown options get ignored and end in 404
if (lookup.isUnknownOption) {
return next();
}
// CASE: last param is of url is /edit, redirect to admin
if (lookup.isEditURL) {
return utils.url.redirectToAdmin(302, res, '#/editor/' + post.id);
}
// CASE: permalink is not valid anymore, we redirect him permanently to the correct one
if (post.url !== req.path) {
return utils.url.redirect301(res, post.url);
}
setRequestIsSecure(req, post);
filters.doFilter('prePostsRender', post, res.locals)
.then(renderEntry(req, res));
}).catch(handleError(next));
};