mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -05:00
Moved "vhost-utils" to config helpers
- These are simple functions that get data from config in a specific format - They are also used by the topmost part of the application - Config helpers seems like a reasonable fit to get them out of the web folder - Functions have also been renamed to try to get them to make more sense
This commit is contained in:
parent
89b0b6cf5b
commit
332beaaf90
5 changed files with 51 additions and 58 deletions
|
@ -4,7 +4,6 @@ const express = require('../../../shared/express');
|
|||
const compress = require('compression');
|
||||
const mw = require('./middleware');
|
||||
const vhost = require('@tryghost/vhost-middleware');
|
||||
const vhostUtils = require('./vhost-utils');
|
||||
|
||||
module.exports = function setupParentApp(options = {}) {
|
||||
debug('ParentApp setup start');
|
||||
|
@ -29,11 +28,11 @@ module.exports = function setupParentApp(options = {}) {
|
|||
|
||||
// ADMIN + API
|
||||
const backendApp = require('./backend')();
|
||||
parentApp.use(vhost(vhostUtils.getBackendHostArg(), backendApp));
|
||||
parentApp.use(vhost(config.getBackendMountPath(), backendApp));
|
||||
|
||||
// SITE + MEMBERS
|
||||
const frontendApp = require('./frontend')(options);
|
||||
parentApp.use(vhost(vhostUtils.getFrontendHostArg(), frontendApp));
|
||||
parentApp.use(vhost(config.getFrontendMountPath(), frontendApp));
|
||||
|
||||
debug('ParentApp setup end');
|
||||
|
||||
|
|
|
@ -1,39 +0,0 @@
|
|||
const config = require('../../../shared/config');
|
||||
const escapeRegExp = require('lodash/escapeRegExp');
|
||||
const {URL} = require('url');
|
||||
|
||||
const DEFAULT_HOST_ARG = /.*/;
|
||||
|
||||
const getHostsFromConfig = () => {
|
||||
const frontendHost = new URL(config.getSiteUrl()).hostname;
|
||||
|
||||
const backendHost = config.getAdminUrl() ? (new URL(config.getAdminUrl()).hostname) : '';
|
||||
const hasSeparateBackendHost = backendHost && backendHost !== frontendHost;
|
||||
|
||||
return {
|
||||
backendHost,
|
||||
hasSeparateBackendHost
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @returns {string|RegExp}
|
||||
*/
|
||||
module.exports.getBackendHostArg = () => {
|
||||
const {backendHost, hasSeparateBackendHost} = getHostsFromConfig();
|
||||
|
||||
// with a separate admin url only serve on that host, otherwise serve on all hosts
|
||||
return (hasSeparateBackendHost) && backendHost ? backendHost : DEFAULT_HOST_ARG;
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @returns {string|RegExp}
|
||||
*/
|
||||
module.exports.getFrontendHostArg = () => {
|
||||
const {backendHost, hasSeparateBackendHost} = getHostsFromConfig();
|
||||
|
||||
// with a separate admin url we adjust the frontend vhost to exclude requests to that host, otherwise serve on all hosts
|
||||
return (hasSeparateBackendHost && backendHost) ? new RegExp(`^(?!${escapeRegExp(backendHost)}).*`) : DEFAULT_HOST_ARG;
|
||||
};
|
|
@ -1,4 +1,42 @@
|
|||
const path = require('path');
|
||||
const escapeRegExp = require('lodash/escapeRegExp');
|
||||
const {URL} = require('url');
|
||||
|
||||
const DEFAULT_HOST_ARG = /.*/;
|
||||
|
||||
const getHostInfo = (config) => {
|
||||
const frontendHost = new URL(config.getSiteUrl()).hostname;
|
||||
|
||||
const backendHost = config.getAdminUrl() ? (new URL(config.getAdminUrl()).hostname) : '';
|
||||
const hasSeparateBackendHost = backendHost && backendHost !== frontendHost;
|
||||
|
||||
return {
|
||||
backendHost,
|
||||
hasSeparateBackendHost
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @returns {string|RegExp}
|
||||
*/
|
||||
const getBackendMountPath = function getFrontendMountPath() {
|
||||
const {backendHost, hasSeparateBackendHost} = getHostInfo(this);
|
||||
|
||||
// with a separate admin url only serve on that host, otherwise serve on all hosts
|
||||
return (hasSeparateBackendHost) && backendHost ? backendHost : DEFAULT_HOST_ARG;
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @returns {string|RegExp}
|
||||
*/
|
||||
const getFrontendMountPath = function getFrontendMountPath() {
|
||||
const {backendHost, hasSeparateBackendHost} = getHostInfo(this);
|
||||
|
||||
// with a separate admin url we adjust the frontend vhost to exclude requests to that host, otherwise serve on all hosts
|
||||
return (hasSeparateBackendHost && backendHost) ? new RegExp(`^(?!${escapeRegExp(backendHost)}).*`) : DEFAULT_HOST_ARG;
|
||||
};
|
||||
|
||||
/**
|
||||
* @callback isPrivacyDisabledFn
|
||||
|
@ -62,4 +100,6 @@ const getContentPath = function getContentPath(type) {
|
|||
module.exports.bindAll = (nconf) => {
|
||||
nconf.isPrivacyDisabled = isPrivacyDisabled.bind(nconf);
|
||||
nconf.getContentPath = getContentPath.bind(nconf);
|
||||
nconf.getBackendMountPath = getBackendMountPath.bind(nconf);
|
||||
nconf.getFrontendMountPath = getFrontendMountPath.bind(nconf);
|
||||
};
|
||||
|
|
|
@ -41,8 +41,8 @@ function loadNconf(options) {
|
|||
// ## Config Methods
|
||||
|
||||
// Expose dynamic utility methods
|
||||
helpers.bindAll(nconf);
|
||||
urlHelpers.bindAll(nconf);
|
||||
helpers.bindAll(nconf);
|
||||
|
||||
// ## Sanitization
|
||||
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
const should = require('should');
|
||||
const configUtils = require('../../../../utils/configUtils');
|
||||
|
||||
const vhostUtils = require('../../../../../core/server/web/parent/vhost-utils');
|
||||
const configUtils = require('../../../utils/configUtils');
|
||||
|
||||
describe('vhost utils', function () {
|
||||
beforeEach(function () {
|
||||
|
@ -12,16 +10,11 @@ describe('vhost utils', function () {
|
|||
configUtils.restore();
|
||||
});
|
||||
|
||||
it('exposes two methods', function () {
|
||||
Object.keys(vhostUtils).should.be.an.Array().with.lengthOf(2);
|
||||
vhostUtils.should.have.properties('getBackendHostArg', 'getFrontendHostArg');
|
||||
});
|
||||
|
||||
// url = 'https://ghost.blog'
|
||||
describe('without separate admin url', function () {
|
||||
it('uses the default arg for both backend and frontend', function () {
|
||||
vhostUtils.getBackendHostArg().should.eql(/.*/);
|
||||
vhostUtils.getFrontendHostArg().should.eql(/.*/);
|
||||
configUtils.config.getBackendMountPath().should.eql(/.*/);
|
||||
configUtils.config.getFrontendMountPath().should.eql(/.*/);
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -33,12 +26,12 @@ describe('vhost utils', function () {
|
|||
});
|
||||
|
||||
it('should use admin url and inverse as args', function () {
|
||||
vhostUtils.getBackendHostArg().should.eql('admin.ghost.blog');
|
||||
vhostUtils.getFrontendHostArg().should.eql(/^(?!admin\.ghost\.blog).*/);
|
||||
configUtils.config.getBackendMountPath().should.eql('admin.ghost.blog');
|
||||
configUtils.config.getFrontendMountPath().should.eql(/^(?!admin\.ghost\.blog).*/);
|
||||
});
|
||||
|
||||
it('should have regex that excludes admin traffic on front-end', function () {
|
||||
const frontendRegex = vhostUtils.getFrontendHostArg();
|
||||
const frontendRegex = configUtils.config.getFrontendMountPath();
|
||||
|
||||
frontendRegex.test('localhost').should.be.true();
|
||||
frontendRegex.test('ghost.blog').should.be.true();
|
||||
|
@ -54,8 +47,8 @@ describe('vhost utils', function () {
|
|||
});
|
||||
|
||||
it('should mount and assign correct routes', function () {
|
||||
vhostUtils.getBackendHostArg().should.eql(/.*/);
|
||||
vhostUtils.getFrontendHostArg().should.eql(/.*/);
|
||||
configUtils.config.getBackendMountPath().should.eql(/.*/);
|
||||
configUtils.config.getFrontendMountPath().should.eql(/.*/);
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Add table
Reference in a new issue