mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -05:00
Merge pull request #5344 from ErisDS/frontend-route-refactor
Frontend route refactor / cleanup
This commit is contained in:
commit
1f8abc3869
3 changed files with 47 additions and 28 deletions
|
@ -19,11 +19,11 @@ function isPaginated(req) {
|
|||
}
|
||||
|
||||
function isTag(req) {
|
||||
return req.route.path.indexOf('/' + config.routeKeywords.tag + '/') !== -1;
|
||||
return req.originalUrl.indexOf('/' + config.routeKeywords.tag + '/') !== -1;
|
||||
}
|
||||
|
||||
function isAuthor(req) {
|
||||
return req.route.path.indexOf('/' + config.routeKeywords.author + '/') !== -1;
|
||||
return req.originalUrl.indexOf('/' + config.routeKeywords.author + '/') !== -1;
|
||||
}
|
||||
|
||||
function handleError(next) {
|
||||
|
@ -226,7 +226,7 @@ generate = function (req, res, next) {
|
|||
data.feedUrl = config.urlFor({relativeUrl: baseUrl, secure: req.secure}, true);
|
||||
data.secure = req.secure;
|
||||
|
||||
return getFeedXml(req.route.path, data).then(function (feedXml) {
|
||||
return getFeedXml(req.originalUrl, data).then(function (feedXml) {
|
||||
res.set('Content-Type', 'text/xml; charset=UTF-8');
|
||||
res.send(feedXml);
|
||||
});
|
||||
|
|
|
@ -8,7 +8,12 @@ var frontend = require('../controllers/frontend'),
|
|||
frontendRoutes = function (middleware) {
|
||||
var router = express.Router(),
|
||||
subdir = config.paths.subdir,
|
||||
routeKeywords = config.routeKeywords;
|
||||
routeKeywords = config.routeKeywords,
|
||||
indexRouter = express.Router(),
|
||||
tagRouter = express.Router({mergeParams: true}),
|
||||
authorRouter = express.Router({mergeParams: true}),
|
||||
rssRouter = express.Router({mergeParams: true}),
|
||||
privateRouter = express.Router();
|
||||
|
||||
// ### Admin routes
|
||||
router.get(/^\/(logout|signout)\/$/, function redirect(req, res) {
|
||||
|
@ -29,44 +34,51 @@ frontendRoutes = function (middleware) {
|
|||
});
|
||||
|
||||
// password-protected frontend route
|
||||
router.get('/' + routeKeywords.private + '/',
|
||||
middleware.isPrivateSessionAuth,
|
||||
frontend.private
|
||||
);
|
||||
router.post('/' + routeKeywords.private + '/',
|
||||
middleware.isPrivateSessionAuth,
|
||||
middleware.spamProtectedPrevention,
|
||||
middleware.authenticateProtection,
|
||||
frontend.private
|
||||
);
|
||||
privateRouter.route('/')
|
||||
.get(
|
||||
middleware.isPrivateSessionAuth,
|
||||
frontend.private
|
||||
)
|
||||
.post(
|
||||
middleware.isPrivateSessionAuth,
|
||||
middleware.spamProtectedPrevention,
|
||||
middleware.authenticateProtection,
|
||||
frontend.private
|
||||
);
|
||||
|
||||
// ### Frontend routes
|
||||
router.get('/rss/', frontend.rss);
|
||||
router.get('/rss/:page/', frontend.rss);
|
||||
router.get('/feed/', function redirect(req, res) {
|
||||
rssRouter.route('/rss/').get(frontend.rss);
|
||||
rssRouter.route('/rss/:page/').get(frontend.rss);
|
||||
rssRouter.route('/feed/').get(function redirect(req, res) {
|
||||
/*jshint unused:true*/
|
||||
res.set({'Cache-Control': 'public, max-age=' + utils.ONE_YEAR_S});
|
||||
res.redirect(301, subdir + '/rss/');
|
||||
});
|
||||
|
||||
// Index
|
||||
indexRouter.route('/').get(frontend.homepage);
|
||||
indexRouter.route('/' + routeKeywords.page + '/:page/').get(frontend.homepage);
|
||||
indexRouter.use(rssRouter);
|
||||
|
||||
// Tags
|
||||
router.get('/' + routeKeywords.tag + '/:slug/rss/', frontend.rss);
|
||||
router.get('/' + routeKeywords.tag + '/:slug/rss/:page/', frontend.rss);
|
||||
router.get('/' + routeKeywords.tag + '/:slug/' + routeKeywords.page + '/:page/', frontend.tag);
|
||||
router.get('/' + routeKeywords.tag + '/:slug/', frontend.tag);
|
||||
tagRouter.route('/').get(frontend.tag);
|
||||
tagRouter.route('/' + routeKeywords.page + '/:page/').get(frontend.tag);
|
||||
tagRouter.use(rssRouter);
|
||||
|
||||
// Authors
|
||||
router.get('/' + routeKeywords.author + '/:slug/rss/', frontend.rss);
|
||||
router.get('/' + routeKeywords.author + '/:slug/rss/:page/', frontend.rss);
|
||||
router.get('/' + routeKeywords.author + '/:slug/' + routeKeywords.page + '/:page/', frontend.author);
|
||||
router.get('/' + routeKeywords.author + '/:slug/', frontend.author);
|
||||
authorRouter.route('/').get(frontend.author);
|
||||
authorRouter.route('/' + routeKeywords.page + '/:page/').get(frontend.author);
|
||||
authorRouter.use(rssRouter);
|
||||
|
||||
// Mount the Routers
|
||||
router.use('/' + routeKeywords.private + '/', privateRouter);
|
||||
router.use('/' + routeKeywords.author + '/:slug/', authorRouter);
|
||||
router.use('/' + routeKeywords.tag + '/:slug/', tagRouter);
|
||||
router.use('/', indexRouter);
|
||||
|
||||
// Post Live Preview
|
||||
router.get('/' + routeKeywords.preview + '/:uuid', frontend.preview);
|
||||
|
||||
// Default
|
||||
router.get('/' + routeKeywords.page + '/:page/', frontend.homepage);
|
||||
router.get('/', frontend.homepage);
|
||||
router.get('*', frontend.single);
|
||||
|
||||
return router;
|
||||
|
|
|
@ -1374,6 +1374,7 @@ describe('Frontend Controller', function () {
|
|||
|
||||
it('Redirects to rss if page number is 0', function () {
|
||||
var req = {params: {page: -1}, route: {path: '/rss/:page/'}};
|
||||
req.originalUrl = req.route.path.replace(':page', req.params.page);
|
||||
|
||||
frontend.rss(req, res, null);
|
||||
|
||||
|
@ -1384,6 +1385,7 @@ describe('Frontend Controller', function () {
|
|||
|
||||
it('Redirects to rss if page number is 0', function () {
|
||||
var req = {params: {page: 0}, route: {path: '/rss/:page/'}};
|
||||
req.originalUrl = req.route.path.replace(':page', req.params.page);
|
||||
|
||||
frontend.rss(req, res, null);
|
||||
|
||||
|
@ -1394,6 +1396,7 @@ describe('Frontend Controller', function () {
|
|||
|
||||
it('Redirects to home if page number is 1', function () {
|
||||
var req = {params: {page: 1}, route: {path: '/rss/:page/'}};
|
||||
req.originalUrl = req.route.path.replace(':page', req.params.page);
|
||||
|
||||
frontend.rss(req, res, null);
|
||||
|
||||
|
@ -1406,6 +1409,7 @@ describe('Frontend Controller', function () {
|
|||
config.set({url: 'http://testurl.com/blog'});
|
||||
|
||||
var req = {params: {page: 0}, route: {path: '/rss/:page/'}};
|
||||
req.originalUrl = req.route.path.replace(':page', req.params.page);
|
||||
|
||||
frontend.rss(req, res, null);
|
||||
|
||||
|
@ -1418,6 +1422,7 @@ describe('Frontend Controller', function () {
|
|||
config.set({url: 'http://testurl.com/blog'});
|
||||
|
||||
var req = {params: {page: 1}, route: {path: '/rss/:page/'}};
|
||||
req.originalUrl = req.route.path.replace(':page', req.params.page);
|
||||
|
||||
frontend.rss(req, res, null);
|
||||
|
||||
|
@ -1430,6 +1435,7 @@ describe('Frontend Controller', function () {
|
|||
config.set({url: 'http://testurl.com/'});
|
||||
|
||||
var req = {params: {page: 4}, route: {path: '/rss/:page/'}};
|
||||
req.originalUrl = req.route.path.replace(':page', req.params.page);
|
||||
|
||||
frontend.rss(req, res, done).then(function () {
|
||||
res.redirect.called.should.be.true;
|
||||
|
@ -1443,6 +1449,7 @@ describe('Frontend Controller', function () {
|
|||
config.set({url: 'http://testurl.com/blog'});
|
||||
|
||||
var req = {params: {page: 4}, route: {path: '/rss/:page/'}};
|
||||
req.originalUrl = req.route.path.replace(':page', req.params.page);
|
||||
|
||||
frontend.rss(req, res, done).then(function () {
|
||||
res.redirect.calledOnce.should.be.true;
|
||||
|
|
Loading…
Add table
Reference in a new issue