I am running Elixir 1.14 on my machine.
I have a config folder with runtime.exs, runtime.prod.exs, and runtime.dev.exs
I reference environment variables inside runtime.prod.exs and runtime.dev.exs.
Things seem to work well in development environment, but things begin to fail when I deploy into production.
The environment variables are not seen, although this is not the case in development environment.
I have tried to revert to config.exs, prod.exs, and dev.exs.
But that is also not working.
Below is a sample error when I attempt to run the application in production:
** (EXIT) an exception was raised:
** (ArgumentError) could not fetch application environment :host_port for application :my_app because configuration at :host_port was not set
Can I get some help on how I should go about the configuration?
I got this, runtime.exs, code from one of the sites:
##runtime.exs
import Config
case config_env() do
:dev ->
Code.require_file("runtime.dev.exs", "config")
:prod ->
Code.require_file("runtime.prod.exs", "config")
end
##runtime.dev.exs
config :my_app,
host_port: String.to_integer(System.fetch_env!("APP_PORT"))
##runtime.prod.exs
config:
host_port: String.to_integer(System.fetch_env!("APP_PORT"))
runtime.dev.exs works fine, but runtime.prod.exs does not.
Inside my application.ex file/module, I have this:
defmodule MyApp.Application do
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
@moduledoc false
use Application
def start(_type, _args) do
port=Application.fetch_env!(:my_app, :host_port)
# List all child processes to be supervised
children = [
]
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
Supervisor.start_link(children, opts)
end
end
The error I get is:
** (EXIT) an exception was raised:
** (ArgumentError) could not fetch application environment :host_port for application :my_app because configuration at :host_port was not set
How did you set the environment variables in production? If your runtime.exs does not see them then they were not there. Whatever ways you used to set them must have not worked.
Thanks to you all for your comments and suggestions.
I have been able to get the configurations to work now.
There was a wrong naming of the environment, prod, and so the environment variables were not being seen.
Does it mean the environment cannot change from dev or prod to any other name or even an additional environment added such as a staging environment, stag?