Ghost/core/server/web/parent/vhost-utils.js
Hannah Wolfe 77996d1ee4
Moved vhost arg logic out of the parent app
- Makes the logic for determining the admin and frontend vhost args independent and easier to test
- Moved the tests to specifically test the vhost utils & removed proxyquire as a dependency
- We want to breakdown the current parent app into the existing core/app.js and boot code, allowing us to decouple the backend and frontend further
- This is all part of the refactoring to separate server and frontend completely
2021-06-28 19:38:42 +01:00

40 lines
1.2 KiB
JavaScript

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