-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Frontend exchange configuration with backend
- Loading branch information
Showing
11 changed files
with
330 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,24 @@ | ||
import { ApplicationConfig, importProvidersFrom } from '@angular/core'; | ||
import {APP_INITIALIZER, ApplicationConfig, importProvidersFrom} from '@angular/core'; | ||
import { provideRouter } from '@angular/router'; | ||
import { routes } from './app.routes'; | ||
import {provideAnimations} from "@angular/platform-browser/animations"; | ||
import {FirebirdConfigService} from "./firebird-config.service"; | ||
import {provideHttpClient, withFetch} from "@angular/common/http"; | ||
|
||
export const appConfig: ApplicationConfig = { | ||
providers: [ | ||
provideRouter(routes), | ||
provideAnimations() | ||
provideAnimations(), | ||
provideHttpClient(withFetch()), | ||
{ | ||
provide: APP_INITIALIZER, | ||
useFactory: configInitializer, | ||
deps: [FirebirdConfigService], | ||
multi: true | ||
} | ||
] | ||
}; | ||
|
||
export function configInitializer(configService: FirebirdConfigService): () => Promise<any> { | ||
return () => configService.loadConfig(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; | ||
import { FirebirdConfigService } from './firebird-config.service'; | ||
import * as jsoncParser from 'jsonc-parser'; | ||
|
||
describe('FirebirdConfigService', () => { | ||
let service: FirebirdConfigService; | ||
let httpMock: HttpTestingController; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [HttpClientTestingModule], | ||
providers: [FirebirdConfigService] | ||
}); | ||
service = TestBed.inject(FirebirdConfigService); | ||
httpMock = TestBed.inject(HttpTestingController); | ||
}); | ||
|
||
afterEach(() => { | ||
httpMock.verify(); // Ensure that no requests are outstanding. | ||
}); | ||
// | ||
it('should fetch and parse JSONC data correctly', () => { | ||
const dummyConfig = { serverPort: 5001 }; | ||
const jsoncData = '{ "key": "value" // comment }'; | ||
|
||
service.loadConfig().then(()=>{ | ||
expect(service.config.serverPort).toBe(5001); | ||
}); | ||
|
||
// Set up the HttpTestingController | ||
const req = httpMock.expectOne(service['configUrl']); | ||
expect(req.request.method).toBe('GET'); | ||
req.flush('{ "serverPort": 5001, "useAuthentication": true, "logLevel": "info" }'); // Mock the HTTP response | ||
|
||
}); | ||
// | ||
// it('should handle HTTP errors gracefully', () => { | ||
// const errorResponse = new ErrorEvent('Network error'); | ||
// | ||
// service.getConfig().subscribe({ | ||
// next: config => fail('should have failed with the network error'), | ||
// error: error => expect(error).toBeTruthy(), | ||
// complete: () => fail('The request should not complete successfully') | ||
// }); | ||
// | ||
// }); | ||
}); | ||
|
||
|
||
// import { TestBed } from '@angular/core/testing'; | ||
// | ||
// import { FirebirdConfigService } from './firebird-config.service'; | ||
// | ||
// describe('FirebirdConfigService', () => { | ||
// let service: FirebirdConfigService; | ||
// | ||
// beforeEach(() => { | ||
// TestBed.configureTestingModule({}); | ||
// service = TestBed.inject(FirebirdConfigService); | ||
// }); | ||
// | ||
// it('should be created', () => { | ||
// expect(service).toBeTruthy(); | ||
// }); | ||
// }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { HttpClient } from '@angular/common/http'; | ||
import * as jsoncParser from 'jsonc-parser'; | ||
import {deepCopy} from "./utils/deep-copy"; | ||
import {BehaviorSubject, Observable, catchError, map, of, firstValueFrom} from "rxjs"; | ||
|
||
|
||
export interface FirebirdConfig { | ||
serverPort: number; | ||
serverHost: string; | ||
servedByPyrobird: boolean; | ||
apiAvailable: boolean; | ||
useAuthentication: boolean; | ||
logLevel: string; | ||
} | ||
|
||
export const defaultFirebirdConfig: FirebirdConfig = { | ||
serverPort: 5454, | ||
serverHost: "localhost", | ||
apiAvailable: false, | ||
servedByPyrobird: false, | ||
useAuthentication: true, | ||
logLevel: 'info' | ||
}; | ||
|
||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class FirebirdConfigService { | ||
private configUrl = 'assets/config.jsonc'; // URL to the JSONC config file | ||
private _config = deepCopy(defaultFirebirdConfig); | ||
|
||
private triedLoading = false; | ||
|
||
constructor(private http: HttpClient) {} | ||
|
||
get config(): FirebirdConfig { | ||
if (!this.triedLoading) { | ||
this.triedLoading = true; | ||
console.error("Client called config while config is not loaded.") | ||
} | ||
return this._config; | ||
} | ||
|
||
async loadConfig(): Promise<void> { | ||
try { | ||
|
||
const jsoncData = await firstValueFrom( | ||
this.http.get(this.configUrl, { responseType: 'text' }) | ||
); | ||
const loadedConfig = this.parseConfig(jsoncData); | ||
|
||
// Merge loadedConfig over default config | ||
this._config = { ...defaultFirebirdConfig, ...loadedConfig }; | ||
} catch (error) { | ||
console.error(`Failed to load config: ${error}`); | ||
console.log(`Default config will be used`); | ||
} finally { | ||
this.triedLoading = true; | ||
} | ||
} | ||
|
||
|
||
private parseConfig(jsoncData: string): Partial<FirebirdConfig> { | ||
try { | ||
return jsoncParser.parse(jsoncData); | ||
} catch (parseError) { | ||
console.error('Error parsing JSONC data', parseError); | ||
return {}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.