Skip to content

Instantly share code, notes, and snippets.

@anteburazer
Created June 5, 2017 14:26
Show Gist options
  • Save anteburazer/a12a8e090f8aa46da5ffb728703bd042 to your computer and use it in GitHub Desktop.
Save anteburazer/a12a8e090f8aa46da5ffb728703bd042 to your computer and use it in GitHub Desktop.
@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