I have a URL that will change depending on if I'm in dev or prod
in dev, it should be http:localhost:3000
in prod, it should be https://www.foobar.com
- Where would I set the value of this environment variable?
- How would I need to update this snippet in my controller so that it uses this environment variable?
conn
|> redirect(external: "http://localhost:3000")
If you know this to be a compile-time variable, you can set it in
config/dev.exs,config/test.exsandconfig/prod.exs(or set it inconfig/config.exsand override it in the environments when it's different).If you need it to be configurable at runtime (i.e. something you can change after building a release with
mix release), you can useruntime.exsfor this instead.There's an example that I believe is generated for you by default:
This type of nested config is accessed with, say,
You can also do something like this:
In runtime.exs, if you really want to use an environment variable as the source of the URL, you may use
System.get_env("EXTERNAL_SUPPORT_SITE")as the value that you assign toWhich you can access in your example like this: