Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fernandohu/122e88c3bcd210bbe41c608c36306db9 to your computer and use it in GitHub Desktop.
Save fernandohu/122e88c3bcd210bbe41c608c36306db9 to your computer and use it in GitHub Desktop.
Reading configuration files before application startup in Angular2 final release

Reading data before application startup in Angular 2

In this demonstration I will show you how to read data in Angular2 final release before application startup. You can use it to read configuration files like you do in other languages like Java, Python, Ruby, Php.

This is how the demonstration will load data:

a) It will read an env file named 'env.json'. This file indicates what is the current working environment. Options are: 'production' and 'development';

b) It will read a config JSON file based on what is found in env file. If env is "production", the file is 'config.production.json'. If env is "development", the file is 'config.development.json'.

All these reads will be done before Angular2 starts up the application.

It assumes you already have a working application, with a module and everything set up.

In Your Module

Open your existing module and add the following two lines to your list of providers.

import { APP_INITIALIZER } from '@angular/core';
import { AppConfig }       from './app.config';
import { HttpModule }      from '@angular/http';

...

@NgModule({
    imports: [
        ...
        HttpModule
    ],
    ...
    providers: [
        ...
        AppConfig,
        { provide: APP_INITIALIZER, useFactory: (config: AppConfig) => () => config.load(), deps: [AppConfig], multi: true }
    ],
    ...
});

The first line makes AppConfig class available to Angular2.

The second line uses APP_INITIALIZER to execute Config.load() method before application startup. The 'multi: true' is being used because an application can have more than one line of APP_INITIALIZER.

Make sure you set "HttpModule" in "imports" section if you want to make http calls using Angular2 built in Http library.

In app.config.ts

Create a class AppConfig and name the file 'app.config.ts' (you can use a name of your choice).

This is the place we will do the reading of env and config files. The data of both files will be stored in the class so we can retrieve it later.

Note that native Angular Http library is used to read the json files.

import { Inject, Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Observable } from 'rxjs/Rx';

@Injectable()
export class AppConfig {

    private config: Object = null;
    private env:    Object = null;

    constructor(private http: Http) {

    }

    /**
     * Use to get the data found in the second file (config file)
     */
    public getConfig(key: any) {
        return this.config[key];
    }

    /**
     * Use to get the data found in the first file (env file)
     */
    public getEnv(key: any) {
        return this.env[key];
    }

    /**
     * This method:
     *   a) Loads "env.json" to get the current working environment (e.g.: 'production', 'development')
     *   b) Loads "config.[env].json" to get all env's variables (e.g.: 'config.development.json')
     */
    public load() {
        return new Promise((resolve, reject) => {
            this.http.get('env.json').map( res => res.json() ).catch((error: any):any => {
                console.log('Configuration file "env.json" could not be read');
                resolve(true);
                return Observable.throw(error.json().error || 'Server error');
            }).subscribe( (envResponse) => {
                this.env = envResponse;
                let request:any = null;

                switch (envResponse.env) {
                    case 'production': {
                        request = this.http.get('config.' + envResponse.env + '.json');
                    } break;

                    case 'development': {
                        request = this.http.get('config.' + envResponse.env + '.json');
                    } break;

                    case 'default': {
                        console.error('Environment file is not set or invalid');
                        resolve(true);
                    } break;
                }

                if (request) {
                    request
                        .map( res => res.json() )
                        .catch((error: any) => {
                            console.error('Error reading ' + envResponse.env + ' configuration file');
                            resolve(error);
                            return Observable.throw(error.json().error || 'Server error');
                        })
                        .subscribe((responseData) => {
                            this.config = responseData;
                            resolve(true);
                        });
                } else {
                    console.error('Env config file "env.json" is not valid');
                    resolve(true);
                }
            });

        });
    }
}

See that we used resolve() in all scenarios because we don't want the application to crash if any problem is found in the configuration files. If you prefer, you can set error scenarios to reject().

In env.json

This is the place you will configure the current development environment. Allowed values are 'development' and 'production'.

{
    "env": "development"
}

You may add this file to .gitignore to your convenience.

In config.development.json

This is the place you will configure development config variables. You can add as many variables you want in this JSON file.

{
    "host": "localhost"
}

You may add this file to .gitignore to your convenience.

In config.production.json

This is the place you will write production config variables. You can add as many variables you want in this JSON file.

{
    "host": "112.164.12.21"
}

You may add this file to .gitignore to your convenience.

In Any Angular2 class

Example of how we read the values previously loaded from both files. In this case, we are reading the 'host' variable from config file and 'env' from the env file.

import { AppConfig } from './app.config';

export class AnyClass {
    constructor(private config: AppConfig) {
        // note that AppConfig is injected into a private property of AnyClass
    }
    
    myMethodToGetHost() {
        // will print 'localhost'
        let host:string = config.get('host');
    }
    
    myMethodToGetCurrentEnv() {
        // will print 'development'
        let env: string = config.getEnv('env');
    }
}
@kavinda1995
Copy link

kavinda1995 commented May 16, 2019

@skygan @lucasklaassen @pchiarato and anyone who using using http interceptor.

Follow this way.
https://stackoverflow.com/a/49013534/6686446

The way this is happening is Http Interceptors sit between the HttpClient interface and the HttpBackend. So creating a HttpClient using HttpBackend is bypassing the http interceptor

@dabbid
Copy link

dabbid commented Aug 16, 2019

@schmorrison, I was stuck on the same problem (I've got several configurable modules), I found an alternative way to load a configuration file before bootstrap:

// src/shared/config/config.model.ts
export interface ExtendedWindow extends Window {
  appConfig: Config;
}

interface Config {
  someProperty: string;
  someOtherProperty: boolean;
  modules: any;
}
// main.ts
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
import { ExtendedWindow, Config } from './src/shared/config/config.model';

fetch('./config.json')
  .then((response: Response) => response.json())
  .then((config: Config) => {
    (window as ExtendedWindow).appConfig = config;
    platformBrowserDynamic()
      .bootstrapModule(AppModule, {})
      .catch(err => console.log(err));
  });
// src/shared/config/config.provider.ts
import { InjectionToken } from '@angular/core';
import { ExtendedWindow, Config } from './config.model';

export const APP_CONFIG: InjectionToken<Config> = new InjectionToken<Config>('AppConfig');

const appConfigFactory = (): Config => (window as ExtendedWindow).appConfig;

export const appConfigProvider: any = { provide: APP_CONFIG, useFactory: appConfigFactory };

export const configurableModuleFactory = (name: string) => (appConfig: Config): any => appConfig.modules[name];
// src/app.module.ts
import { NgModule } from '@angular/core';
// both configurable modules exposes a configuration injection token
// so I can define a factory provider for these (https://angular.io/guide/dependency-injection-providers#factory-providers)
import { FooConfigurableModule, FOO_CONFIGURABLE_MODULE_CONFIGURATION } from '@scope/foo';
import { BarConfigurableModule, BAR_CONFIGURABLE_MODULE_CONFIGURATION } from '@scope/bar';

import { AppComponent } from './app.component';
import { APP_CONFIG, appConfigProvider, configurableModuleFactory } from './shared/config/config.provider';

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    FooConfigurableModule.withConfig(null),
    BarConfigurableModule.withConfig(null),
  ],
  providers: [
    appConfigProvider,
    {
      provide: FOO_CONFIGURABLE_MODULE_CONFIGURATION,
      useFactory: configurableModuleFactory('foo'),
      deps: [APP_CONFIG],
    },
    {
      provide: BAR_CONFIGURABLE_MODULE_CONFIGURATION,
      useFactory: configurableModuleFactory('bar'),
      deps: [APP_CONFIG],
    },
  ],
  bootstrap: [AppComponent],
})
export class AppModule {}

@ayyash
Copy link

ayyash commented Aug 20, 2019

@dabbid oh wow, may be we're over killing it? may be environment rebuild isn't such a bad idea after all :)

@dabbid
Copy link

dabbid commented Aug 20, 2019

Believe me @ayyash, if I could have done without runtime config, I'd be happy... But it's imposed by my company's context.

@ayyash
Copy link

ayyash commented Aug 20, 2019

@schmorrison that is a very easy way to do it, unfortunately, it doesn't work on ssr. Unless you rewrite the firebaseConfig in server.ts

@vitaliidasaev
Copy link

vitaliidasaev commented Jun 18, 2020

There is example here:
How to initialize msal configurations using APP_INITIALIZER
AzureAD/microsoft-authentication-library-for-js#1403

@sforsandeep
Copy link

Sorry to ask a dumb question... because I am just a beginner in Angular. Is it same in Angular 11 or any easier solution already integrated ??

@ayyash
Copy link

ayyash commented May 17, 2021

I don't think there is anything new to how to initialize config from server in angular 11, but then again, may be i curled into a comfort zone and didn't try

@mohitp22
Copy link

I am also reading config.json in my web component to get API endpoint since path of config json is relative it’s taking path of application using my web component
I already tried by passing @input variable to webcomponent but since it’s changing the way currently other application using it I can’t use this approach

need suggestions
Thanks

@ayyash
Copy link

ayyash commented Jul 24, 2021

By the way, in Angular 12, what changed is that now you no longer need a promise, you can return an observable directly from load function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment