From be10039f76679daf323f2ec801394c86857cd23f Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Mon, 27 Apr 2020 16:54:31 +0100 Subject: [PATCH] Renamed app related files+variables for clarity - renamed the parentApp in index.js to ghostApp, to reduce confusion with the layer that is named parentApp - renamed the adminApp inside of parentApp to backendApp to reflect the fact it's both admin+api - renamed a bunch more variables there to be backend, rather than admin - renamed the api index.js file to app.js and created a new index which is an actual index --- core/server/web/api/app.js | 31 +++++++++++++++++++++++++++++++ core/server/web/api/index.js | 32 +------------------------------- core/server/web/parent/app.js | 24 ++++++++++++------------ index.js | 8 ++++---- 4 files changed, 48 insertions(+), 47 deletions(-) create mode 100644 core/server/web/api/app.js diff --git a/core/server/web/api/app.js b/core/server/web/api/app.js new file mode 100644 index 0000000000..4e3c1506e2 --- /dev/null +++ b/core/server/web/api/app.js @@ -0,0 +1,31 @@ +const debug = require('ghost-ignition').debug('web:api:default:app'); +const express = require('express'); +const urlUtils = require('../../lib/url-utils'); +const errorHandler = require('../shared/middlewares/error-handler'); +const sentry = require('../../sentry'); + +module.exports = function setupApiApp() { + debug('Parent API setup start'); + const apiApp = express(); + apiApp.use(sentry.requestHandler); + + // Mount different API versions + apiApp.use(urlUtils.getVersionPath({version: 'v2', type: 'content'}), require('./v2/content/app')()); + apiApp.use(urlUtils.getVersionPath({version: 'v2', type: 'admin'}), require('./v2/admin/app')()); + + apiApp.use(urlUtils.getVersionPath({version: 'v3', type: 'content'}), require('./canary/content/app')()); + apiApp.use(urlUtils.getVersionPath({version: 'v3', type: 'admin'}), require('./canary/admin/app')()); + apiApp.use(urlUtils.getVersionPath({version: 'v3', type: 'members'}), require('./canary/members/app')()); + + apiApp.use(urlUtils.getVersionPath({version: 'canary', type: 'content'}), require('./canary/content/app')()); + apiApp.use(urlUtils.getVersionPath({version: 'canary', type: 'admin'}), require('./canary/admin/app')()); + apiApp.use(urlUtils.getVersionPath({version: 'canary', type: 'members'}), require('./canary/members/app')()); + + // Error handling for requests to non-existent API versions + apiApp.use(sentry.errorHandler); + apiApp.use(errorHandler.resourceNotFound); + apiApp.use(errorHandler.handleJSONResponse); + + debug('Parent API setup end'); + return apiApp; +}; diff --git a/core/server/web/api/index.js b/core/server/web/api/index.js index 4e3c1506e2..a9612f49fb 100644 --- a/core/server/web/api/index.js +++ b/core/server/web/api/index.js @@ -1,31 +1 @@ -const debug = require('ghost-ignition').debug('web:api:default:app'); -const express = require('express'); -const urlUtils = require('../../lib/url-utils'); -const errorHandler = require('../shared/middlewares/error-handler'); -const sentry = require('../../sentry'); - -module.exports = function setupApiApp() { - debug('Parent API setup start'); - const apiApp = express(); - apiApp.use(sentry.requestHandler); - - // Mount different API versions - apiApp.use(urlUtils.getVersionPath({version: 'v2', type: 'content'}), require('./v2/content/app')()); - apiApp.use(urlUtils.getVersionPath({version: 'v2', type: 'admin'}), require('./v2/admin/app')()); - - apiApp.use(urlUtils.getVersionPath({version: 'v3', type: 'content'}), require('./canary/content/app')()); - apiApp.use(urlUtils.getVersionPath({version: 'v3', type: 'admin'}), require('./canary/admin/app')()); - apiApp.use(urlUtils.getVersionPath({version: 'v3', type: 'members'}), require('./canary/members/app')()); - - apiApp.use(urlUtils.getVersionPath({version: 'canary', type: 'content'}), require('./canary/content/app')()); - apiApp.use(urlUtils.getVersionPath({version: 'canary', type: 'admin'}), require('./canary/admin/app')()); - apiApp.use(urlUtils.getVersionPath({version: 'canary', type: 'members'}), require('./canary/members/app')()); - - // Error handling for requests to non-existent API versions - apiApp.use(sentry.errorHandler); - apiApp.use(errorHandler.resourceNotFound); - apiApp.use(errorHandler.handleJSONResponse); - - debug('Parent API setup end'); - return apiApp; -}; +module.exports = require('./app'); diff --git a/core/server/web/parent/app.js b/core/server/web/parent/app.js index 57add55a27..a32b182fe5 100644 --- a/core/server/web/parent/app.js +++ b/core/server/web/parent/app.js @@ -46,27 +46,27 @@ module.exports = function setupParentApp(options = {}) { // Mount the express apps on the parentApp - const adminHost = config.get('admin:url') ? (new URL(config.get('admin:url')).hostname) : ''; + const backendHost = config.get('admin:url') ? (new URL(config.get('admin:url')).hostname) : ''; const frontendHost = new URL(config.get('url')).hostname; - const hasSeparateAdmin = adminHost && adminHost !== frontendHost; + const hasSeparateBackendHost = backendHost && backendHost !== frontendHost; // Wrap the admin and API apps into a single express app for use with vhost - const adminApp = express(); - adminApp.use(sentry.requestHandler); - adminApp.enable('trust proxy'); // required to respect x-forwarded-proto in admin requests - adminApp.use('/ghost/api', require('../api')()); - adminApp.use('/ghost/.well-known', require('../well-known')()); - adminApp.use('/ghost', require('../../services/auth/session').createSessionFromToken, require('../admin')()); + const backendApp = express(); + backendApp.use(sentry.requestHandler); + backendApp.enable('trust proxy'); // required to respect x-forwarded-proto in admin requests + backendApp.use('/ghost/api', require('../api')()); + backendApp.use('/ghost/.well-known', require('../well-known')()); + backendApp.use('/ghost', require('../../services/auth/session').createSessionFromToken, require('../admin')()); // ADMIN + API // with a separate admin url only serve on that host, otherwise serve on all hosts - const adminVhostArg = hasSeparateAdmin && adminHost ? adminHost : /.*/; - parentApp.use(vhost(adminVhostArg, adminApp)); + const backendVhostArg = hasSeparateBackendHost && backendHost ? backendHost : /.*/; + parentApp.use(vhost(backendVhostArg, backendApp)); // BLOG // with a separate admin url we adjust the frontend vhost to exclude requests to that host, otherwise serve on all hosts - const frontendVhostArg = (hasSeparateAdmin && adminHost) ? - new RegExp(`^(?!${escapeRegExp(adminHost)}).*`) : /.*/; + const frontendVhostArg = (hasSeparateBackendHost && backendHost) ? + new RegExp(`^(?!${escapeRegExp(backendHost)}).*`) : /.*/; parentApp.use(vhost(frontendVhostArg, require('../site')(options))); diff --git a/index.js b/index.js index c62948bf7c..d050ba032e 100644 --- a/index.js +++ b/index.js @@ -14,19 +14,19 @@ debug('Required ghost'); const express = require('express'); const common = require('./core/server/lib/common'); const urlService = require('./core/frontend/services/url'); -const parentApp = express(); +const ghostApp = express(); -parentApp.use(sentry.requestHandler); +ghostApp.use(sentry.requestHandler); debug('Initialising Ghost'); ghost().then(function (ghostServer) { // Mount our Ghost instance on our desired subdirectory path if it exists. - parentApp.use(urlService.utils.getSubdir(), ghostServer.rootApp); + ghostApp.use(urlService.utils.getSubdir(), ghostServer.rootApp); debug('Starting Ghost'); // Let Ghost handle starting our server instance. - return ghostServer.start(parentApp) + return ghostServer.start(ghostApp) .then(function afterStart() { common.logging.info('Ghost boot', (Date.now() - startTime) / 1000 + 's'); });