blob: 75b6da41d83973e863e980f3dd2b3ff433ab0ac4 [file] [log] [blame]
/********************************************************************************
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
********************************************************************************/
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { Http, Headers, RequestOptions } from '@angular/http';
@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) => {
const headers = new Headers({
Accept: 'application/json',
'Content-Type': 'application/json',
DataType: 'application/json'
});
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];
}
}