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 <erisds@gmail.com>
This commit is contained in:
Naz 2021-12-01 19:56:29 +04:00 committed by naz
parent f2ba9d3aac
commit b19424acb3
3 changed files with 24 additions and 25 deletions

View File

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

View File

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

View File

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