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