0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00

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!
This commit is contained in:
Naz 2021-11-17 20:00:27 +04:00 committed by naz
parent 5168d6a822
commit c12ae81ece
3 changed files with 51 additions and 26 deletions

View file

@ -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<object>} 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');

View file

@ -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);
}

View file

@ -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;