Back to home page

EIC code displayed by LXR

 
 

    


Warning, /firebird/firebird-ng/src/app/services/server-config.service.ts is written in an unsupported language. File is not indexed.

0001 import { Injectable } from '@angular/core';
0002 import { HttpClient } from '@angular/common/http';
0003 import * as jsoncParser from 'jsonc-parser';
0004 import {deepCopy} from "../utils/deep-copy";
0005 import {firstValueFrom} from "rxjs";
0006 
0007 
0008 export interface ServerConfig {
0009   servedByPyrobird: boolean;
0010   apiAvailable: boolean;
0011   apiBaseUrl: string;
0012   logLevel: string;
0013   configs: any[];
0014 }
0015 
0016 export const defaultFirebirdConfig: ServerConfig = {
0017   apiAvailable: false,
0018   apiBaseUrl: "",
0019   servedByPyrobird: false,
0020   logLevel: 'info',
0021   configs: []
0022 };
0023 
0024 
0025 @Injectable({
0026   providedIn: 'root'
0027 })
0028 export class ServerConfigService {
0029   private configUrl = 'assets/config.jsonc'; // URL to the JSONC config file
0030   private _config = deepCopy(defaultFirebirdConfig);
0031   private triedLoading = false;
0032 
0033   constructor(private http: HttpClient) {}
0034 
0035   get config(): ServerConfig {
0036     if (!this.triedLoading) {
0037       this.triedLoading = true;
0038       console.error("[ServerConfigService] config() is called while config is not loaded")
0039     }
0040     return this._config;
0041   }
0042 
0043   async loadConfig(): Promise<void> {
0044     try {
0045 
0046       const jsoncData = await firstValueFrom(
0047         this.http.get(this.configUrl, { responseType: 'text' })
0048       );
0049       const loadedConfig = this.parseConfig(jsoncData);
0050 
0051       // Merge loadedConfig over default config
0052       this._config = { ...defaultFirebirdConfig, ...loadedConfig };
0053       console.log("[ServerConfigService] Server config loaded file");
0054       console.log(`[ServerConfigService] Subsystems configs loaded: ${this._config?.configs?.length}`);
0055     } catch (error) {
0056       console.error(`Failed to load config: ${error}`);
0057       console.log(`[ServerConfigService] Default config will be used`);
0058     } finally {
0059       this.triedLoading = true;
0060     }
0061   }
0062 
0063   private parseConfig(jsoncData: string): Partial<ServerConfig> {
0064     try {
0065       return jsoncParser.parse(jsoncData);
0066     } catch (parseError) {
0067       console.error('Error parsing JSONC data', parseError);
0068       return {};
0069     }
0070   }
0071 
0072   /**
0073    * Sets the configuration - intended for use in unit tests only.
0074    * This method is safeguarded to be operational only in non-production environments.
0075    */
0076   public setUnitTestConfig(value: Partial<ServerConfig>) {
0077     this.triedLoading = true;
0078     this._config = {...defaultFirebirdConfig, ...value};
0079   }
0080 }