Skip to content

Commit

Permalink
Refactored routing config for multiple api versions
Browse files Browse the repository at this point in the history
refs TryGhost#10124

- one clean v0.1 and v2 config file for routing
  • Loading branch information
kirrg001 committed Jan 4, 2019
1 parent 62a669a commit d051050
Show file tree
Hide file tree
Showing 23 changed files with 4,683 additions and 2,035 deletions.
2 changes: 1 addition & 1 deletion core/server/apps/amp/lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function getPostData(req, res, next) {
}

// @NOTE: amp is not supported for static pages
helpers.entryLookup(urlWithoutSubdirectoryWithoutAmp, {permalinks, query: {resource: 'posts'}}, res.locals)
helpers.entryLookup(urlWithoutSubdirectoryWithoutAmp, {permalinks, query: {controller: 'posts', resource: 'posts'}}, res.locals)
.then((result) => {
if (result && result.entry) {
req.body.post = result.entry;
Expand Down
11 changes: 5 additions & 6 deletions core/server/services/routing/CollectionRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ const middlewares = require('./middlewares');
const RSSRouter = require('./RSSRouter');

class CollectionRouter extends ParentRouter {
constructor(mainRoute, object) {
constructor(mainRoute, object, RESOURCE_CONFIG) {
super('CollectionRouter');

this.RESOURCE_CONFIG = RESOURCE_CONFIG.QUERY.post;

this.routerName = mainRoute === '/' ? 'index' : mainRoute.replace(/\//g, '');

// NOTE: index/parent route e.g. /, /podcast/, /magic/ ;)
Expand Down Expand Up @@ -94,10 +96,7 @@ class CollectionRouter extends ParentRouter {
order: this.order,
permalinks: this.permalinks.getValue({withUrlOptions: true}),
resourceType: this.getResourceType(),
query: {
alias: 'posts',
resource: 'posts'
},
query: this.RESOURCE_CONFIG,
context: this.context,
frontPageTemplate: 'home',
templates: this.templates,
Expand Down Expand Up @@ -142,7 +141,7 @@ class CollectionRouter extends ParentRouter {
}

getResourceType() {
return 'posts';
return this.RESOURCE_CONFIG.resourceAlias || this.RESOURCE_CONFIG.resource;
}

getRoute(options) {
Expand Down
9 changes: 4 additions & 5 deletions core/server/services/routing/PreviewRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ const urlService = require('../url');
const controllers = require('./controllers');

class PreviewRouter extends ParentRouter {
constructor() {
constructor(RESOURCE_CONFIG) {
super('PreviewRouter');

this.RESOURCE_CONFIG = RESOURCE_CONFIG.QUERY.preview;

this.route = {value: '/p/'};

this._registerRoutes();
Expand All @@ -20,10 +22,7 @@ class PreviewRouter extends ParentRouter {
_prepareContext(req, res, next) {
res.routerOptions = {
type: 'entry',
query: {
alias: 'preview',
resource: 'posts'
}
query: this.RESOURCE_CONFIG
};

next();
Expand Down
24 changes: 16 additions & 8 deletions core/server/services/routing/StaticPagesRouter.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
const debug = require('ghost-ignition').debug('services:routing:static-pages-router');
const urlService = require('../url');
const ParentRouter = require('./ParentRouter');
const controllers = require('./controllers');
const common = require('../../lib/common');

class StaticPagesRouter extends ParentRouter {
constructor() {
constructor(RESOURCE_CONFIG) {
super('StaticPagesRouter');

this.RESOURCE_CONFIG = RESOURCE_CONFIG.QUERY.page;

this.permalinks = {
value: '/:slug/'
};

this.permalinks.getValue = () => {
this.permalinks.getValue = (options = {}) => {
options = options || {};

// @NOTE: url options are only required when registering urls in express.
// e.g. the UrlService will access the routes and doesn't want to know about possible url options
if (options.withUrlOptions) {
return urlService.utils.urlJoin(this.permalinks.value, '/:options(edit)?/');
}

return this.permalinks.value;
};

Expand All @@ -26,7 +37,7 @@ class StaticPagesRouter extends ParentRouter {
this.router().param('slug', this._respectDominantRouter.bind(this));

// REGISTER: permalink for static pages
this.mountRoute(this.permalinks.getValue(), controllers.entry);
this.mountRoute(this.permalinks.getValue({withUrlOptions: true}), controllers.entry);

common.events.emit('router.created', this);
}
Expand All @@ -35,12 +46,9 @@ class StaticPagesRouter extends ParentRouter {
res.routerOptions = {
type: 'entry',
filter: this.filter,
permalinks: this.permalinks.getValue(),
permalinks: this.permalinks.getValue({withUrlOptions: true}),
resourceType: this.getResourceType(),
query: {
alias: 'pages',
resource: 'posts'
},
query: this.RESOURCE_CONFIG,
context: ['page']
};

Expand Down
12 changes: 6 additions & 6 deletions core/server/services/routing/TaxonomyRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ const RSSRouter = require('./RSSRouter');
const urlService = require('../url');
const controllers = require('./controllers');
const middlewares = require('./middlewares');
const RESOURCE_CONFIG = require('./assets/resource-config');

class TaxonomyRouter extends ParentRouter {
constructor(key, permalinks) {
constructor(key, permalinks, RESOURCE_CONFIG) {
super('Taxonomy');

this.taxonomyKey = key;
this.RESOURCE_CONFIG = RESOURCE_CONFIG;

this.permalinks = {
value: permalinks
Expand Down Expand Up @@ -53,8 +53,8 @@ class TaxonomyRouter extends ParentRouter {
type: 'channel',
name: this.taxonomyKey,
permalinks: this.permalinks.getValue(),
data: {[this.taxonomyKey]: RESOURCE_CONFIG.QUERY[this.taxonomyKey]},
filter: RESOURCE_CONFIG.TAXONOMIES[this.taxonomyKey].filter,
data: {[this.taxonomyKey]: this.RESOURCE_CONFIG.QUERY[this.taxonomyKey]},
filter: this.RESOURCE_CONFIG.TAXONOMIES[this.taxonomyKey].filter,
resourceType: this.getResourceType(),
context: [this.taxonomyKey],
slugTemplate: true,
Expand All @@ -65,11 +65,11 @@ class TaxonomyRouter extends ParentRouter {
}

_redirectEditOption(req, res) {
urlService.utils.redirectToAdmin(302, res, RESOURCE_CONFIG.TAXONOMIES[this.taxonomyKey].editRedirect.replace(':slug', req.params.slug));
urlService.utils.redirectToAdmin(302, res, this.RESOURCE_CONFIG.TAXONOMIES[this.taxonomyKey].editRedirect.replace(':slug', req.params.slug));
}

getResourceType() {
return RESOURCE_CONFIG.QUERY[this.taxonomyKey].alias;
return this.RESOURCE_CONFIG.TAXONOMIES[this.taxonomyKey].resource;
}

getRoute() {
Expand Down
14 changes: 9 additions & 5 deletions core/server/services/routing/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const debug = require('ghost-ignition').debug('services:routing:bootstrap');
const _ = require('lodash');
const common = require('../../lib/common');
const settingsService = require('../settings');
const themeService = require('../themes');
const StaticRoutesRouter = require('./StaticRoutesRouter');
const StaticPagesRouter = require('./StaticPagesRouter');
const CollectionRouter = require('./CollectionRouter');
Expand Down Expand Up @@ -37,34 +38,37 @@ module.exports.init = (options = {start: false}) => {
* - is the PreviewRouter an app?
*/
module.exports.start = () => {
const previewRouter = new PreviewRouter();
const apiVersion = themeService.getActive().engine('ghost-api');
const RESOURCE_CONFIG = require(`../../services/routing/config/${apiVersion}`);

const previewRouter = new PreviewRouter(RESOURCE_CONFIG);

siteRouter.mountRouter(previewRouter.router());
registry.setRouter('previewRouter', previewRouter);

const dynamicRoutes = settingsService.get('routes');

_.each(dynamicRoutes.routes, (value, key) => {
const staticRoutesRouter = new StaticRoutesRouter(key, value);
const staticRoutesRouter = new StaticRoutesRouter(key, value, RESOURCE_CONFIG);
siteRouter.mountRouter(staticRoutesRouter.router());

registry.setRouter(staticRoutesRouter.identifier, staticRoutesRouter);
});

_.each(dynamicRoutes.taxonomies, (value, key) => {
const taxonomyRouter = new TaxonomyRouter(key, value);
const taxonomyRouter = new TaxonomyRouter(key, value, RESOURCE_CONFIG);
siteRouter.mountRouter(taxonomyRouter.router());

registry.setRouter(taxonomyRouter.identifier, taxonomyRouter);
});

_.each(dynamicRoutes.collections, (value, key) => {
const collectionRouter = new CollectionRouter(key, value);
const collectionRouter = new CollectionRouter(key, value, RESOURCE_CONFIG);
siteRouter.mountRouter(collectionRouter.router());
registry.setRouter(collectionRouter.identifier, collectionRouter);
});

const staticPagesRouter = new StaticPagesRouter();
const staticPagesRouter = new StaticPagesRouter(RESOURCE_CONFIG);
siteRouter.mountRouter(staticPagesRouter.router());

registry.setRouter('staticPagesRouter', staticPagesRouter);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable */
module.exports.QUERY = {
tag: {
alias: 'tags',
controller: 'tags',
type: 'read',
resource: 'tags',
options: {
Expand All @@ -10,7 +10,8 @@ module.exports.QUERY = {
}
},
author: {
alias: 'authors',
resourceAlias: 'authors',
controller: 'users',
type: 'read',
resource: 'users',
options: {
Expand All @@ -19,7 +20,8 @@ module.exports.QUERY = {
}
},
user: {
alias: 'authors',
resourceAlias: 'authors',
controller: 'users',
type: 'read',
resource: 'users',
options: {
Expand All @@ -28,7 +30,7 @@ module.exports.QUERY = {
}
},
post: {
alias: 'posts',
controller: 'posts',
type: 'read',
resource: 'posts',
options: {
Expand All @@ -38,25 +40,31 @@ module.exports.QUERY = {
}
},
page: {
alias: 'pages',
controller: 'posts',
type: 'read',
resource: 'posts',
options: {
slug: '%s',
status: 'published',
page: 1
}
},
preview: {
controller: 'posts',
resource: 'posts'
}
};

module.exports.TAXONOMIES = {
tag: {
filter: 'tags:\'%s\'+tags.visibility:public',
editRedirect: '#/settings/tags/:slug/'
editRedirect: '#/settings/tags/:slug/',
resource: 'tags'
},
author: {
filter: 'authors:\'%s\'',
editRedirect: '#/team/:slug/'
editRedirect: '#/team/:slug/',
resource: 'authors'
}
};
/* eslint-enable */
54 changes: 54 additions & 0 deletions core/server/services/routing/config/v2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* eslint-disable */
module.exports.QUERY = {
tag: {
controller: 'tagsPublic',
type: 'read',
resource: 'tags',
options: {
slug: '%s',
visibility: 'public'
}
},
author: {
controller: 'authors',
type: 'read',
resource: 'authors',
options: {
slug: '%s'
}
},
post: {
controller: 'posts',
type: 'read',
resource: 'posts',
options: {
slug: '%s'
}
},
page: {
controller: 'pages',
type: 'read',
resource: 'pages',
options: {
slug: '%s'
}
},
preview: {
controller: 'preview',
resource: 'preview'
}
};

module.exports.TAXONOMIES = {
tag: {
filter: 'tags:\'%s\'+tags.visibility:public',
editRedirect: '#/settings/tags/:slug/',
resource: 'tags'
},
author: {
filter: 'authors:\'%s\'',
editRedirect: '#/team/:slug/',
resource: 'authors'
}
};
/* eslint-enable */
4 changes: 2 additions & 2 deletions core/server/services/routing/controllers/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ module.exports = function previewController(req, res, next) {
include: 'author,authors,tags'
};

(api[res.routerOptions.query.alias] || api[res.routerOptions.query.resource])
api[res.routerOptions.query.controller]
.read(params)
.then(function then(result) {
const post = (result[res.routerOptions.query.alias] || result[res.routerOptions.query.resource])[0];
const post = result[res.routerOptions.query.resource][0];

if (!post) {
return next();
Expand Down
4 changes: 2 additions & 2 deletions core/server/services/routing/controllers/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function processQuery(query, locals) {
}

// Return a promise for the api query
return (api[query.alias] || api[query.resource])[query.type](query.options);
return api[query.controller][query.type](query.options);
}

module.exports = function staticController(req, res, next) {
Expand All @@ -41,7 +41,7 @@ module.exports = function staticController(req, res, next) {
if (config.type === 'browse') {
response.data[name] = result[name];
} else {
response.data[name] = result[name][config.alias] || result[name][config.resource];
response.data[name] = result[name][config.resource];
}
});
}
Expand Down
5 changes: 2 additions & 3 deletions core/server/services/routing/helpers/entry-lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ function entryLookup(postUrl, routerOptions, locals) {
const api = require('../../../api')[locals.apiVersion];
const targetPath = url.parse(postUrl).path;
const permalinks = routerOptions.permalinks;

let isEditURL = false;

// CASE: e.g. /:slug/ -> { slug: 'value' }
Expand All @@ -36,10 +35,10 @@ function entryLookup(postUrl, routerOptions, locals) {
* Query database to find entry.
* @deprecated: `author`, will be removed in Ghost 3.0
*/
return (api[routerOptions.query.alias] || api[routerOptions.query.resource])
return api[routerOptions.query.controller]
.read(_.extend(_.pick(params, 'slug', 'id'), {include: 'author,authors,tags'}))
.then(function then(result) {
const entry = (result[routerOptions.query.alias] || result[routerOptions.query.resource])[0];
const entry = result[routerOptions.query.resource][0];

if (!entry) {
return Promise.resolve();
Expand Down
Loading

0 comments on commit d051050

Please sign in to comment.