Skip to content

Commit

Permalink
Get virtualHost and virtualHostedPaths from env
Browse files Browse the repository at this point in the history
  • Loading branch information
razvanMiu committed Sep 15, 2021
1 parent c06e019 commit ae339e5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -9,7 +9,8 @@ This package offers an Express middleware virtual hosting using the Virtual Host

## Getting started

This package requires a config setting `virtualHostedPaths`, an array of paths that will go through vh, which can be set in the theme addon. Also the theme addon needs to be placed above volto-middleware-vh in the addons dependency list.
This package requires a config setting `virtualHostedPaths` or an env `RAZZLE_VIRTUAL_HOSTED_PATHS`, an array of paths that will go through vh, which can be set in the theme addon. Also the theme addon needs to be placed above volto-middleware-vh in the addons dependency list.
You can also set a `virtualHost` config setting or `RAZZLE_VIRTUAL_HOST` env which will be used as the new host by Virtual Host Monster

### Example of usage:

Expand Down
8 changes: 6 additions & 2 deletions src/index.js
Expand Up @@ -14,12 +14,13 @@ const getReqPath = (req) => {

const getVirtualHost = () => {
const { settings } = config;
const vh = process.env.RAZZLE_VIRTUAL_HOST || settings.virtualHost;
const internalApiUrl = parseUrl(settings.internalApiPath || settings.apiPath);
const apiUrl = parseUrl(settings.apiPath);
const publicUrl = parseUrl(settings.publicURL);
const apiProtocol = apiUrl.protocol.slice(0, apiUrl.protocol.length - 1);
const virtualHost =
settings.virtualHost ||
vh ||
`/${apiProtocol}/${publicUrl.hostname}${
__DEVELOPMENT__ ? `:${publicUrl.port}` : ''
}${internalApiUrl.path}`;
Expand All @@ -46,7 +47,10 @@ export const getAPIResourceWithAuth = (req) => {

export default (config) => {
if (__SERVER__) {
const vhPaths = config.settings.virtualHostedPaths || [];
const vhPaths =
process.env.RAZZLE_VIRTUAL_HOSTED_PATHS?.split(',') ||
config.settings.virtualHostedPaths ||
[];
if (vhPaths.length) {
const express = require('express');
const middleware = express.Router();
Expand Down

0 comments on commit ae339e5

Please sign in to comment.