runtime.exs is evaluated when the system starts, before the application runs. It converts runtime-specific inputs such as OS environment variables into application settings, avoiding hard-coded deployment values. section 11.4.1
Configuring an HTTP port
Read TODO_HTTP_PORT with a default of 5454, convert it to an integer, and store it as :http_port for :todo. Todo.Web can retrieve it with Application.fetch_env!/2; missing configuration raises an error. section 11.4.1
Listing 11.14 Configuring the HTTP port (todo_env/config/runtime.exs)
import Config #1
http_port = System.get_env("TODO_HTTP_PORT", "5454") #2
config :todo, http_port: String.to_integer(http_port) #3
Shows the compact runtime.exs pipeline from OS environment variable to typed application setting. section 11.4.1
Use runtime configuration to:
- Select different defaults and OS variables according to config_env(), such as separate development and test ports. section 11.4.2
- Keep test data isolated by varying settings such as the database folder. section 11.4.2
- Prefer runtime.exs over config.exs when values depend on the machine that runs the system; config.exs executes before compilation. section 11.4.3
