0
Fork 0
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:
Jason Williams 2015-05-27 23:15:38 -05:00
commit 1f8abc3869
3 changed files with 47 additions and 28 deletions

View file

@ -19,11 +19,11 @@ function isPaginated(req) {
} }
function isTag(req) { function isTag(req) {
return req.route.path.indexOf('/' + config.routeKeywords.tag + '/') !== -1; return req.originalUrl.indexOf('/' + config.routeKeywords.tag + '/') !== -1;
} }
function isAuthor(req) { function isAuthor(req) {
return req.route.path.indexOf('/' + config.routeKeywords.author + '/') !== -1; return req.originalUrl.indexOf('/' + config.routeKeywords.author + '/') !== -1;
} }
function handleError(next) { function handleError(next) {
@ -226,7 +226,7 @@ generate = function (req, res, next) {
data.feedUrl = config.urlFor({relativeUrl: baseUrl, secure: req.secure}, true); data.feedUrl = config.urlFor({relativeUrl: baseUrl, secure: req.secure}, true);
data.secure = req.secure; 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.set('Content-Type', 'text/xml; charset=UTF-8');
res.send(feedXml); res.send(feedXml);
}); });

View file

@ -8,7 +8,12 @@ var frontend = require('../controllers/frontend'),
frontendRoutes = function (middleware) { frontendRoutes = function (middleware) {
var router = express.Router(), var router = express.Router(),
subdir = config.paths.subdir, 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 // ### Admin routes
router.get(/^\/(logout|signout)\/$/, function redirect(req, res) { router.get(/^\/(logout|signout)\/$/, function redirect(req, res) {
@ -29,44 +34,51 @@ frontendRoutes = function (middleware) {
}); });
// password-protected frontend route // password-protected frontend route
router.get('/' + routeKeywords.private + '/', privateRouter.route('/')
middleware.isPrivateSessionAuth, .get(
frontend.private middleware.isPrivateSessionAuth,
); frontend.private
router.post('/' + routeKeywords.private + '/', )
middleware.isPrivateSessionAuth, .post(
middleware.spamProtectedPrevention, middleware.isPrivateSessionAuth,
middleware.authenticateProtection, middleware.spamProtectedPrevention,
frontend.private middleware.authenticateProtection,
); frontend.private
);
// ### Frontend routes rssRouter.route('/rss/').get(frontend.rss);
router.get('/rss/', frontend.rss); rssRouter.route('/rss/:page/').get(frontend.rss);
router.get('/rss/:page/', frontend.rss); rssRouter.route('/feed/').get(function redirect(req, res) {
router.get('/feed/', function redirect(req, res) {
/*jshint unused:true*/ /*jshint unused:true*/
res.set({'Cache-Control': 'public, max-age=' + utils.ONE_YEAR_S}); res.set({'Cache-Control': 'public, max-age=' + utils.ONE_YEAR_S});
res.redirect(301, subdir + '/rss/'); res.redirect(301, subdir + '/rss/');
}); });
// Index
indexRouter.route('/').get(frontend.homepage);
indexRouter.route('/' + routeKeywords.page + '/:page/').get(frontend.homepage);
indexRouter.use(rssRouter);
// Tags // Tags
router.get('/' + routeKeywords.tag + '/:slug/rss/', frontend.rss); tagRouter.route('/').get(frontend.tag);
router.get('/' + routeKeywords.tag + '/:slug/rss/:page/', frontend.rss); tagRouter.route('/' + routeKeywords.page + '/:page/').get(frontend.tag);
router.get('/' + routeKeywords.tag + '/:slug/' + routeKeywords.page + '/:page/', frontend.tag); tagRouter.use(rssRouter);
router.get('/' + routeKeywords.tag + '/:slug/', frontend.tag);
// Authors // Authors
router.get('/' + routeKeywords.author + '/:slug/rss/', frontend.rss); authorRouter.route('/').get(frontend.author);
router.get('/' + routeKeywords.author + '/:slug/rss/:page/', frontend.rss); authorRouter.route('/' + routeKeywords.page + '/:page/').get(frontend.author);
router.get('/' + routeKeywords.author + '/:slug/' + routeKeywords.page + '/:page/', frontend.author); authorRouter.use(rssRouter);
router.get('/' + routeKeywords.author + '/:slug/', frontend.author);
// 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 // Post Live Preview
router.get('/' + routeKeywords.preview + '/:uuid', frontend.preview); router.get('/' + routeKeywords.preview + '/:uuid', frontend.preview);
// Default // Default
router.get('/' + routeKeywords.page + '/:page/', frontend.homepage);
router.get('/', frontend.homepage);
router.get('*', frontend.single); router.get('*', frontend.single);
return router; return router;

View file

@ -1374,6 +1374,7 @@ describe('Frontend Controller', function () {
it('Redirects to rss if page number is 0', function () { it('Redirects to rss if page number is 0', function () {
var req = {params: {page: -1}, route: {path: '/rss/:page/'}}; var req = {params: {page: -1}, route: {path: '/rss/:page/'}};
req.originalUrl = req.route.path.replace(':page', req.params.page);
frontend.rss(req, res, null); frontend.rss(req, res, null);
@ -1384,6 +1385,7 @@ describe('Frontend Controller', function () {
it('Redirects to rss if page number is 0', function () { it('Redirects to rss if page number is 0', function () {
var req = {params: {page: 0}, route: {path: '/rss/:page/'}}; var req = {params: {page: 0}, route: {path: '/rss/:page/'}};
req.originalUrl = req.route.path.replace(':page', req.params.page);
frontend.rss(req, res, null); frontend.rss(req, res, null);
@ -1394,6 +1396,7 @@ describe('Frontend Controller', function () {
it('Redirects to home if page number is 1', function () { it('Redirects to home if page number is 1', function () {
var req = {params: {page: 1}, route: {path: '/rss/:page/'}}; var req = {params: {page: 1}, route: {path: '/rss/:page/'}};
req.originalUrl = req.route.path.replace(':page', req.params.page);
frontend.rss(req, res, null); frontend.rss(req, res, null);
@ -1406,6 +1409,7 @@ describe('Frontend Controller', function () {
config.set({url: 'http://testurl.com/blog'}); config.set({url: 'http://testurl.com/blog'});
var req = {params: {page: 0}, route: {path: '/rss/:page/'}}; var req = {params: {page: 0}, route: {path: '/rss/:page/'}};
req.originalUrl = req.route.path.replace(':page', req.params.page);
frontend.rss(req, res, null); frontend.rss(req, res, null);
@ -1418,6 +1422,7 @@ describe('Frontend Controller', function () {
config.set({url: 'http://testurl.com/blog'}); config.set({url: 'http://testurl.com/blog'});
var req = {params: {page: 1}, route: {path: '/rss/:page/'}}; var req = {params: {page: 1}, route: {path: '/rss/:page/'}};
req.originalUrl = req.route.path.replace(':page', req.params.page);
frontend.rss(req, res, null); frontend.rss(req, res, null);
@ -1430,6 +1435,7 @@ describe('Frontend Controller', function () {
config.set({url: 'http://testurl.com/'}); config.set({url: 'http://testurl.com/'});
var req = {params: {page: 4}, route: {path: '/rss/:page/'}}; 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 () { frontend.rss(req, res, done).then(function () {
res.redirect.called.should.be.true; res.redirect.called.should.be.true;
@ -1443,6 +1449,7 @@ describe('Frontend Controller', function () {
config.set({url: 'http://testurl.com/blog'}); config.set({url: 'http://testurl.com/blog'});
var req = {params: {page: 4}, route: {path: '/rss/:page/'}}; 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 () { frontend.rss(req, res, done).then(function () {
res.redirect.calledOnce.should.be.true; res.redirect.calledOnce.should.be.true;