Last active
January 14, 2025 13:47
-
-
Save imran3/92d86b1331146ef817ed03bfee0b5417 to your computer and use it in GitHub Desktop.
Runtime configuration for Angular apps
This file contains hidden or 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
loadConfig(): Promise<void> { | |
if (isPlatformBrowser(this.platformId)) { | |
return this.http | |
... | |
} | |
} |
This file contains hidden or 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
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, | |
}, | |
], | |
... | |
}) |
This file contains hidden or 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
{ | |
"appInsightsKey": "" | |
} |
This file contains hidden or 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
export class ApplicationInsightsService { | |
appInsights: ApplicationInsights; | |
constructor(private appConfigService: AppConfigService) { | |
const appInsightsKey = this.appConfigService.getConfig().appInsightsKey; | |
... | |
} | |
... | |
} |
This file contains hidden or 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
//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; | |
} | |
} |
This file contains hidden or 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
data: | |
app.config.json: |- | |
{{ toJson .Values.appConfigs | indent 4 }} |
This file contains hidden or 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
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" |
This file contains hidden or 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
"configurations": { | |
"test-env": { | |
"fileReplacements": [ | |
{ | |
"replace": "src/environments/environment.ts", | |
"with": "src/environments/environment.test.ts" | |
} | |
], | |
}, | |
... | |
} |
This file contains hidden or 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
//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', | |
}; |
This file contains hidden or 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
// Values.yaml | |
appConfigs: | |
appInsightsKey: '' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment