recursive merge of config

This commit is contained in:
William Welling
2017-09-26 19:13:32 -05:00
parent 35580b7c48
commit 26e2da93fb
7 changed files with 74 additions and 13 deletions

View File

@@ -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() {

View File

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

View File

@@ -1,4 +1,6 @@
export interface CacheConfig {
import { Config } from './config.interface';
export interface CacheConfig extends Config {
msToLive: number,
control: string
}

View File

@@ -0,0 +1,4 @@
// tslint:disable-next-line:no-empty-interface
export interface Config {
}

View File

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

View File

@@ -1,4 +1,6 @@
export interface ServerConfig {
import { Config } from './config.interface';
export interface ServerConfig extends Config {
ssl: boolean;
host: string;
port: number;

View File

@@ -1,4 +1,6 @@
export interface UniversalConfig {
import { Config } from './config.interface';
export interface UniversalConfig extends Config {
preboot: boolean,
async: boolean,
time: boolean