Skip to content

Instantly share code, notes, and snippets.

@diyfr
Last active July 17, 2019 07:05
Show Gist options
  • Save diyfr/37ec3b8d103699a2dbdf69e31731eac9 to your computer and use it in GitHub Desktop.
Save diyfr/37ec3b8d103699a2dbdf69e31731eac9 to your computer and use it in GitHub Desktop.
[DRAFT] Angular load external environment properties

Use external configuration for your web app (docker, mount volume to /assets/config) Edit maitn.tsin main.ts :

environmentLoader("./assets/config/environment.json", environment).then(env => {
  environment.production = env.production;
  environment.settings = env.settings;
  // Default angular initialization
  if (environment.production) {
    enableProdMode();
  }
  // Example init Logger
  Logger.init(environment.settings.logLevel);
  
  // Default angular initialization
  platformBrowserDynamic().bootstrapModule(AppModule)
    .catch(err => console.error(err));
})

sample /assets/config

{
    "production": false,
    "settings": {
        "logLevel": "INFO",
        "backend": "http://test",
        "keycloakJsonUrl": "./assets/config/keycloak.json",
        "keycloakRedirectUri":"http://localhost:4200/secure"
    }
}
export const environmentLoader = function (path: string = "./assets/confi/environment.json", defaultEnvironment: any = {}): Promise<any> {
return new Promise<any>((resolve, reject) => {
var xmlhttp = new XMLHttpRequest(),
method = 'GET',
url = path;
xmlhttp.open(method, url, true);
xmlhttp.onload = function () {
if (xmlhttp.status === 200) {
resolve(JSON.parse(xmlhttp.responseText));
} else {
resolve(defaultEnvironment);
}
};
xmlhttp.send();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment