Renamed bootstrap to routerManager

refs https://linear.app/tryghost/issue/CORE-104/decouple-frontend-routing-events-from-urlserver-events

- A follow up rename after bootstrap module was transformed into class
This commit is contained in:
Naz 2021-10-18 22:01:38 +04:00 committed by naz
parent 979474a8cc
commit 51b78211c5
14 changed files with 33 additions and 33 deletions

View File

@ -158,7 +158,7 @@ async function initExpressApps() {
/** /**
* Dynamic routing is generated from the routes.yaml file * Dynamic routing is generated from the routes.yaml file
* When Ghost's DB and core are loaded, we can access this file and call routing.bootstrap.start * When Ghost's DB and core are loaded, we can access this file and call routing.routingManager.start
* However this _must_ happen after the express Apps are loaded, hence why this is here and not in initFrontend * However this _must_ happen after the express Apps are loaded, hence why this is here and not in initFrontend
* Routing is currently tightly coupled between the frontend and backend * Routing is currently tightly coupled between the frontend and backend
*/ */
@ -173,7 +173,7 @@ async function initDynamicRouting() {
const routeSettings = await routeSettingsService.loadRouteSettings(); const routeSettings = await routeSettingsService.loadRouteSettings();
debug(`Frontend API Version: ${apiVersion}`); debug(`Frontend API Version: ${apiVersion}`);
routing.bootstrap.start(apiVersion, routeSettings); routing.routerManager.start(apiVersion, routeSettings);
const getRoutesHash = () => routeSettingsService.api.getCurrentHash(); const getRoutesHash = () => routeSettingsService.api.getCurrentHash();
const settings = require('./server/services/settings'); const settings = require('./server/services/settings');

View File

@ -16,7 +16,7 @@ const config = require('./shared/config');
const logging = require('@tryghost/logging'); const logging = require('@tryghost/logging');
const tpl = require('@tryghost/tpl'); const tpl = require('@tryghost/tpl');
const themeEngine = require('./frontend/services/theme-engine'); const themeEngine = require('./frontend/services/theme-engine');
const frontendRouting = require('./frontend/services/routing').bootstrap; const routerManager = require('./frontend/services/routing').routerManager;
const settingsCache = require('./shared/settings-cache'); const settingsCache = require('./shared/settings-cache');
// Listen to settings.lang.edited, similar to the member service and models/base/listeners // Listen to settings.lang.edited, similar to the member service and models/base/listeners
@ -41,7 +41,7 @@ class Bridge {
// for now this eliminates the need for the frontend routing to listen to // for now this eliminates the need for the frontend routing to listen to
// server events // server events
events.on('settings.timezone.edited', (model) => { events.on('settings.timezone.edited', (model) => {
frontendRouting.handleTimezoneEdit(model); routerManager.handleTimezoneEdit(model);
}); });
} }

View File

@ -3,7 +3,7 @@ const debug = require('@tryghost/debug')('services:routing:controllers:collectio
const tpl = require('@tryghost/tpl'); const tpl = require('@tryghost/tpl');
const errors = require('@tryghost/errors'); const errors = require('@tryghost/errors');
const security = require('@tryghost/security'); const security = require('@tryghost/security');
const {bootstrap} = require('../'); const {routerManager} = require('../');
const themeEngine = require('../../theme-engine'); const themeEngine = require('../../theme-engine');
const helpers = require('../helpers'); const helpers = require('../helpers');
@ -72,7 +72,7 @@ module.exports = function collectionController(req, res, next) {
* People should always invert their filters to ensure that the database query loads unique posts per collection. * People should always invert their filters to ensure that the database query loads unique posts per collection.
*/ */
result.posts = _.filter(result.posts, (post) => { result.posts = _.filter(result.posts, (post) => {
if (bootstrap.owns(res.routerOptions.identifier, post.id)) { if (routerManager.owns(res.routerOptions.identifier, post.id)) {
return post; return post;
} }

View File

@ -1,6 +1,6 @@
const debug = require('@tryghost/debug')('services:routing:controllers:emailpost'); const debug = require('@tryghost/debug')('services:routing:controllers:emailpost');
const config = require('../../../../shared/config'); const config = require('../../../../shared/config');
const {bootstrap} = require('../'); const {routerManager} = require('../');
const urlUtils = require('../../../../shared/url-utils'); const urlUtils = require('../../../../shared/url-utils');
const helpers = require('../helpers'); const helpers = require('../helpers');
@ -48,7 +48,7 @@ module.exports = function emailPostController(req, res, next) {
} }
if (post.status === 'published') { if (post.status === 'published') {
return urlUtils.redirect301(res, bootstrap.getUrlByResourceId(post.id, {withSubdirectory: true})); return urlUtils.redirect301(res, routerManager.getUrlByResourceId(post.id, {withSubdirectory: true}));
} }
if (res.locals.apiVersion !== 'v0.1' && res.locals.apiVersion !== 'v2') { if (res.locals.apiVersion !== 'v0.1' && res.locals.apiVersion !== 'v2') {

View File

@ -1,7 +1,7 @@
const debug = require('@tryghost/debug')('services:routing:controllers:entry'); const debug = require('@tryghost/debug')('services:routing:controllers:entry');
const url = require('url'); const url = require('url');
const config = require('../../../../shared/config'); const config = require('../../../../shared/config');
const {bootstrap} = require('../'); const {routerManager} = require('../');
const urlUtils = require('../../../../shared/url-utils'); const urlUtils = require('../../../../shared/url-utils');
const helpers = require('../helpers'); const helpers = require('../helpers');
@ -60,7 +60,7 @@ module.exports = function entryController(req, res, next) {
* *
* That's why we have to check against the router type. * That's why we have to check against the router type.
*/ */
if (bootstrap.getResourceById(entry.id).config.type !== res.routerOptions.resourceType) { if (routerManager.getResourceById(entry.id).config.type !== res.routerOptions.resourceType) {
debug('not my resource type'); debug('not my resource type');
return next(); return next();
} }

View File

@ -1,6 +1,6 @@
const debug = require('@tryghost/debug')('services:routing:controllers:preview'); const debug = require('@tryghost/debug')('services:routing:controllers:preview');
const config = require('../../../../shared/config'); const config = require('../../../../shared/config');
const {bootstrap} = require('../'); const {routerManager} = require('../');
const urlUtils = require('../../../../shared/url-utils'); const urlUtils = require('../../../../shared/url-utils');
const helpers = require('../helpers'); const helpers = require('../helpers');
@ -50,7 +50,7 @@ module.exports = function previewController(req, res, next) {
} }
if (post.status === 'published') { if (post.status === 'published') {
return urlUtils.redirect301(res, bootstrap.getUrlByResourceId(post.id, {withSubdirectory: true})); return urlUtils.redirect301(res, routerManager.getUrlByResourceId(post.id, {withSubdirectory: true}));
} }
if (res.locals.apiVersion !== 'v0.1' && res.locals.apiVersion !== 'v2') { if (res.locals.apiVersion !== 'v0.1' && res.locals.apiVersion !== 'v2') {

View File

@ -1,9 +1,9 @@
const registry = require('./registry'); const registry = require('./registry');
const RouterManager = require('./bootstrap'); const RouterManager = require('./router-manager');
const routerManager = new RouterManager({registry}); const routerManager = new RouterManager({registry});
module.exports = { module.exports = {
bootstrap: routerManager, routerManager: routerManager,
get registry() { get registry() {
return require('./registry'); return require('./registry');

View File

@ -64,7 +64,7 @@ class RouterManager {
*/ */
init({start = false, routerSettings, apiVersion, urlService}) { init({start = false, routerSettings, apiVersion, urlService}) {
this.urlService = urlService; this.urlService = urlService;
debug('bootstrap init', start, apiVersion, routerSettings); debug('routing init', start, apiVersion, routerSettings);
this.registry.resetAllRouters(); this.registry.resetAllRouters();
this.registry.resetAllRoutes(); this.registry.resetAllRoutes();
@ -101,7 +101,7 @@ class RouterManager {
* @param {object} routerSettings * @param {object} routerSettings
*/ */
start(apiVersion, routerSettings) { start(apiVersion, routerSettings) {
debug('bootstrap start', apiVersion, routerSettings); debug('routing start', apiVersion, routerSettings);
const RESOURCE_CONFIG = require(`./config/${apiVersion}`); const RESOURCE_CONFIG = require(`./config/${apiVersion}`);
const unsubscribeRouter = new UnsubscribeRouter(); const unsubscribeRouter = new UnsubscribeRouter();

View File

@ -3,7 +3,7 @@ const Promise = require('bluebird');
const cheerio = require('cheerio'); const cheerio = require('cheerio');
const RSS = require('rss'); const RSS = require('rss');
const urlUtils = require('../../../shared/url-utils'); const urlUtils = require('../../../shared/url-utils');
const {bootstrap} = require('../routing'); const {routerManager} = require('../routing');
const generateTags = function generateTags(data) { const generateTags = function generateTags(data) {
if (data.tags) { if (data.tags) {
@ -19,7 +19,7 @@ const generateTags = function generateTags(data) {
}; };
const generateItem = function generateItem(post, secure) { const generateItem = function generateItem(post, secure) {
const itemUrl = bootstrap.getUrlByResourceId(post.id, {secure, absolute: true}); const itemUrl = routerManager.getUrlByResourceId(post.id, {secure, absolute: true});
const htmlContent = cheerio.load(post.html || ''); const htmlContent = cheerio.load(post.html || '');
const item = { const item = {
title: post.title, title: post.title,

View File

@ -7,5 +7,5 @@ module.exports = function siteRoutes(options = {}) {
debug('site Routes', options); debug('site Routes', options);
options.routerSettings = routeSettings.loadRouteSettingsSync(); options.routerSettings = routeSettings.loadRouteSettingsSync();
options.urlService = urlService; options.urlService = urlService;
return routing.bootstrap.init(options); return routing.routerManager.init(options);
}; };

View File

@ -1,12 +1,12 @@
const should = require('should'); const should = require('should');
const sinon = require('sinon'); const sinon = require('sinon');
const CollectionRouter = require('../../../../../core/frontend/services/routing/CollectionRouter'); const CollectionRouter = require('../../../../../core/frontend/services/routing/CollectionRouter');
const RouterManager = require('../../../../../core/frontend/services/routing/bootstrap'); const RouterManager = require('../../../../../core/frontend/services/routing/router-manager');
const registry = require('../../../../../core/frontend/services/routing/registry'); const registry = require('../../../../../core/frontend/services/routing/registry');
const RESOURCE_CONFIG = {QUERY: {post: {controller: 'posts', resource: 'posts'}}}; const RESOURCE_CONFIG = {QUERY: {post: {controller: 'posts', resource: 'posts'}}};
describe('UNIT: services/routing/bootstrap', function () { describe('UNIT: services/routing/router-manager', function () {
let routerUpdatedSpy; let routerUpdatedSpy;
let routerCreatedSpy; let routerCreatedSpy;

View File

@ -4,7 +4,7 @@ const sinon = require('sinon');
const testUtils = require('../../../../../utils'); const testUtils = require('../../../../../utils');
const security = require('@tryghost/security'); const security = require('@tryghost/security');
const themeEngine = require('../../../../../../core/frontend/services/theme-engine'); const themeEngine = require('../../../../../../core/frontend/services/theme-engine');
const bootstrap = require('../../../../../../core/frontend/services/routing/').bootstrap; const routerManager = require('../../../../../../core/frontend/services/routing/').routerManager;
const controllers = require('../../../../../../core/frontend/services/routing/controllers'); const controllers = require('../../../../../../core/frontend/services/routing/controllers');
const helpers = require('../../../../../../core/frontend/services/routing/helpers'); const helpers = require('../../../../../../core/frontend/services/routing/helpers');
@ -56,7 +56,7 @@ describe('Unit - services/routing/controllers/collection', function () {
sinon.stub(helpers, 'renderEntries').returns(renderStub); sinon.stub(helpers, 'renderEntries').returns(renderStub);
ownsStub = sinon.stub(bootstrap, 'owns'); ownsStub = sinon.stub(routerManager, 'owns');
ownsStub.withArgs('identifier', posts[0].id).returns(true); ownsStub.withArgs('identifier', posts[0].id).returns(true);
req = { req = {

View File

@ -3,7 +3,7 @@ const sinon = require('sinon');
const testUtils = require('../../../../../utils'); const testUtils = require('../../../../../utils');
const configUtils = require('../../../../../utils/configUtils'); const configUtils = require('../../../../../utils/configUtils');
const urlUtils = require('../../../../../../core/shared/url-utils'); const urlUtils = require('../../../../../../core/shared/url-utils');
const bootstrap = require('../../../../../../core/frontend/services/routing/').bootstrap; const routerManager = require('../../../../../../core/frontend/services/routing/').routerManager;
const controllers = require('../../../../../../core/frontend/services/routing/controllers'); const controllers = require('../../../../../../core/frontend/services/routing/controllers');
const helpers = require('../../../../../../core/frontend/services/routing/helpers'); const helpers = require('../../../../../../core/frontend/services/routing/helpers');
const EDITOR_URL = `/#/editor/post/`; const EDITOR_URL = `/#/editor/post/`;
@ -41,7 +41,7 @@ describe('Unit - services/routing/controllers/entry', function () {
sinon.stub(urlUtils, 'redirectToAdmin'); sinon.stub(urlUtils, 'redirectToAdmin');
sinon.stub(urlUtils, 'redirect301'); sinon.stub(urlUtils, 'redirect301');
sinon.stub(bootstrap, 'getResourceById'); sinon.stub(routerManager, 'getResourceById');
req = { req = {
path: '/', path: '/',
@ -81,7 +81,7 @@ describe('Unit - services/routing/controllers/entry', function () {
res.routerOptions.resourceType = 'posts'; res.routerOptions.resourceType = 'posts';
bootstrap.getResourceById.withArgs(post.id).returns({ routerManager.getResourceById.withArgs(post.id).returns({
config: { config: {
type: 'posts' type: 'posts'
} }
@ -162,7 +162,7 @@ describe('Unit - services/routing/controllers/entry', function () {
req.path = post.url; req.path = post.url;
res.routerOptions.resourceType = 'posts'; res.routerOptions.resourceType = 'posts';
bootstrap.getResourceById.withArgs(post.id).returns({ routerManager.getResourceById.withArgs(post.id).returns({
config: { config: {
type: 'pages' type: 'pages'
} }
@ -186,7 +186,7 @@ describe('Unit - services/routing/controllers/entry', function () {
res.routerOptions.resourceType = 'posts'; res.routerOptions.resourceType = 'posts';
bootstrap.getResourceById.withArgs(post.id).returns({ routerManager.getResourceById.withArgs(post.id).returns({
config: { config: {
type: 'posts' type: 'posts'
} }
@ -215,7 +215,7 @@ describe('Unit - services/routing/controllers/entry', function () {
res.routerOptions.resourceType = 'posts'; res.routerOptions.resourceType = 'posts';
bootstrap.getResourceById.withArgs(post.id).returns({ routerManager.getResourceById.withArgs(post.id).returns({
config: { config: {
type: 'posts' type: 'posts'
} }

View File

@ -3,7 +3,7 @@ const sinon = require('sinon');
const _ = require('lodash'); const _ = require('lodash');
const testUtils = require('../../../../utils'); const testUtils = require('../../../../utils');
const configUtils = require('../../../../utils/configUtils'); const configUtils = require('../../../../utils/configUtils');
const bootstrap = require('../../../../../core/frontend/services/routing').bootstrap; const routerManager = require('../../../../../core/frontend/services/routing').routerManager;
const generateFeed = require('../../../../../core/frontend/services/rss/generate-feed'); const generateFeed = require('../../../../../core/frontend/services/rss/generate-feed');
describe('RSS: Generate Feed', function () { describe('RSS: Generate Feed', function () {
@ -43,7 +43,7 @@ describe('RSS: Generate Feed', function () {
}); });
beforeEach(function () { beforeEach(function () {
sinon.stub(bootstrap, 'getUrlByResourceId'); sinon.stub(routerManager, 'getUrlByResourceId');
baseUrl = '/rss/'; baseUrl = '/rss/';
@ -92,7 +92,7 @@ describe('RSS: Generate Feed', function () {
data.posts = posts; data.posts = posts;
_.each(data.posts, function (post) { _.each(data.posts, function (post) {
bootstrap.getUrlByResourceId.withArgs(post.id, {secure: undefined, absolute: true}).returns('http://my-ghost-blog.com/' + post.slug + '/'); routerManager.getUrlByResourceId.withArgs(post.id, {secure: undefined, absolute: true}).returns('http://my-ghost-blog.com/' + post.slug + '/');
}); });
generateFeed(baseUrl, data).then(function (xmlData) { generateFeed(baseUrl, data).then(function (xmlData) {
@ -204,7 +204,7 @@ describe('RSS: Generate Feed', function () {
data.posts = [posts[0]]; data.posts = [posts[0]];
_.each(data.posts, function (post) { _.each(data.posts, function (post) {
bootstrap.getUrlByResourceId.withArgs(post.id, {secure: undefined, absolute: true}).returns('http://my-ghost-blog.com/' + post.slug + '/'); routerManager.getUrlByResourceId.withArgs(post.id, {secure: undefined, absolute: true}).returns('http://my-ghost-blog.com/' + post.slug + '/');
}); });
generateFeed(baseUrl, data).then(function (xmlData) { generateFeed(baseUrl, data).then(function (xmlData) {