mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -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:
parent
441b12d62c
commit
e410e16a5b
2 changed files with 14 additions and 12 deletions
|
@ -145,10 +145,11 @@ async function initDynamicRouting() {
|
||||||
debug('Begin: Dynamic Routing');
|
debug('Begin: Dynamic Routing');
|
||||||
const routing = require('./frontend/services/routing');
|
const routing = require('./frontend/services/routing');
|
||||||
const bridge = require('./bridge');
|
const bridge = require('./bridge');
|
||||||
// We pass the frontend API version here, so that the frontend services are slightly less tightly-coupled
|
// We pass the frontend API version + the dynamic routes here, so that the frontend services are slightly less tightly-coupled
|
||||||
routing.bootstrap.start(bridge.getFrontendApiVersion());
|
|
||||||
const settings = require('./server/services/settings');
|
const settings = require('./server/services/settings');
|
||||||
const frontendSettings = require('./frontend/services/settings');
|
const frontendSettings = require('./frontend/services/settings');
|
||||||
|
const dynamicRoutes = frontendSettings.get('routes');
|
||||||
|
routing.bootstrap.start(bridge.getFrontendApiVersion(), dynamicRoutes);
|
||||||
const getRoutesHash = () => frontendSettings.getCurrentHash('routes');
|
const getRoutesHash = () => frontendSettings.getCurrentHash('routes');
|
||||||
await settings.syncRoutesHash(getRoutesHash);
|
await settings.syncRoutesHash(getRoutesHash);
|
||||||
debug('End: Dynamic Routing');
|
debug('End: Dynamic Routing');
|
||||||
|
|
21
core/frontend/services/routing/bootstrap.js
vendored
21
core/frontend/services/routing/bootstrap.js
vendored
|
@ -1,7 +1,7 @@
|
||||||
const debug = require('@tryghost/debug')('services:routing:bootstrap');
|
const debug = require('@tryghost/debug')('services:routing:bootstrap');
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const events = require('../../../server/lib/common/events');
|
const events = require('../../../server/lib/common/events');
|
||||||
const settingsService = require('../settings');
|
const frontendSettings = require('../settings');
|
||||||
const StaticRoutesRouter = require('./StaticRoutesRouter');
|
const StaticRoutesRouter = require('./StaticRoutesRouter');
|
||||||
const StaticPagesRouter = require('./StaticPagesRouter');
|
const StaticPagesRouter = require('./StaticPagesRouter');
|
||||||
const CollectionRouter = require('./CollectionRouter');
|
const CollectionRouter = require('./CollectionRouter');
|
||||||
|
@ -22,8 +22,7 @@ let siteRouter;
|
||||||
* CASES:
|
* CASES:
|
||||||
* - if Ghost starts, it will first init the site app with the wrapper router and then call `start`
|
* - 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
|
* 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
|
* @param {Object} options
|
||||||
* @returns {ExpressRouter}
|
* @returns {ExpressRouter}
|
||||||
|
@ -41,14 +40,16 @@ module.exports.init = (options = {start: false}) => {
|
||||||
|
|
||||||
if (options.start) {
|
if (options.start) {
|
||||||
let apiVersion = _.isBoolean(options.start) ? defaultApiVersion : 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();
|
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
|
* The routers are created in a specific order. This order defines who can get a resource first or
|
||||||
* who can dominant other routers.
|
* who can dominant other routers.
|
||||||
|
@ -59,8 +60,11 @@ module.exports.init = (options = {start: false}) => {
|
||||||
* 4. Collections
|
* 4. Collections
|
||||||
* 5. Static Pages: Weaker than collections, because we first try to find a post slug and fallback to lookup a static page.
|
* 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
|
* 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 RESOURCE_CONFIG = require(`./config/${apiVersion}`);
|
||||||
|
|
||||||
const unsubscribeRouter = new UnsubscribeRouter();
|
const unsubscribeRouter = new UnsubscribeRouter();
|
||||||
|
@ -71,11 +75,8 @@ module.exports.start = (apiVersion) => {
|
||||||
siteRouter.mountRouter(previewRouter.router());
|
siteRouter.mountRouter(previewRouter.router());
|
||||||
registry.setRouter('previewRouter', previewRouter);
|
registry.setRouter('previewRouter', previewRouter);
|
||||||
|
|
||||||
// NOTE: Get the routes.yaml config
|
|
||||||
const dynamicRoutes = settingsService.get('routes');
|
|
||||||
|
|
||||||
_.each(dynamicRoutes.routes, (value, key) => {
|
_.each(dynamicRoutes.routes, (value, key) => {
|
||||||
const staticRoutesRouter = new StaticRoutesRouter(key, value, RESOURCE_CONFIG);
|
const staticRoutesRouter = new StaticRoutesRouter(key, value);
|
||||||
siteRouter.mountRouter(staticRoutesRouter.router());
|
siteRouter.mountRouter(staticRoutesRouter.router());
|
||||||
|
|
||||||
registry.setRouter(staticRoutesRouter.identifier, staticRoutesRouter);
|
registry.setRouter(staticRoutesRouter.identifier, staticRoutesRouter);
|
||||||
|
|
Loading…
Add table
Reference in a new issue