0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Refactored routing to be passed routes config

- At the moment the bootstrap.start method asks the settings service for its settings
- This couples the routing and settings services together - when maybe we want to use a different method to generate settings
- By passing the settings to the routing service at the right time, we open up possibilities for refactoring
This commit is contained in:
Hannah Wolfe 2021-06-28 12:24:16 +01:00
parent 441b12d62c
commit e410e16a5b
No known key found for this signature in database
GPG key ID: 9F8C7532D0A6BA55
2 changed files with 14 additions and 12 deletions

View file

@ -145,10 +145,11 @@ async function initDynamicRouting() {
debug('Begin: Dynamic Routing');
const routing = require('./frontend/services/routing');
const bridge = require('./bridge');
// We pass the frontend API version here, so that the frontend services are slightly less tightly-coupled
routing.bootstrap.start(bridge.getFrontendApiVersion());
// We pass the frontend API version + the dynamic routes here, so that the frontend services are slightly less tightly-coupled
const settings = require('./server/services/settings');
const frontendSettings = require('./frontend/services/settings');
const dynamicRoutes = frontendSettings.get('routes');
routing.bootstrap.start(bridge.getFrontendApiVersion(), dynamicRoutes);
const getRoutesHash = () => frontendSettings.getCurrentHash('routes');
await settings.syncRoutesHash(getRoutesHash);
debug('End: Dynamic Routing');

View file

@ -1,7 +1,7 @@
const debug = require('@tryghost/debug')('services:routing:bootstrap');
const _ = require('lodash');
const events = require('../../../server/lib/common/events');
const settingsService = require('../settings');
const frontendSettings = require('../settings');
const StaticRoutesRouter = require('./StaticRoutesRouter');
const StaticPagesRouter = require('./StaticPagesRouter');
const CollectionRouter = require('./CollectionRouter');
@ -22,8 +22,7 @@ let siteRouter;
* CASES:
* - if Ghost starts, it will first init the site app with the wrapper router and then call `start`
* separately, because it could be that your blog goes into maintenance mode
* - if you upload your routes.yaml in the admin client, we will re-initialise routing
* -
* - if you change your route settings, we will re-initialise routing
*
* @param {Object} options
* @returns {ExpressRouter}
@ -41,14 +40,16 @@ module.exports.init = (options = {start: false}) => {
if (options.start) {
let apiVersion = _.isBoolean(options.start) ? defaultApiVersion : options.start;
this.start(apiVersion);
// NOTE: Get the routes.yaml config
const dynamicRoutes = frontendSettings.get('routes');
this.start(apiVersion, dynamicRoutes);
}
return siteRouter.router();
};
/**
* @description This function will create the routers based on the routes.yaml config.
* @description This function will create the routers based on the route settings
*
* The routers are created in a specific order. This order defines who can get a resource first or
* who can dominant other routers.
@ -59,8 +60,11 @@ module.exports.init = (options = {start: false}) => {
* 4. Collections
* 5. Static Pages: Weaker than collections, because we first try to find a post slug and fallback to lookup a static page.
* 6. Internal Apps: Weakest
*
* @param {string} apiVersion
* @param {object} dynamicRoutes
*/
module.exports.start = (apiVersion) => {
module.exports.start = (apiVersion, dynamicRoutes) => {
const RESOURCE_CONFIG = require(`./config/${apiVersion}`);
const unsubscribeRouter = new UnsubscribeRouter();
@ -71,11 +75,8 @@ module.exports.start = (apiVersion) => {
siteRouter.mountRouter(previewRouter.router());
registry.setRouter('previewRouter', previewRouter);
// NOTE: Get the routes.yaml config
const dynamicRoutes = settingsService.get('routes');
_.each(dynamicRoutes.routes, (value, key) => {
const staticRoutesRouter = new StaticRoutesRouter(key, value, RESOURCE_CONFIG);
const staticRoutesRouter = new StaticRoutesRouter(key, value);
siteRouter.mountRouter(staticRoutesRouter.router());
registry.setRouter(staticRoutesRouter.identifier, staticRoutesRouter);