Skip to content

Instantly share code, notes, and snippets.

@imran3
Last active January 14, 2025 13:47
Show Gist options
  • Save imran3/92d86b1331146ef817ed03bfee0b5417 to your computer and use it in GitHub Desktop.
Save imran3/92d86b1331146ef817ed03bfee0b5417 to your computer and use it in GitHub Desktop.
Runtime configuration for Angular apps
loadConfig(): Promise<void> {
if (isPlatformBrowser(this.platformId)) {
return this.http
...
}
}
import { NgModule, APP_INITIALIZER } from '@angular/core';
export function initConfig(appConfig: AppConfigService) {
return () => appConfig.loadConfig();
}
@NgModule({
providers: [
{
provide: APP_INITIALIZER,
useFactory: initConfig,
deps: [AppConfigService],
multi: true,
},
],
...
})
{
"appInsightsKey": ""
}
export class ApplicationInsightsService {
appInsights: ApplicationInsights;
constructor(private appConfigService: AppConfigService) {
const appInsightsKey = this.appConfigService.getConfig().appInsightsKey;
...
}
...
}
//AppConfig.d.ts
export interface AppConfig {
appInsightsKey: string;
}
//AppConfigService.ts
@Injectable()
export class AppConfigService {
private config: AppConfig;
loaded = false;
constructor(private http: HttpClient) {}
loadConfig(): Promise<void> {
return this.http
.get<AppConfig>('/assets/app.config.json')
.toPromise()
.then(data => {
this.config = data;
this.loaded = true;
});
}
getConfig(): AppConfig {
return this.config;
}
}
data:
app.config.json: |-
{{ toJson .Values.appConfigs | indent 4 }}
template:
...
spec:
...
containers:
...
volumeMounts:
- name: "{{ include "< APP-NAME >.fullname" . }}-config"
mountPath: "/var/www/assets/app.config.json"
subPath: app.config.json
readOnly: true
volumes:
- name: "{{ include "< APP-NAME >.fullname" . }}-config"
configMap:
name: "{{ include "< APP-NAME >.fullname" . }}-config"
"configurations": {
"test-env": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.test.ts"
}
],
},
...
}
//environment.dev.ts
export const environment = {
appInsightKey: 'A Key',
};
//environment.test.ts
export const environment = {
appInsightKey: 'B Key',
};
//environment.prod.ts
export const environment = {
appInsightKey: 'C Key',
};
// Values.yaml
appConfigs:
appInsightsKey: ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment