Revert "Refactored routing to be passed routes config"

This reverts commit 8d754a592e.
This commit is contained in:
Hannah Wolfe 2021-06-30 10:52:11 +01:00
parent d196d9b525
commit 4ef2ae4436
No known key found for this signature in database
GPG Key ID: 9F8C7532D0A6BA55
3 changed files with 14 additions and 16 deletions

View File

@ -145,11 +145,10 @@ async function initDynamicRouting() {
debug('Begin: Dynamic Routing');
const routing = require('./frontend/services/routing');
const bridge = require('./bridge');
// We pass the frontend API version + the dynamic routes here, so that the frontend services are slightly less tightly-coupled
// We pass the frontend API version here, so that the frontend services are slightly less tightly-coupled
routing.bootstrap.start(bridge.getFrontendApiVersion());
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 frontendSettings = require('../settings');
const settingsService = require('../settings');
const StaticRoutesRouter = require('./StaticRoutesRouter');
const StaticPagesRouter = require('./StaticPagesRouter');
const CollectionRouter = require('./CollectionRouter');
@ -22,7 +22,8 @@ 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 change your route settings, we will re-initialise routing
* - if you upload your routes.yaml in the admin client, we will re-initialise routing
* -
*
* @param {Object} options
* @returns {ExpressRouter}
@ -40,16 +41,14 @@ module.exports.init = (options = {start: false}) => {
if (options.start) {
let apiVersion = _.isBoolean(options.start) ? defaultApiVersion : options.start;
// NOTE: Get the routes.yaml config
const dynamicRoutes = frontendSettings.get('routes');
this.start(apiVersion, dynamicRoutes);
this.start(apiVersion);
}
return siteRouter.router();
};
/**
* @description This function will create the routers based on the route settings
* @description This function will create the routers based on the routes.yaml config.
*
* The routers are created in a specific order. This order defines who can get a resource first or
* who can dominant other routers.
@ -60,11 +59,8 @@ 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, dynamicRoutes) => {
module.exports.start = (apiVersion) => {
const RESOURCE_CONFIG = require(`./config/${apiVersion}`);
const unsubscribeRouter = new UnsubscribeRouter();
@ -75,8 +71,11 @@ module.exports.start = (apiVersion, dynamicRoutes) => {
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);
const staticRoutesRouter = new StaticRoutesRouter(key, value, RESOURCE_CONFIG);
siteRouter.mountRouter(staticRoutesRouter.router());
registry.setRouter(staticRoutesRouter.identifier, staticRoutesRouter);

View File

@ -313,7 +313,7 @@ module.exports = {
method: 'edit'
},
async query(frame) {
await routeSettings.setFromFilePath(frame.file.path);
await routeSettings.settings.setFromFilePath(frame.file.path);
const getRoutesHash = () => frontendSettings.getCurrentHash('routes');
await settingsService.syncRoutesHash(getRoutesHash);
}
@ -333,7 +333,7 @@ module.exports = {
method: 'browse'
},
query() {
return routeSettings.get();
return routeSettings.settings.get();
}
}
};