From b19424acb338f06de30c1d038a6e4c8023255447 Mon Sep 17 00:00:00 2001 From: Naz Date: Wed, 1 Dec 2021 19:56:29 +0400 Subject: [PATCH] Moved vhost mounts into boot file refs https://github.com/TryGhost/Toolbox/issues/152 - This stops the mounting of the admin and frontend from being buried deep in express initialisation - Instead it's explicit, which makes two things almost possible: 1. we can potentially boot the frontend or backend independently 2. we can pass services and settings loaded during boot into the frontend - This needs more work, but we can start to group all the frontend code together - Meanwhile we also need to rip apart the routing and url services to decouple the frontend from the backend fully - BABY STEPS! Co-authored-by: Hannah Wolfe --- core/boot.js | 23 ++++++++++++++++++++--- core/server/web/parent/app.js | 24 ++---------------------- core/server/web/parent/backend.js | 2 ++ 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/core/boot.js b/core/boot.js index 57e308d85e..a2010d846c 100644 --- a/core/boot.js +++ b/core/boot.js @@ -166,10 +166,27 @@ async function initFrontend() { * @param {Object} options * @param {Boolean} options.backend * @param {Boolean} options.frontend + * @param {Object} options.config */ -async function initExpressApps(options) { +async function initExpressApps({frontend, backend, config}) { debug('Begin: initExpressApps'); - const parentApp = require('./server/web/parent/app')(options); + + const parentApp = require('./server/web/parent/app')(); + const vhost = require('@tryghost/vhost-middleware'); + + // Mount the express apps on the parentApp + if (backend) { + // ADMIN + API + const backendApp = require('./server/web/parent/backend')(); + parentApp.use(vhost(config.getBackendMountPath(), backendApp)); + } + + if (frontend) { + // SITE + MEMBERS + const frontendApp = require('./server/web/parent/frontend')({}); + parentApp.use(vhost(config.getFrontendMountPath(), frontendApp)); + } + debug('End: initExpressApps'); return parentApp; } @@ -373,7 +390,7 @@ async function bootGhost({backend = true, frontend = true} = {}) { if (frontend) { await initFrontend(); } - const ghostApp = await initExpressApps({frontend, backend}); + const ghostApp = await initExpressApps({frontend, backend, config}); if (frontend) { await initDynamicRouting(); diff --git a/core/server/web/parent/app.js b/core/server/web/parent/app.js index 67ead46fc6..9b9baf9446 100644 --- a/core/server/web/parent/app.js +++ b/core/server/web/parent/app.js @@ -3,22 +3,15 @@ const config = require('../../../shared/config'); const express = require('../../../shared/express'); const compress = require('compression'); const mw = require('./middleware'); -const vhost = require('@tryghost/vhost-middleware'); -/** - * @param {Object} options - * @param {Boolean} [options.start] - * @param {Boolean} [options.backend] - * @param {Boolean} [options.frontend] - */ -module.exports = function setupParentApp({start, frontend = true, backend = true}) { +module.exports = function setupParentApp() { debug('ParentApp setup start'); const parentApp = express('parent'); parentApp.use(mw.requestId); parentApp.use(mw.logRequest); - // Register event emmiter on req/res to trigger cache invalidation webhook event + // Register event emitter on req/res to trigger cache invalidation webhook event parentApp.use(mw.emitEvents); // enabled gzip compression by default @@ -30,19 +23,6 @@ module.exports = function setupParentApp({start, frontend = true, backend = true // @TODO: figure out if this is really needed everywhere? Is it not frontend only... parentApp.use(mw.ghostLocals); - // Mount the express apps on the parentApp - - if (backend) { - debug('Mounting bakcend: ADMIN + API'); - const backendApp = require('./backend')(); - parentApp.use(vhost(config.getBackendMountPath(), backendApp)); - } - - if (frontend) { - debug('Mounting frontend: SITE + MEMBERS'); - const frontendApp = require('./frontend')({start}); - parentApp.use(vhost(config.getFrontendMountPath(), frontendApp)); - } debug('ParentApp setup end'); return parentApp; diff --git a/core/server/web/parent/backend.js b/core/server/web/parent/backend.js index b71cb6c77a..23c340441f 100644 --- a/core/server/web/parent/backend.js +++ b/core/server/web/parent/backend.js @@ -10,9 +10,11 @@ module.exports = () => { // BACKEND // Wrap the admin and API apps into a single express app for use with vhost const backendApp = express('backend'); + backendApp.lazyUse('/ghost/api', require('../api')); backendApp.lazyUse('/ghost/oauth', require('../oauth')); backendApp.lazyUse('/ghost/.well-known', require('../well-known')); + backendApp.use('/ghost', require('../../services/auth/session').createSessionFromToken, require('../admin')()); return backendApp;