diff --git a/src/app/core/_services/shared/config.service.ts b/src/app/core/_services/shared/config.service.ts index 0eb9facdc..a0eef6328 100644 --- a/src/app/core/_services/shared/config.service.ts +++ b/src/app/core/_services/shared/config.service.ts @@ -1,23 +1,53 @@ -import { environment } from 'src/environments/environment'; - +import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; +import { Observable } from 'rxjs'; +import { environment } from 'src/environments/environment'; @Injectable({ providedIn: 'root' }) export class ConfigService { - /** - * Get the API endpoint from the environment configuration - * @returns {string} The API endpoint URL - */ - public getEndpoint(): string { - let apiEndpoint = environment.config.prodApiEndpoint; + private configUrl = 'assets/config.json'; + constructor(private http: HttpClient) { } - // Remove trailing slash for URL consistency - if (apiEndpoint.endsWith('/')) { - apiEndpoint = apiEndpoint.slice(0, -1); + private getConfigOnce(): Observable { + return this.http.get(this.configUrl); + } + + public refreshEndpoint(){ + let ApiEndPoint = ""; + this.getConfigOnce().subscribe(config => { + // Assuming the JSON contains a property named 'backendUrl' + if (config && config.hashtopolis_backend_url) { + // If we get a config from the backend, we set the config property + ApiEndPoint = config.hashtopolis_backend_url; + // Remove trailing slash, because this causing invalid URLs + if (ApiEndPoint.endsWith('/')) { + ApiEndPoint = ApiEndPoint.slice(0, -1); + } + localStorage.setItem('prodApiEndpoint', ApiEndPoint); + } else if (config && !config.hashtopolis_backend_url) { + console.error('Invalid configuration file. Please check your config.json. Using defaults.'); + ApiEndPoint = environment.config.prodApiEndpoint; + localStorage.setItem('prodApiEndpoint', ApiEndPoint); + } else { + ApiEndPoint = environment.config.prodApiEndpoint; + localStorage.setItem('prodApiEndpoint', ApiEndPoint); + } + }, + error => { + ApiEndPoint = environment.config.prodApiEndpoint; + localStorage.setItem('prodApiEndpoint', ApiEndPoint); } + ); + } - return apiEndpoint; + public getEndpoint(){ + let ApiEndPoint = localStorage.getItem('prodApiEndpoint'); + if (!ApiEndPoint) { + this.refreshEndpoint(); + } + ApiEndPoint = localStorage.getItem('prodApiEndpoint'); + return ApiEndPoint; } } diff --git a/src/assets/config.json.example b/src/assets/config.json.example new file mode 100644 index 000000000..d4247d37d --- /dev/null +++ b/src/assets/config.json.example @@ -0,0 +1,3 @@ +{ + "hashtopolis_backend_url": "${HASHTOPOLIS_BACKEND_URL}" +}