Created
June 5, 2017 14:26
-
-
Save anteburazer/a12a8e090f8aa46da5ffb728703bd042 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Injectable() | |
export class ConfigService { | |
private config: Object | |
private env: Object | |
constructor(private http: Http) {} | |
/** | |
* Loads the environment config file first. Reads the environment | |
* variable from the file and based on that loads the appropriate | |
* configuration file - development or production | |
*/ | |
load() { | |
return new Promise((resolve, reject) => { | |
this.http.get('/config/env.json') | |
.map(res => res.json()) | |
.subscribe((env_data) => { | |
this.env = env_data; | |
this.http.get('/config/' + env_data.env + '.json') | |
.map(res => res.json()) | |
.catch((error: any) => { | |
return Observable.throw(error.json().error || 'Server error'); | |
}) | |
.subscribe((data) => { | |
this.config = data; | |
resolve(true); | |
}); | |
}); | |
}); | |
} | |
/** | |
* Returns environment variable based on given key | |
* | |
* @param key | |
*/ | |
getEnv(key: any) { | |
return this.env[key]; | |
} | |
/** | |
* Returns configuration value based on given key | |
* | |
* @param key | |
*/ | |
get(key: any) { | |
return this.config[key]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment