From c12ae81ecee2840b9f2062ca60a25601dce94afa Mon Sep 17 00:00:00 2001 From: Naz Date: Wed, 17 Nov 2021 20:00:27 +0400 Subject: [PATCH] Introduced "withFrontend" and "withBackend" flags in boot refs https://github.com/TryGhost/Toolbox/issues/135 - These flags are meant to control initialization of sections of the boot sequence depending on the needs - with or without bakend (API)/frontend (public handlebars site) - Ideally these flags should not be passed deep into the components, and if the are (like in the web/parent/app case) it's a smell that we need to move things up into the boot process! --- core/boot.js | 45 +++++++++++++++++--------- core/server/services/url/UrlService.js | 7 ++-- core/server/web/parent/app.js | 25 +++++++++----- 3 files changed, 51 insertions(+), 26 deletions(-) diff --git a/core/boot.js b/core/boot.js index 5c6dba4330..fcd341ebb8 100644 --- a/core/boot.js +++ b/core/boot.js @@ -69,8 +69,9 @@ async function initDatabase({config, logging}) { * @param {object} options.ghostServer * @param {object} options.config * @param {object} options.bootLogger + * @param {boolean} options.withFrontend */ -async function initCore({ghostServer, config, bootLogger}) { +async function initCore({ghostServer, config, bootLogger, withFrontend}) { debug('Begin: initCore'); // URL Utils is a bit slow, put it here so the timing is visible separate from models @@ -100,7 +101,8 @@ async function initCore({ghostServer, config, bootLogger}) { urlService.init({ onFinished: () => { bootLogger.log('URL Service Ready'); - } + }, + urlCache: !withFrontend // hacky parameter to make the cache initialization kick in as we can't initialize labs before the boot }); debug('End: Url Service'); @@ -159,10 +161,13 @@ async function initFrontend() { * At the moment we load our express apps all in one go, they require themselves and are co-located * What we want is to be able to optionally load various components and mount them * So eventually this function should go away + * @param {Object} options + * @param {Boolean} options.withBackend + * @param {Boolean} options.withFrontend */ -async function initExpressApps() { +async function initExpressApps(options) { debug('Begin: initExpressApps'); - const parentApp = require('./server/web/parent/app')(); + const parentApp = require('./server/web/parent/app')(options); debug('End: initExpressApps'); return parentApp; } @@ -297,7 +302,7 @@ async function initBackgroundServices({config}) { * @returns {Promise} ghostServer */ -async function bootGhost() { +async function bootGhost({withBackend = true, withFrontend = true} = {}) { // Metrics const startTime = Date.now(); debug('Begin Boot'); @@ -346,11 +351,14 @@ async function bootGhost() { // Step 2 - Start server with minimal app in global maintenance mode debug('Begin: load server + minimal app'); const rootApp = require('./app'); - const GhostServer = require('./server/ghost-server'); - ghostServer = new GhostServer({url: config.getSiteUrl()}); - await ghostServer.start(rootApp); - bootLogger.log('server started'); - debug('End: load server + minimal app'); + + if (withBackend) { + const GhostServer = require('./server/ghost-server'); + ghostServer = new GhostServer({url: config.getSiteUrl()}); + await ghostServer.start(rootApp); + bootLogger.log('server started'); + debug('End: load server + minimal app'); + } // Step 3 - Get the DB ready debug('Begin: Get DB ready'); @@ -360,11 +368,18 @@ async function bootGhost() { // Step 4 - Load Ghost with all its services debug('Begin: Load Ghost Services & Apps'); - await initCore({ghostServer, config, bootLogger}); - await initServicesForFrontend(); - await initFrontend(); - const ghostApp = await initExpressApps(); - await initDynamicRouting(); + await initCore({ghostServer, config, bootLogger, withFrontend}); + + if (withFrontend) { + await initServicesForFrontend(); + await initFrontend(); + } + const ghostApp = await initExpressApps({withFrontend, withBackend}); + + if (withFrontend) { + await initDynamicRouting(); + } + await initServices({config}); debug('End: Load Ghost Services & Apps'); diff --git a/core/server/services/url/UrlService.js b/core/server/services/url/UrlService.js index 5bbce1b8bc..fbc333612c 100644 --- a/core/server/services/url/UrlService.js +++ b/core/server/services/url/UrlService.js @@ -319,14 +319,15 @@ class UrlService { * @description Initializes components needed for the URL Service to function * @param {Object} options * @param {Function} [options.onFinished] - callback when url generation is finished + * @param {Boolean} [options.urlCache] - whether to init using url cache or not */ - async init(options = {}) { - this.onFinished = options.onFinished; + async init({onFinished, urlCache} = {}) { + this.onFinished = onFinished; let persistedUrls; let persistedResources; - if (labs.isSet('urlCache')) { + if (labs.isSet('urlCache') || urlCache) { persistedUrls = await this.readCacheFile(this.urlsCachePath); persistedResources = await this.readCacheFile(this.resourcesCachePath); } diff --git a/core/server/web/parent/app.js b/core/server/web/parent/app.js index 3575729788..5533cc2662 100644 --- a/core/server/web/parent/app.js +++ b/core/server/web/parent/app.js @@ -5,7 +5,13 @@ const compress = require('compression'); const mw = require('./middleware'); const vhost = require('@tryghost/vhost-middleware'); -module.exports = function setupParentApp(options = {}) { +/** + * @param {Object} options + * @param {Boolean} [options.start] + * @param {Boolean} options.withBackend + * @param {Boolean} options.withFrontend + */ +module.exports = function setupParentApp({start, withFrontend, withBackend}) { debug('ParentApp setup start'); const parentApp = express('parent'); @@ -26,14 +32,17 @@ module.exports = function setupParentApp(options = {}) { // Mount the express apps on the parentApp - // ADMIN + API - const backendApp = require('./backend')(); - parentApp.use(vhost(config.getBackendMountPath(), backendApp)); - - // SITE + MEMBERS - const frontendApp = require('./frontend')(options); - parentApp.use(vhost(config.getFrontendMountPath(), frontendApp)); + if (withBackend) { + debug('Mounting bakcend: ADMIN + API'); + const backendApp = require('./backend')(); + parentApp.use(vhost(config.getBackendMountPath(), backendApp)); + } + if (withFrontend) { + debug('Mounting frontend: SITE + MEMBERS'); + const frontendApp = require('./frontend')({start}); + parentApp.use(vhost(config.getFrontendMountPath(), frontendApp)); + } debug('ParentApp setup end'); return parentApp;