-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (config): added confiuration service
- added configuration service for making config changeable after build. - removed config from static environment
- Loading branch information
Mario Lukas
committed
Nov 26, 2019
1 parent
70e43b4
commit 19d3105
Showing
13 changed files
with
169 additions
and
40 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { Inject, Injectable } from '@angular/core'; | ||
import {Http,Response} from '@angular/http' | ||
import { Observable } from 'rxjs'; | ||
import { map } from 'rxjs/operators'; | ||
import { catchError } from 'rxjs/operators'; | ||
|
||
@Injectable() | ||
export class AppConfig { | ||
|
||
private config: Object = null; | ||
private env: Object = null; | ||
|
||
constructor(private http: Http) { | ||
} | ||
|
||
/** | ||
* Use to get the data found in the second file (config file) | ||
*/ | ||
public getConfig(key: any) { | ||
return this.config[key]; | ||
} | ||
|
||
/** | ||
* Use to get the data found in the first file (env file) | ||
*/ | ||
public getEnv(key: any) { | ||
return this.env[key]; | ||
} | ||
|
||
/** | ||
* This method: | ||
* a) Loads "env.json" to get the current working environment (e.g.: 'production', 'development') | ||
* b) Loads "config.[env].json" to get all env's variables (e.g.: 'config.development.json') | ||
*/ | ||
public load() { | ||
return new Promise((resolve, reject) => { | ||
this.http.get('./assets/config/env.json') | ||
.pipe(map( res => res.json())) | ||
.pipe( catchError(err => { | ||
console.log('Handling error locally and rethrowing it...', err); | ||
return Observable.throw(err.json().error || 'Server error'); | ||
})).subscribe( (envResponse) => { | ||
this.env = envResponse; | ||
let request: any = null; | ||
|
||
switch (envResponse.env) { | ||
case 'production': { | ||
request = this.http.get('./assets/config/config.' + envResponse.env + '.json'); | ||
} break; | ||
|
||
case 'development': { | ||
request = this.http.get('./assets/config/config.' + envResponse.env + '.json'); | ||
console.log('calling config dev'); | ||
} break; | ||
|
||
case 'default': { | ||
console.error('Environment file is not set or invalid'); | ||
resolve(true); | ||
} break; | ||
} | ||
|
||
if (request) { | ||
request.pipe( | ||
map((res: Response) => ( | ||
res.json() | ||
))) | ||
.subscribe((responseData: any) => { | ||
this.config = responseData; | ||
console.log(responseData); | ||
resolve(true); | ||
}); | ||
} else { | ||
console.error('Env config file "env.json" is not valid'); | ||
resolve(true); | ||
} | ||
}); | ||
|
||
}); | ||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
snapcast: { | ||
ip: '127.0.0.1', | ||
port: 1780 | ||
}, | ||
mopidy: [ | ||
{ | ||
ip: '127.0.0.1', | ||
port: 6681, | ||
id: 'STREAM1' | ||
}, | ||
{ | ||
ip: '127.0.0.1', | ||
port: 6682, | ||
id: 'STREAM2' | ||
} | ||
] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
snapcast: { | ||
ip: '127.0.0.1', | ||
port: 1780 | ||
}, | ||
mopidy: [ | ||
{ | ||
ip: '127.0.0.1', | ||
port: 6681, | ||
id: 'STREAM1' | ||
}, | ||
{ | ||
ip: '127.0.0.1', | ||
port: 6682, | ||
id: 'STREAM2' | ||
} | ||
] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"env": "development" | ||
} |
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