From 26e2da93fbb32c70a1e6df1ef4b2e668e4115195 Mon Sep 17 00:00:00 2001 From: William Welling Date: Tue, 26 Sep 2017 19:13:32 -0500 Subject: [PATCH] recursive merge of config --- src/app/app.component.ts | 4 ++ src/config.ts | 64 ++++++++++++++++++++---- src/config/cache-config.interface.ts | 4 +- src/config/config.interface.ts | 4 ++ src/config/global-config.interface.ts | 3 +- src/config/server-config.interface.ts | 4 +- src/config/universal-config.interface.ts | 4 +- 7 files changed, 74 insertions(+), 13 deletions(-) create mode 100644 src/config/config.interface.ts diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 8309a518de..9ea58365df 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -41,6 +41,10 @@ export class AppComponent implements OnInit { translate.setDefaultLang('en'); // the lang to use, if the lang isn't available, it will use the current loader to get them translate.use('en'); + + if (config.debug) { + console.info(config); + } } ngAfterViewChecked() { diff --git a/src/config.ts b/src/config.ts index 5aa66fed8b..12cb2274fb 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,6 +1,7 @@ // Look in ./config folder for config 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'; @@ -41,7 +42,7 @@ try { // if envConfigFile set try to get configs if (envConfigFile) { try { - ENV_CONFIG = Object.assign(ENV_CONFIG, configContext(envConfigFile)) as GlobalConfig; + merge(configContext(envConfigFile) as GlobalConfig); } catch (e) { console.warn('Cannot find file ' + envConfigFile.substring(2, envConfigFile.length), 'Using default environment.'); } @@ -50,15 +51,60 @@ if (envConfigFile) { // set config for whether running in production ENV_CONFIG.production = production; -// set base url if property is object with ssl, address, and port. i.e. ServerConfig -for (const key in ENV_CONFIG) { - if (ENV_CONFIG[key].host) { - ENV_CONFIG[key].baseUrl = [ - ENV_CONFIG[key].ssl ? 'https://' : 'http://', - ENV_CONFIG[key].host, - ENV_CONFIG[key].port ? (ENV_CONFIG[key].port !== 80 && ENV_CONFIG[key].port !== 443) ? ':' + ENV_CONFIG[key].port : '' : '' - ].join(''); +function merge(config: GlobalConfig): void { + innerMerge(ENV_CONFIG, config); + buildBaseUrls(); +} + +function innerMerge(globalConfig: Config, config: Config): void { + for (const key in config) { + if (config.hasOwnProperty(key)) { + if (isObject(config[key])) { + innerMerge(globalConfig[key], config[key]); + } else { + if (isDefined(config[key])) { + globalConfig[key] = config[key]; + } + } + } } } +function buildBaseUrls(): void { + for (const key in ENV_CONFIG) { + if (ENV_CONFIG.hasOwnProperty(key) && ENV_CONFIG[key].host) { + ENV_CONFIG[key].baseUrl = [ + getProtocol(ENV_CONFIG[key].ssl), + getHost(ENV_CONFIG[key].host), + getPort(ENV_CONFIG[key].port), + getContextPath(ENV_CONFIG[key].contextPath) + ].join(''); + } + } +} + +function getProtocol(ssl: boolean): string { + return ssl ? 'https://' : 'http://'; +} + +function getHost(host: string): string { + return host; +} + +function getPort(port: number): string { + return port ? (port !== 80 && port !== 443) ? ':' + port : '' : ''; +} + +function getContextPath(contextPath: string): string { + return contextPath ? '/' + contextPath : ''; +} + +function isDefined(value: any): boolean { + return typeof value !== 'undefined' && value !== null; +} + +function isObject(item: any): boolean { + return item && typeof item === 'object' && !Array.isArray(item); +} + export { GlobalConfig, GLOBAL_CONFIG, ENV_CONFIG } diff --git a/src/config/cache-config.interface.ts b/src/config/cache-config.interface.ts index c26fa416de..f8a2c88640 100644 --- a/src/config/cache-config.interface.ts +++ b/src/config/cache-config.interface.ts @@ -1,4 +1,6 @@ -export interface CacheConfig { +import { Config } from './config.interface'; + +export interface CacheConfig extends Config { msToLive: number, control: string } diff --git a/src/config/config.interface.ts b/src/config/config.interface.ts new file mode 100644 index 0000000000..efae94cc56 --- /dev/null +++ b/src/config/config.interface.ts @@ -0,0 +1,4 @@ +// tslint:disable-next-line:no-empty-interface +export interface Config { + +} diff --git a/src/config/global-config.interface.ts b/src/config/global-config.interface.ts index 547169ef0c..056263dcb7 100644 --- a/src/config/global-config.interface.ts +++ b/src/config/global-config.interface.ts @@ -1,8 +1,9 @@ +import { Config } from './config.interface'; import { ServerConfig } from './server-config.interface'; import { CacheConfig } from './cache-config.interface'; import { UniversalConfig } from './universal-config.interface'; -export interface GlobalConfig { +export interface GlobalConfig extends Config { ui: ServerConfig; rest: ServerConfig; production: boolean; diff --git a/src/config/server-config.interface.ts b/src/config/server-config.interface.ts index 5ea6fcc255..aea2cfb725 100644 --- a/src/config/server-config.interface.ts +++ b/src/config/server-config.interface.ts @@ -1,4 +1,6 @@ -export interface ServerConfig { +import { Config } from './config.interface'; + +export interface ServerConfig extends Config { ssl: boolean; host: string; port: number; diff --git a/src/config/universal-config.interface.ts b/src/config/universal-config.interface.ts index 2d92564e52..40df23f5eb 100644 --- a/src/config/universal-config.interface.ts +++ b/src/config/universal-config.interface.ts @@ -1,4 +1,6 @@ -export interface UniversalConfig { +import { Config } from './config.interface'; + +export interface UniversalConfig extends Config { preboot: boolean, async: boolean, time: boolean