prefix dspace environment variables

This commit is contained in:
William Welling
2021-12-16 00:25:59 -06:00
parent b820794790
commit 21fcc9dde8
3 changed files with 58 additions and 37 deletions

View File

@@ -120,11 +120,6 @@ DSPACE_HOST # The host name of the angular application
DSPACE_PORT # The port number of the angular application DSPACE_PORT # The port number of the angular application
DSPACE_NAMESPACE # The namespace of the angular application DSPACE_NAMESPACE # The namespace of the angular application
DSPACE_SSL # Whether the angular application uses SSL [true/false] DSPACE_SSL # Whether the angular application uses SSL [true/false]
DSPACE_REST_HOST # The host name of the REST application
DSPACE_REST_PORT # The port number of the REST application
DSPACE_REST_NAMESPACE # The namespace of the REST application
DSPACE_REST_SSL # Whether the angular REST uses SSL [true/false]
``` ```
All other settings can be set using the following convention for naming the environment variables: All other settings can be set using the following convention for naming the environment variables:
@@ -134,14 +129,36 @@ All other settings can be set using the following convention for naming the envi
e.g. e.g.
``` ```bash
# The host name of the REST application
dspace.rest.host => DSPACE_REST_HOST
# The port number of the REST application
dspace.rest.port => DSPACE_REST_PORT
# The namespace of the REST application
dspace.rest.nameSpace => DSPACE_REST_NAMESPACE
# Whether the angular REST uses SSL [true/false]
dspace.rest.ssl => DSPACE_REST_SSL
cache.msToLive.default => CACHE_MSTOLIVE_DEFAULT cache.msToLive.default => CACHE_MSTOLIVE_DEFAULT
auth.ui.timeUntilIdle => AUTH_UI_TIMEUNTILIDLE auth.ui.timeUntilIdle => AUTH_UI_TIMEUNTILIDLE
``` ```
The equavelant to the non-conventional legacy settings:
```bash
DSPACE_UI_HOST => DSPACE_HOST
DSPACE_UI_PORT => DSPACE_PORT
DSPACE_UI_NAMESPACE => DSPACE_NAMESPACE
DSPACE_UI_SSL => DSPACE_SSL
```
The same settings can also be overwritten by setting system environment variables instead, E.g.: The same settings can also be overwritten by setting system environment variables instead, E.g.:
```bash ```bash
export DSPACE_HOST=api7.dspace.org export DSPACE_HOST=api7.dspace.org
export DSPACE_UI_PORT=4200
``` ```
The priority works as follows: **environment variable** overrides **variable in `.env` file** overrides external config set by `APP_CONFIG_PATH` overrides **`config.(prod or dev).yml`** The priority works as follows: **environment variable** overrides **variable in `.env` file** overrides external config set by `APP_CONFIG_PATH` overrides **`config.(prod or dev).yml`**

View File

@@ -5,11 +5,11 @@ Default configuration file is located at `config/config.yml`. All configuration
- Create a new `config.(dev or development).yml` file in `config/` for `development` environment; - Create a new `config.(dev or development).yml` file in `config/` for `development` environment;
- Create a new `config.(prod or production).yml` file in `config/` for `production` environment; - Create a new `config.(prod or production).yml` file in `config/` for `production` environment;
Alternatively, create a desired app config file at an external location and set the path as environment variable `APP_CONFIG_PATH`. Alternatively, create a desired app config file at an external location and set the path as environment variable `DSPACE_APP_CONFIG_PATH`.
e.g. e.g.
``` ```
APP_CONFIG_PATH=/usr/local/dspace/config/config.yml DSPACE_APP_CONFIG_PATH=/usr/local/dspace/config/config.yml
``` ```
Configuration options can be overridden by setting environment variables. Configuration options can be overridden by setting environment variables.
@@ -36,10 +36,10 @@ Alternately you can set the following environment variables. If any of these are
``` ```
or or
``` ```
UI_SSL=true DSPACE_UI_SSL=true
UI_HOST=localhost DSPACE_UI_HOST=localhost
UI_PORT=4000 DSPACE_UI_PORT=4000
UI_NAMESPACE=/ DSPACE_UI_NAMESPACE=/
``` ```
## DSpace's REST endpoint ## DSpace's REST endpoint
@@ -61,13 +61,6 @@ Alternately you can set the following environment variables. If any of these are
DSPACE_REST_PORT=443 DSPACE_REST_PORT=443
DSPACE_REST_NAMESPACE=/server DSPACE_REST_NAMESPACE=/server
``` ```
or
```
REST_SSL=true
REST_HOST=api7.dspace.org
REST_PORT=443
REST_NAMESPACE=/server
```
## Environment variable naming convention ## Environment variable naming convention

View File

@@ -14,6 +14,14 @@ const CONFIG_PATH = join(process.cwd(), 'config');
type Environment = 'production' | 'development' | 'test'; type Environment = 'production' | 'development' | 'test';
const DSPACE = (key: string): string => {
return `DSPACE_${key}`;
};
const ENV = (key: string, prefix = false): any => {
return prefix ? process.env[DSPACE(key)] : process.env[key];
};
const getBooleanFromString = (variable: string): boolean => { const getBooleanFromString = (variable: string): boolean => {
return variable === 'true' || variable === '1'; return variable === 'true' || variable === '1';
}; };
@@ -24,8 +32,8 @@ const getNumberFromString = (variable: string): number => {
const getEnvironment = (): Environment => { const getEnvironment = (): Environment => {
let environment: Environment = 'development'; let environment: Environment = 'development';
if (isNotEmpty(process.env.NODE_ENV)) { if (isNotEmpty(ENV('NODE_ENV'))) {
switch (process.env.NODE_ENV) { switch (ENV('NODE_ENV')) {
case 'prod': case 'prod':
case 'production': case 'production':
environment = 'production'; environment = 'production';
@@ -37,7 +45,7 @@ const getEnvironment = (): Environment => {
case 'development': case 'development':
break; break;
default: default:
console.warn(`Unknown NODE_ENV ${process.env.NODE_ENV}. Defaulting to development`); console.warn(`Unknown NODE_ENV ${ENV('NODE_ENV')}. Defaulting to development`);
} }
} }
@@ -102,19 +110,20 @@ const overrideWithEnvironment = (config: Config, key: string = '') => {
if (typeof innerConfig === 'object') { if (typeof innerConfig === 'object') {
overrideWithEnvironment(innerConfig, variable); overrideWithEnvironment(innerConfig, variable);
} else { } else {
if (isNotEmpty(process.env[variable])) { const value = ENV(variable, true);
console.log(`Applying environment variable ${variable} with value ${process.env[variable]}`); if (isNotEmpty(value)) {
console.log(`Applying environment variable ${DSPACE(variable)} with value ${value}`);
switch (typeof innerConfig) { switch (typeof innerConfig) {
case 'number': case 'number':
config[property] = getNumberFromString(process.env[variable]); config[property] = getNumberFromString(value);
break; break;
case 'boolean': case 'boolean':
config[property] = getBooleanFromString(process.env[variable]); config[property] = getBooleanFromString(value);
break; break;
case 'string': case 'string':
config[property] = process.env[variable]; config[property] = value;
default: default:
console.warn(`Unsupported environment variable type ${typeof innerConfig} ${variable}`); console.warn(`Unsupported environment variable type ${typeof innerConfig} ${DSPACE(variable)}`);
} }
} }
} }
@@ -122,6 +131,8 @@ const overrideWithEnvironment = (config: Config, key: string = '') => {
} }
}; };
const buildBaseUrl = (config: ServerConfig): void => { const buildBaseUrl = (config: ServerConfig): void => {
config.baseUrl = [ config.baseUrl = [
config.ssl ? 'https://' : 'http://', config.ssl ? 'https://' : 'http://',
@@ -168,7 +179,7 @@ export const buildAppConfig = (destConfigPath?: string): AppConfig => {
} }
// override with external config if specified by environment variable `APP_CONFIG_PATH` // override with external config if specified by environment variable `APP_CONFIG_PATH`
const externalConfigPath = process.env.APP_CONFIG_PATH; const externalConfigPath = ENV('APP_CONFIG_PATH', true);
if (isNotEmpty(externalConfigPath)) { if (isNotEmpty(externalConfigPath)) {
if (fs.existsSync(externalConfigPath)) { if (fs.existsSync(externalConfigPath)) {
overrideWithConfig(appConfig, externalConfigPath); overrideWithConfig(appConfig, externalConfigPath);
@@ -181,16 +192,16 @@ export const buildAppConfig = (destConfigPath?: string): AppConfig => {
overrideWithEnvironment(appConfig); overrideWithEnvironment(appConfig);
// apply existing non convention UI environment variables // apply existing non convention UI environment variables
appConfig.ui.host = isNotEmpty(process.env.DSPACE_HOST) ? process.env.DSPACE_HOST : appConfig.ui.host; appConfig.ui.host = isNotEmpty(ENV('HOST', true)) ? ENV('HOST', true) : appConfig.ui.host;
appConfig.ui.port = isNotEmpty(process.env.DSPACE_PORT) ? getNumberFromString(process.env.DSPACE_PORT) : appConfig.ui.port; appConfig.ui.port = isNotEmpty(ENV('PORT', true)) ? getNumberFromString(ENV('PORT', true)) : appConfig.ui.port;
appConfig.ui.nameSpace = isNotEmpty(process.env.DSPACE_NAMESPACE) ? process.env.DSPACE_NAMESPACE : appConfig.ui.nameSpace; appConfig.ui.nameSpace = isNotEmpty(ENV('NAMESPACE', true)) ? ENV('NAMESPACE', true) : appConfig.ui.nameSpace;
appConfig.ui.ssl = isNotEmpty(process.env.DSPACE_SSL) ? getBooleanFromString(process.env.DSPACE_SSL) : appConfig.ui.ssl; appConfig.ui.ssl = isNotEmpty(ENV('SSL', true)) ? getBooleanFromString(ENV('SSL', true)) : appConfig.ui.ssl;
// apply existing non convention REST environment variables // apply existing non convention REST environment variables
appConfig.rest.host = isNotEmpty(process.env.DSPACE_REST_HOST) ? process.env.DSPACE_REST_HOST : appConfig.rest.host; appConfig.rest.host = isNotEmpty(ENV('REST_HOST', true)) ? ENV('REST_HOST', true) : appConfig.rest.host;
appConfig.rest.port = isNotEmpty(process.env.DSPACE_REST_PORT) ? getNumberFromString(process.env.DSPACE_REST_PORT) : appConfig.rest.port; appConfig.rest.port = isNotEmpty(ENV('REST_PORT', true)) ? getNumberFromString(ENV('REST_PORT', true)) : appConfig.rest.port;
appConfig.rest.nameSpace = isNotEmpty(process.env.DSPACE_REST_NAMESPACE) ? process.env.DSPACE_REST_NAMESPACE : appConfig.rest.nameSpace; appConfig.rest.nameSpace = isNotEmpty(ENV('REST_NAMESPACE', true)) ? ENV('REST_NAMESPACE', true) : appConfig.rest.nameSpace;
appConfig.rest.ssl = isNotEmpty(process.env.DSPACE_REST_SSL) ? getBooleanFromString(process.env.DSPACE_REST_SSL) : appConfig.rest.ssl; appConfig.rest.ssl = isNotEmpty(ENV('REST_SSL', true)) ? getBooleanFromString(ENV('REST_SSL', true)) : appConfig.rest.ssl;
// apply build defined production // apply build defined production
appConfig.production = env === 'production'; appConfig.production = env === 'production';