From 0fcc7cf54151fc3bdc7e0c2c5855342fb7af4562 Mon Sep 17 00:00:00 2001 From: cv5ch <176032962+cv5ch@users.noreply.github.com> Date: Thu, 4 Sep 2025 09:00:03 +0200 Subject: [PATCH] =?UTF-8?q?Revert=20"Removed=20config.json,=20Backend=20UR?= =?UTF-8?q?L=20is=20now=20always=20loaded=20via=20environment=E2=80=A6"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit a0a4c57f10d182306404e2a13b00f715d12d3498. --- .../core/_services/shared/config.service.ts | 54 ++++++++++++++----- src/assets/config.json.example | 3 ++ 2 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 src/assets/config.json.example 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}" +}