@@ -8,32 +8,46 @@ import { environment } from 'src/environments/environment';
88} )
99export class ConfigService {
1010 private configUrl = 'assets/config.json' ;
11-
1211 constructor ( private http : HttpClient ) { }
1312
1413 private getConfigOnce ( ) : Observable < any > {
1514 return this . http . get ( this . configUrl ) ;
1615 }
1716
17+ public refreshEndpoint ( ) {
18+ let ApiEndPoint = "" ;
19+ this . getConfigOnce ( ) . subscribe ( config => {
20+ // Assuming the JSON contains a property named 'backendUrl'
21+ if ( config && config . hashtopolis_backend_url ) {
22+ // If we get a config from the backend, we set the config property
23+ ApiEndPoint = config . hashtopolis_backend_url ;
24+ // Remove trailing slash, because this causing invalid URLs
25+ if ( ApiEndPoint . endsWith ( '/' ) ) {
26+ ApiEndPoint = ApiEndPoint . slice ( 0 , - 1 ) ;
27+ }
28+ localStorage . setItem ( 'prodApiEndpoint' , ApiEndPoint ) ;
29+ } else if ( config && ! config . hashtopolis_backend_url ) {
30+ console . error ( 'Invalid configuration file. Please check your config.json. Using defaults.' ) ;
31+ ApiEndPoint = environment . config . prodApiEndpoint ;
32+ localStorage . setItem ( 'prodApiEndpoint' , ApiEndPoint ) ;
33+ } else {
34+ ApiEndPoint = environment . config . prodApiEndpoint ;
35+ localStorage . setItem ( 'prodApiEndpoint' , ApiEndPoint ) ;
36+ }
37+ } ,
38+ error => {
39+ ApiEndPoint = environment . config . prodApiEndpoint ;
40+ localStorage . setItem ( 'prodApiEndpoint' , ApiEndPoint ) ;
41+ }
42+ ) ;
43+ }
44+
1845 public getEndpoint ( ) {
1946 let ApiEndPoint = localStorage . getItem ( 'prodApiEndpoint' ) ;
2047 if ( ! ApiEndPoint ) {
21- this . getConfigOnce ( ) . subscribe ( config => {
22- // Assuming the JSON contains a property named 'backendUrl'
23- if ( config && config . hashtopolis_backend_url ) {
24- // If we get a config from the backend, we set the config property
25- ApiEndPoint = config . hashtopolis_backend_url ;
26- localStorage . setItem ( 'prodApiEndpoint' , ApiEndPoint ) ;
27- } else if ( config && ! config . hashtopolis_backend_url ) {
28- console . error ( 'Invalid configuration file. Please check your config.json. Using defaults.' ) ;
29- ApiEndPoint = environment . config . prodApiEndpoint ;
30- localStorage . setItem ( 'prodApiEndpoint' , ApiEndPoint ) ;
31- } else {
32- ApiEndPoint = environment . config . prodApiEndpoint ;
33- localStorage . setItem ( 'prodApiEndpoint' , ApiEndPoint ) ;
34- }
35- } ) ;
48+ this . refreshEndpoint ( ) ;
3649 }
50+ ApiEndPoint = localStorage . getItem ( 'prodApiEndpoint' ) ;
3751 return ApiEndPoint ;
3852 }
3953}
0 commit comments