Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APP_INITIALIZER alternative #1

Open
ccadieux opened this issue Jun 15, 2017 · 10 comments
Open

APP_INITIALIZER alternative #1

ccadieux opened this issue Jun 15, 2017 · 10 comments

Comments

@ccadieux
Copy link

We had similar requirements.

  • Devops wanted a per environment config.
  • Build once for all environments.
  • Prevent people checking in passwords and secrets.

We ended up using APP_INITIALIZER to load a json file with our configuration.
Here's a minimal example: https://embed.plnkr.co/295SxTOZEZUbiyrhAKh2/

Some other links
angular/angular#9047
https://stackoverflow.com/questions/39033835/angularjs2-preload-server-configuration-before-the-application-starts

@natchiketa
Copy link
Owner

@ccadieux That's a nice solution! The thing for me was that, since we do builds using something like a GoCD box, I wanted to use OS environment variables. But yeah, this approach might be more pragmatic. It certainly involves fewer lines of code! 🤘

@jjlorenzo
Copy link

In a current project (angular:frontend + django:api-backend) I use APP_INITIALIZER to load some info from the api backend server in this scenario APP_INITIALIZER works for me.

Nevertheless for what we are try to solving here https://medium.com/@natchiketa/angular-cli-and-os-environment-variables-4cfa3b849659 I fail to see why your approach with APP_INITIALIZER loading a local file is different from the regular conventions of angular cli:

"environmentSource": "environments/environment.ts",
"environments": {
  "dev": "environments/environment.ts",
  "prod": "environments/environment.prod.ts"
}

I've achieved this in the backend using https://github.com/joke2k/django-environ with good results, in this kind of projects where the app runs in the client and not in the server I prefer to solve this problem at build time, so looking at the built code I can see all env settings I'm exposing to the client.

Did I'm missing something? I'll like your opinions!

@jjlorenzo
Copy link

Probably I will end with something different since I'm using ansible, I can generate the env files with the proper settings for the env I'm building

@kenisteward
Copy link

@jjlorenzo I believe @ccadieux 's answer is was due to what he said in dev ops required that after the build is made no more new builds should be made.

If you go with the environment.ts method for each environment you must do a rebuild of the code to be able to properly get the values into the app.

I also use configuration.json in APP_INITIALIZER for the exact same reason. After the code is built, no new builds are made as it is being promoted to production.

@masimplo
Copy link

masimplo commented May 7, 2018

@ccadieux you mentioned that you do a single build using this approach. How is the final built code know which environment to load the config for? (i.e. hit uat.example.com/config vs app.example.com/config)

@jjlorenzo
Copy link

@kenisteward good catch, your'e right.

@ccadieux
Copy link
Author

ccadieux commented May 7, 2018

Basically, not much is in the environment file anymore. Everything that can change is in the config file for each environment.

@masimplo
Copy link

masimplo commented May 7, 2018

I am probably missing something. If I have 10 config values that are different between UAT and Production (so they are stored in either 2 different files or in 2 different urls), how does APP_INITIALIZER decide which of the two to load at runtime? Or do you always reference a single file in a specific location and change this file's contents depending on the environment.

The reason I am asking is because I would like to load the configuration from a different location depending on the environment. eg. config.prod.json and config.dev.json or example.com/dev/config.json and example.com/prod/config.json but this does not seem possible with this setup.

@jjlorenzo
Copy link

my 5 cents, add a property to every environment.ts with the env name, for example and use this value in the APP_INITIALIZER to determine the url.

@kenisteward
Copy link

kenisteward commented May 7, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants