Environment variables are always strings. If you wish to cast them to a different type, you need to do that manually.
Be careful about getting environment variables in your config.exs files, as they are evaluated during build time and will reflect the build environment. When you move to production, you probably want to be able to get the values at startup / runtime instead. I just wrote a blog post about the different places of configuration.
Great article! Thanks! It says don’t mix the env (build & runtime) however I can see there’re same configs for both. I assume they’ll be separated in their each env. When it releases it will be runtime, otherwise build time config.
And I couldn’t setup init method for my project. I guess it depends on the Phoenix version. I use v1.5.1. def init never called. I tried to setup application variables there like you did, but it didn’t work for me. So, I put the same configs for release (releases.exs).
I don’t know what is the reason of app env. variables are string but I hope one it will support other types as well, until that time I’ll use string. So, I did it like this
Not sure what you mean by this. The main thing to think about is that the normal config files are evaluated at build time, but config/releases.exs is evaluated at startup time.
I should probably clarify that in the article. The init functions are just a convention that many libraries use, but Phoenix does not use it. So that’s why it’s never called. The example uses Geolix which does allow setting an init function to be called at startup.
but it always sets string when access it from Application.get_env but you’re cause I don’t have that problem when I set Application env from test like this: Application.put_env(:coliving, :usage_logging_enabled, true)
In your config.exs (or a similar file) you are doing this:
config :foo, :bar, System.get_env("ENV")
As System.get_env/1 always returns a string (the system environment isn’t even able to store something else), Application.get_env(:foo, :bar) will of course return a string as well.
If you want to read a “boolean” from the enf, you need a little helper: