Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

"remoteEnv": {
"NODE_OPTIONS": "--use-openssl-ca",
"HASHTOPOLIS_BACKEND_URL": "http://localhost:8080"
},
"overrideCommand": false,
"remoteUser": "node"
Expand Down
89 changes: 61 additions & 28 deletions src/app/core/_services/shared/config.service.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,86 @@
import { Observable, of } from 'rxjs';
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';

/**
* Interface for the expected shape of `assets/config.json`.
*/
interface AppConfig {
hashtopolis_backend_url?: string;
}

@Injectable({
providedIn: 'root'
})
export class ConfigService {
private configUrl = 'assets/config.json';
constructor(private http: HttpClient) { }

private getConfigOnce(): Observable<any> {
constructor(private http: HttpClient) {}

/**
* Retrieves the configuration file once from the assets folder.
*
* - In **production**, this fetches `assets/config.json` (created at container startup).
* - In **development**, this bypasses the HTTP request and just returns `null`,
* because dev uses `environment.ts` instead of `config.json`.
*
* @returns An observable of the parsed JSON config (prod) or `null` (dev).
*/
private getConfigOnce(): Observable<AppConfig> {
if (!environment.production) {
return of(null);
}
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);
/**
* Refreshes the API endpoint configuration.
*
* - In **production**, it tries to read `hashtopolis_backend_url` from `config.json`.
* If found, it is saved to `localStorage`.
* If missing or invalid, falls back to `environment.config.prodApiEndpoint`.
*
* - In **development**, it always uses `environment.config.prodApiEndpoint`
* and stores it in `localStorage`.
*/
public refreshEndpoint(): void {
let ApiEndPoint = '';
this.getConfigOnce().subscribe({
next: (config) => {
if (config && config.hashtopolis_backend_url) {
ApiEndPoint = config.hashtopolis_backend_url;
if (ApiEndPoint.endsWith('/')) {
ApiEndPoint = ApiEndPoint.slice(0, -1);
}
} else {
ApiEndPoint = environment.config.prodApiEndpoint;
}
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 {
},
error: () => {
ApiEndPoint = environment.config.prodApiEndpoint;
localStorage.setItem('prodApiEndpoint', ApiEndPoint);
}
},
error => {
ApiEndPoint = environment.config.prodApiEndpoint;
localStorage.setItem('prodApiEndpoint', ApiEndPoint);
}
);
});
}

public getEndpoint(){
/**
* Retrieves the currently configured API endpoint.
*
* - If an endpoint is already stored in `localStorage`, returns it.
* - If not, it triggers `refreshEndpoint()` to initialize it and then returns
* either the stored value or falls back to `environment.config.prodApiEndpoint`.
*
* @returns The active API endpoint as a string.
*/
public getEndpoint(): string {
let ApiEndPoint = localStorage.getItem('prodApiEndpoint');
if (!ApiEndPoint) {
this.refreshEndpoint();
ApiEndPoint = localStorage.getItem('prodApiEndpoint');
}
ApiEndPoint = localStorage.getItem('prodApiEndpoint');
return ApiEndPoint;
return ApiEndPoint ?? environment.config.prodApiEndpoint;
}
}