Refactored bootstrap.init to require route settings

- The router bootstrap is no longer allowed to fetch it's own settings, but rather is passed them
- This moves the call to the site routes.js file, which isn't much better but it's a start
- The goal is to always pass these in from the boot process, or from the bridge reloader
This commit is contained in:
Hannah Wolfe 2021-07-06 11:38:52 +01:00
parent bab5764179
commit e7b80e50dc
No known key found for this signature in database
GPG Key ID: 9F8C7532D0A6BA55
5 changed files with 26 additions and 20 deletions

View File

@ -144,14 +144,20 @@ async function initExpressApps() {
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
const settings = require('./server/services/settings');
const frontendSettings = require('./frontend/services/settings');
const dynamicRoutes = frontendSettings.get('routes');
routing.bootstrap.start(bridge.getFrontendApiVersion(), dynamicRoutes);
const bridge = require('./bridge');
// We pass the frontend API version + the dynamic routes here, so that the frontend services are slightly less tightly-coupled
const apiVersion = bridge.getFrontendApiVersion();
const routeSettings = frontendSettings.get('routes');
debug(`Frontend API Version: ${apiVersion}`);
routing.bootstrap.start(apiVersion, routeSettings);
const getRoutesHash = () => frontendSettings.getCurrentHash('routes');
const settings = require('./server/services/settings');
await settings.syncRoutesHash(getRoutesHash);
debug('End: Dynamic Routing');
}

View File

@ -29,6 +29,7 @@ class Bridge {
* @deprecated: the term "lang" was deprecated in favor of "locale" publicly in 4.0
*/
events.on('settings.lang.edited', (model) => {
debug('Active theme init18n');
this.getActiveTheme().initI18n({locale: model.get('value')});
});
}

View File

@ -1,7 +1,6 @@
const debug = require('@tryghost/debug')('routing');
const _ = require('lodash');
const events = require('../../../server/lib/common/events');
const frontendSettings = require('../settings');
const StaticRoutesRouter = require('./StaticRoutesRouter');
const StaticPagesRouter = require('./StaticPagesRouter');
const CollectionRouter = require('./CollectionRouter');
@ -27,8 +26,8 @@ let siteRouter;
* @param {Object} options
* @returns {ExpressRouter}
*/
module.exports.init = (options = {start: false}) => {
debug('bootstrap init', options);
module.exports.init = ({start = false, routerSettings, apiVersion}) => {
debug('bootstrap init', start, apiVersion, routerSettings);
registry.resetAllRouters();
registry.resetAllRoutes();
@ -38,11 +37,9 @@ module.exports.init = (options = {start: false}) => {
siteRouter = new ParentRouter('SiteRouter');
registry.setRouter('siteRouter', siteRouter);
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);
if (start) {
apiVersion = apiVersion || defaultApiVersion;
this.start(apiVersion, routerSettings);
}
return siteRouter.router();
@ -62,10 +59,10 @@ module.exports.init = (options = {start: false}) => {
* 6. Internal Apps: Weakest
*
* @param {string} apiVersion
* @param {object} dynamicRoutes
* @param {object} routerSettings
*/
module.exports.start = (apiVersion, dynamicRoutes) => {
debug('bootstrap start', apiVersion, dynamicRoutes);
module.exports.start = (apiVersion, routerSettings) => {
debug('bootstrap start', apiVersion, routerSettings);
const RESOURCE_CONFIG = require(`./config/${apiVersion}`);
const unsubscribeRouter = new UnsubscribeRouter();
@ -76,14 +73,14 @@ module.exports.start = (apiVersion, dynamicRoutes) => {
siteRouter.mountRouter(previewRouter.router());
registry.setRouter('previewRouter', previewRouter);
_.each(dynamicRoutes.routes, (value, key) => {
_.each(routerSettings.routes, (value, key) => {
const staticRoutesRouter = new StaticRoutesRouter(key, value);
siteRouter.mountRouter(staticRoutesRouter.router());
registry.setRouter(staticRoutesRouter.identifier, staticRoutesRouter);
});
_.each(dynamicRoutes.collections, (value, key) => {
_.each(routerSettings.collections, (value, key) => {
const collectionRouter = new CollectionRouter(key, value, RESOURCE_CONFIG);
siteRouter.mountRouter(collectionRouter.router());
registry.setRouter(collectionRouter.identifier, collectionRouter);
@ -94,7 +91,7 @@ module.exports.start = (apiVersion, dynamicRoutes) => {
registry.setRouter('staticPagesRouter', staticPagesRouter);
_.each(dynamicRoutes.taxonomies, (value, key) => {
_.each(routerSettings.taxonomies, (value, key) => {
const taxonomyRouter = new TaxonomyRouter(key, value, RESOURCE_CONFIG);
siteRouter.mountRouter(taxonomyRouter.router());

View File

@ -191,7 +191,7 @@ module.exports = function setupSiteApp(options = {}) {
module.exports.reload = ({apiVersion}) => {
// https://github.com/expressjs/express/issues/2596
router = siteRoutes({start: apiVersion});
router = siteRoutes({start: true, apiVersion});
Object.setPrototypeOf(SiteRouter, router);
// re-initialse apps (register app routers, because we have re-initialised the site routers)

View File

@ -1,7 +1,9 @@
const debug = require('@tryghost/debug')('routing');
const routing = require('../../../frontend/services/routing');
const frontendSettings = require('../../../frontend/services/settings');
module.exports = function siteRoutes(options = {}) {
debug('site Routes', options);
options.routerSettings = frontendSettings.get('routes');
return routing.bootstrap.init(options);
};