mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-10 23:36:14 -05:00
refs #9192 - The AMP app is nothing more than a custom controller - this will come clear soon - Moved enabled/disabled logic into router - Removed error-related code, as this wasn't used - Changed logic for static pages to be based on req.body, not context - Improved the tests to match
51 lines
1.5 KiB
JavaScript
51 lines
1.5 KiB
JavaScript
var path = require('path'),
|
|
express = require('express'),
|
|
ampRouter = express.Router(),
|
|
i18n = require('../../../i18n'),
|
|
|
|
// Dirty requires
|
|
errors = require('../../../errors'),
|
|
templates = require('../../../controllers/frontend/templates'),
|
|
postLookup = require('../../../controllers/frontend/post-lookup'),
|
|
setResponseContext = require('../../../controllers/frontend/context');
|
|
|
|
function controller(req, res, next) {
|
|
var templateName = 'amp',
|
|
defaultTemplate = path.resolve(__dirname, 'views', templateName + '.hbs'),
|
|
view = templates.pickTemplate(templateName, defaultTemplate),
|
|
data = req.body || {};
|
|
|
|
// CASE: we only support amp pages for posts that are not static pages
|
|
if (!data.post || data.post.page) {
|
|
return next(new errors.NotFoundError({message: i18n.t('errors.errors.pageNotFound')}));
|
|
}
|
|
|
|
setResponseContext(req, res, data);
|
|
|
|
return res.render(view, data);
|
|
}
|
|
|
|
function getPostData(req, res, next) {
|
|
req.body = req.body || {};
|
|
|
|
postLookup(res.locals.relativeUrl)
|
|
.then(function (result) {
|
|
if (result && result.post) {
|
|
req.body.post = result.post;
|
|
}
|
|
|
|
next();
|
|
})
|
|
.catch(next);
|
|
}
|
|
|
|
// AMP frontend route
|
|
ampRouter.route('/')
|
|
.get(
|
|
getPostData,
|
|
controller
|
|
);
|
|
|
|
module.exports = ampRouter;
|
|
module.exports.controller = controller;
|
|
module.exports.getPostData = getPostData;
|