Skip to content

Commit

Permalink
Merge pull request #19 from NorskHelsenett/feature/make-config-loadin…
Browse files Browse the repository at this point in the history
…g-great-again

Easier loading of config, using  platformBrowserDynamic at startup instead
  • Loading branch information
rogerwesterbo authored Jan 14, 2025
2 parents 2bd291f + 311299e commit 8f31f6b
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 100 deletions.
Binary file modified bun.lockb
Binary file not shown.
4 changes: 2 additions & 2 deletions charts/ror-webapp/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
enabled: true
web:
# hosts is a list of hosts that the webapp should be available on
hosts:
hosts:
- host.ror.io
# apiUrl is the url of the ror api
apiUrl: https://api.ror.io
Expand Down Expand Up @@ -67,7 +67,7 @@ resources:
memory: 128Mi
autoscaling:
enabled: true
minReplicas: 2
minReplicas: 1
maxReplicas: 5
targetCPUUtilizationPercentage: 85
nodeSelector: {}
Expand Down
6 changes: 5 additions & 1 deletion src/app/core/models/config.ts → src/app.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export interface Config {
import { InjectionToken } from '@angular/core';

export const APP_CONFIG = new InjectionToken<AppConfig>('app.config');

export interface AppConfig {
auth: Auth;
regex: Regex;
rowsPerPage: number[];
Expand Down
8 changes: 0 additions & 8 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import localeEnExtra from '@angular/common/locales/extra/en';
import { CoreModule } from './core/core.modules';
import { SharedModule } from './shared/shared.module';
import { MessageService } from 'primeng/api';
import { ConfigService, configFactory } from './core/services/config.service';
import { provideHighlightOptions } from 'ngx-highlightjs';
import { InMemoryScrollingFeature, InMemoryScrollingOptions, withInMemoryScrolling } from '@angular/router';
import { environment } from '../environments/environment';
Expand Down Expand Up @@ -70,18 +69,11 @@ const inMemoryScrollingFeature: InMemoryScrollingFeature = withInMemoryScrolling
}),
],
providers: [
//provideRouter(routes, inMemoryScrollingFeature, withViewTransitions(), withDebugTracing()),
{
provide: HTTP_INTERCEPTORS,
useClass: AuthInterceptor,
multi: true,
},
{
provide: APP_INITIALIZER,
useFactory: configFactory,
multi: true,
deps: [ConfigService],
},
MessageService,
provideHighlightOptions({
lineNumbersOptions: {
Expand Down
33 changes: 20 additions & 13 deletions src/app/core/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,14 @@ import { jwtDecode } from 'jwt-decode';
})
export class AuthService {
authenticationEventObservable: Subject<boolean> = new Subject<boolean>();
authConfig: AuthConfig = {
issuer: this.configService.config.auth.issuer,
redirectUri: this.configService.config.auth.redirectUri,
clientId: this.configService.config.auth.clientId,
responseType: this.configService.config.auth.responseType,
scope: this.configService.config.auth.scope,
showDebugInformation: false,
timeoutFactor: 0.75,
postLogoutRedirectUri: this.configService.config.auth.postLogoutRedirectUri,
logoutUrl: this.configService.config.auth.logoutUrl,
requireHttps: this.configService.config.auth.requireHttps,
strictDiscoveryDocumentValidation: this.configService.config.auth.strictDiscoveryDocumentValidation,
};
authConfig: AuthConfig | undefined;

constructor(
private oauthService: OAuthService,
private userService: UserService,
private configService: ConfigService,
) {
this.setConfig();
this.oauthService.configure(this.authConfig);
}

Expand All @@ -53,6 +42,7 @@ export class AuthService {
this.authenticationEventObservable.next(result);
})
.catch((error: any) => {
console.error('Error logging in', error);
this.logout();
});

Expand All @@ -79,4 +69,21 @@ export class AuthService {
exp.setUTCSeconds(decodedToken.exp);
return now > exp;
}

private setConfig() {
const config = this.configService.config;
this.authConfig = {
issuer: config?.auth.issuer,
redirectUri: config?.auth?.redirectUri,
clientId: config?.auth?.clientId,
responseType: config?.auth?.responseType,
scope: config?.auth?.scope,
showDebugInformation: false,
timeoutFactor: 0.75,
postLogoutRedirectUri: config?.auth?.postLogoutRedirectUri,
logoutUrl: config?.auth?.logoutUrl,
requireHttps: config?.auth?.requireHttps,
strictDiscoveryDocumentValidation: config?.auth?.strictDiscoveryDocumentValidation,
};
}
}
79 changes: 9 additions & 70 deletions src/app/core/services/config.service.ts
Original file line number Diff line number Diff line change
@@ -1,79 +1,18 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable, catchError, map, of } from 'rxjs';
import { Config } from '../models/config';
import { environment } from '../../../environments/environment';

export const configFactory = (config: ConfigService) => {
return () => config.loadConfig();
};
import { Inject, Injectable } from '@angular/core';
import { APP_CONFIG, AppConfig } from '../../../app.config';

@Injectable({
providedIn: 'root',
})
export class ConfigService {
config: Config;

constructor(private httpClient: HttpClient) {
this.config = this.defaultConfig();
}

loadConfig(): Observable<boolean> {
return this.httpClient.get(environment.configPath).pipe(
map((response: any) => {
// do something to reflect into local model
this.config = this.createConfig(response);
return true;
}),
catchError((error: any) => {
this.config = this.defaultConfig();
console.error('Error loading config', error);
return of(false);
}),
);
}
config: AppConfig;

private createConfig(json: any): Config {
const config = { ...(<Config>json) };
config.auth.redirectUri = window.location.origin + config.auth.redirectUri;
config.auth.postLogoutRedirectUri = window.location.origin;
return config;
}

private defaultConfig(): Config {
const config: Config = {
auth: {
issuer: 'http://localhost:5556/dex',
clientId: 'ror.sky.test.nhn.no',
redirectUri: window.location.origin + '/auth/callback',
scope: 'profile email groups',
responseType: 'id_token token',
logoutUrl: window.location.origin,
postLogoutRedirectUri: window.location.origin,
requireHttps: false,
strictDiscoveryDocumentValidation: false,
},
regex: {
forms: `^[@()\\/:?\\r\\n.,a-zA-Z æøåÆØÅ0-9_-]+$`,
},
rorApi: 'https://ror.sky.test.nhn.no',
rows: 25,
rowsPerPage: [10, 25, 50, 75, 100],
sse: {
postfixUrl: '/v1/events/listen',
timeout: 30000,
method: 'GET',
},
};

if (environment.production) {
config.auth.clientId = 'ror.sky.test.nhn.no';
config.auth.issuer = 'https://auth.sky.nhn.no/dex';
config.rorApi = 'https://api.ror.nhn.no';
config.auth.requireHttps = true;
config.auth.strictDiscoveryDocumentValidation = true;
constructor(@Inject(APP_CONFIG) config: AppConfig) {
this.config = config;
if (!config) {
console.error('Config not loaded');
return;
}

return config;
this.config.auth.redirectUri = window.location.origin + this.config.auth.redirectUri;
}
}
21 changes: 15 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@ import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';

if (environment.production) {
enableProdMode();
}
import { APP_CONFIG } from './app.config';

platformBrowserDynamic()
.bootstrapModule(AppModule)
.catch((err) => console.error(err));
fetch(environment.configPath)
.then((response) => response.json())
.then((config) => {
if (environment.production) {
enableProdMode();
}
platformBrowserDynamic([{ provide: APP_CONFIG, useValue: config }])
.bootstrapModule(AppModule)
.catch((err) => console.error(err));
})
.catch((err) => {
console.error(err);
return 'Failed to load config';
});

0 comments on commit 8f31f6b

Please sign in to comment.