diff --git a/docs/Configuration.md b/docs/Configuration.md index 712b1523a0..b2ec81533d 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -1,5 +1,60 @@ # Configuration +Default configuration file is located in `config/` folder. All configuration options should be listed in the default configuration file `config/environment.default.js`. Please do not change this file directly! To change the default configuration values, create local files that override the parameters you need to change: + +- Create a new `environment.dev.js` file in `config/` for `devel` environment; +- Create a new `environment.prod.js` file in `config/` for `production` environment; + +Some few configuration options can be overridden by setting environment variables. These and the variable names are listed below. + +## Nodejs server +When you start dspace-angular on node, it spins up an http server on which it listens for incoming connections. You can define the ip address and port the server should bind itsself to, and if ssl should be enabled not. By default it listens on `localhost:3000`. If you want it to listen on all your network connections, configure it to bind itself to `0.0.0.0`. + +To change this configuration, change the options `ui.host`, `ui.port` and `ui.ssl` in the appropriate configuration file (see above): +``` +module.exports = { + // Angular Universal server settings. + ui: { + ssl: false, + host: 'localhost', + port: 3000, + nameSpace: '/' + } +}; +``` + +Alternately you can set the following environment variables. If any of these are set, it will override all configuration files: +``` + DSPACE_SSL=true + DSPACE_HOST=localhost + DSPACE_PORT=3000 + DSPACE_NAMESPACE=/ +``` + +## DSpace's REST endpoint +dspace-angular connects to your DSpace installation by using its REST endpoint. To do so, you have to define the ip address, port and if ssl should be enabled. You can do this in a configuration file (see above) by adding the following options: + +``` +module.exports = { + // The REST API server settings. + rest: { + ssl: true, + host: 'dspace7.4science.it', + port: 443, + // NOTE: Space is capitalized because 'namespace' is a reserved string in TypeScript + nameSpace: '/dspace-spring-rest/api' + } +}; +``` + +Alternately you can set the following environment variables. If any of these are set, it will override all configuration files: +``` + DSPACE_REST_SSL=true + DSPACE_REST_HOST=localhost + DSPACE_REST_PORT=3000 + DSPACE_REST_NAMESPACE=/ +``` + ## Supporting analytics services other than Google Analytics This project makes use of [Angulartics](https://angulartics.github.io/angulartics2/) to track usage events and send them to Google Analytics. diff --git a/src/config.ts b/src/config.ts index f07cab37dc..2f2855c6fa 100644 --- a/src/config.ts +++ b/src/config.ts @@ -4,6 +4,7 @@ import { InjectionToken } from '@angular/core'; import { Config } from './config/config.interface'; import { ServerConfig } from './config/server-config.interface'; import { GlobalConfig } from './config/global-config.interface'; +import { hasValue } from './app/shared/empty.util'; const GLOBAL_CONFIG: InjectionToken = new InjectionToken('config'); @@ -55,6 +56,41 @@ if (envConfigFile) { } } +// allow to override a few important options by environment variables +function createServerConfig(host?: string, port?: string, nameSpace?: string, ssl?: string): ServerConfig { + const result = { host, nameSpace } as any; + + if (hasValue(port)) { + result.port = Number(port); + } + + if (hasValue(ssl)) { + result.ssl = ssl.trim().match(/^(true|1|yes)$/i) ? true : false; + } + + return result; +} + +const processEnv = { + ui: createServerConfig( + process.env.DSPACE_HOST, + process.env.DSPACE_PORT, + process.env.DSPACE_NAMESPACE, + process.env.DSPACE_SSL) as ServerConfig, + rest: createServerConfig( + process.env.DSPACE_REST_HOST, + process.env.DSPACE_REST_PORT, + process.env.DSPACE_REST_NAMESPACE, + process.env.DSPACE_REST_SSL) as ServerConfig +} as GlobalConfig; + +// merge the environment variables with our configuration. +try { + merge(processEnv) +} catch (e) { + console.warn('Unable to merge environment variable into the configuration') +} + buildBaseUrls(); // set config for whether running in production diff --git a/src/server.ts b/src/server.ts index 91229a7c78..040d13311f 100644 --- a/src/server.ts +++ b/src/server.ts @@ -21,8 +21,6 @@ import { ENV_CONFIG } from './config'; export function startServer(bootstrap: Type<{}> | NgModuleFactory<{}>) { const app = express(); - const port = ENV_CONFIG.ui.port ? ENV_CONFIG.ui.port : 80; - if (ENV_CONFIG.production) { enableProdMode(); app.use(compression()); @@ -90,7 +88,7 @@ export function startServer(bootstrap: Type<{}> | NgModuleFactory<{}>) { https.createServer({ key: keys.serviceKey, cert: keys.certificate - }, app).listen(port, ENV_CONFIG.ui.host, () => { + }, app).listen(ENV_CONFIG.ui.port, ENV_CONFIG.ui.host, () => { serverStarted(); }); } @@ -127,7 +125,7 @@ export function startServer(bootstrap: Type<{}> | NgModuleFactory<{}>) { }); } } else { - app.listen(port, ENV_CONFIG.ui.host, () => { + app.listen(ENV_CONFIG.ui.port, ENV_CONFIG.ui.host, () => { serverStarted(); }); }}