zoedsoupe
Ok, I’ve an app that I intend to use releases on future, so I decided to create a runtime.exs, that means: generic configuration file that is evaluated on runtime on every env, correct?
Also, as a Phoenix app, we have config.exs, a config file that is evaluated at compile time, correct?
Doubt number one: which one come first: runtime or compile time?
My problem:
This is my config.exs:
import Config
config :lovelace,
ecto_repos: [Lovelace.Repo],
generators: [binary_id: true]
# Configures the endpoint
config :lovelace, LovelaceWeb.Endpoint,
url: [host: "localhost"],
secret_key_base: "+41x5h8Xbn3ilu0YZ5UShcHI2/qhY3JGZpT8ockkzdkTHahMJe177aE2dcyQ5CAn",
render_errors: [view: LovelaceWeb.ErrorView, accepts: ~w(html json), layout: false],
pubsub_server: Lovelace.PubSub,
live_view: [signing_salt: "XP1985o+"]
# Configures Elixir's Logger
config :logger, :console,
format: "$time $metadata[$level] $message\n",
metadata: [:request_id]
# Use Jason for JSON parsing in Phoenix
config :phoenix, :json_library, Jason
config :tesla, adapter: Tesla.Adapter.Hackney
config :lovelace, pubsub_channel: Lovelace.PubSub
# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{config_env()}.exs"
And this is my runtime.exs:
import Config
get_env_var = fn var_name, default ->
value = System.get_env(var_name)
cond do
default != :none ->
default
is_nil(value) or value == "" ->
raise """
Environment variable #{var_name} is missing!
"""
true ->
value
end
end
# server config
app_host = get_env_var.("HOST", "localhost")
app_port = get_env_var.("PORT", "4000") |> String.to_integer()
# general config
config :lovelace, bot_name: "lovelace"
if config_env() == :prod do
# bot config
bot_token = get_env_var.("BOT_TOKEN", :none)
# database config
db_url = get_env_var.("DB_URL", :none)
pool_size = get_env_var.("POOL_SIZE", "2") |> String.to_integer()
# server config
secret_key_base = get_env_var.("SECRET_KEY_BASE", :none)
config :lovelace, bot_token: bot_token
config :lovelace, Lovelace.Repo,
url: db_url,
pool_size: pool_size
config :lovelace, Lovelace.Endpoint,
server: true,
url: [host: app_host, port: app_port],
http: [
port: app_port,
transport_options: [socket_opts: [:inet6]]
],
secret_key_base: secret_key_base
end
When I run:
MIX_ENV=prod DB_URL=xxxxx SECRET_KEY_BASE=xxxxx BOT_TOKEN=xxxx PORT=4000 mix phx.server
where “xxxx…” == some real config,
I receive this output:
21:54:54.910 [info] Starting Elixir.LovelaceIntegration.Telegram.Consumers.MessageHandler
21:54:55.071 [info] Access LovelaceWeb.Endpoint at http://localhost
So, the server don’t really starts…
When I execute the same command, but using iex -S mix, and then running Application.get_all_env :lovelace, I receive this result:
iex(1)> Application.get_all_env :lovelace
[
{Lovelace.Repo,
[
url: "xxxxxx",
pool_size: 2
]},
{:pubsub_channel, Lovelace.PubSub},
{:ecto_repos, [Lovelace.Repo]},
{:bot_token, "xxxxx"},
{:bot_name, "lovelace"},
{Lovelace.Endpoint,
[
server: true,
url: [host: "localhost", port: 4000],
http: [port: 4000, transport_options: [socket_opts: [:inet6]]],
secret_key_base: "xxxxxx"
]},
{:generators, [binary_id: true]},
{LovelaceWeb.Endpoint,
[
url: [host: "localhost"],
secret_key_base: "xxxxx",
render_errors: [
view: LovelaceWeb.ErrorView,
accepts: ["html", "json"],
layout: false
],
pubsub_server: Lovelace.PubSub,
live_view: [signing_salt: "XP1985o+"]
]}
]
Again, “xxxx” == sensity info
So, it seems that the config.exs and runtime.exs configs, actually, only the LovelaceWeb.Endpoint config, isn’t being merged, so the last one (config.exs) is being evaluated.
If I run mix phx.server, on dev env, server starts correctly:
Generated lovelace app
[info] Starting Elixir.LovelaceIntegration.Telegram.Consumers.MessageHandler
[info] Running LovelaceWeb.Endpoint with cowboy 2.8.0 at 0.0.0.0:4000 (http)
[info] Access LovelaceWeb.Endpoint at http://localhost:4000
webpack is watching the files…
[hardsource:321e22a3] Using 1 MB of disk space.
[hardsource:321e22a3] Tracking node dependencies with: yarn.lock.
[hardsource:321e22a3] Reading from cache 321e22a3...
Hash: 59927b50d778eaad2717
Version: webpack 4.41.5
Time: 212ms
Built at: 02/16/2021 10:02:48 PM
Asset Size Chunks Chunk Names
../css/app.css 12.9 KiB app [emitted] app
../favicon.ico 1.23 KiB [emitted]
../images/phoenix.png 13.6 KiB [emitted]
../robots.txt 202 bytes [emitted]
app.js 278 KiB app [emitted] app
Entrypoint app = ../css/app.css app.js
[0] multi ./js/app.js 28 bytes {app} [built]
+ 9 hidden modules
Also tests works fine!
What I’m doing wrong?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Exadra37
You need to be on Elixir
1.11.*to be able to useruntime.exs, what version are you in?zoedsoupe
Sorry, I didn’t provide this information!
elixir -v:erlang OTP v23.2.5
phx.new v1.5.7
zoedsoupe
I also took a look into
endpoint.exandendpoint/supervisor.exfile on phoenix repo, on:https://github.com/phoenixframework/phoenix/blob/master/lib/phoenix/endpoint.ex#L45
https://github.com/phoenixframework/phoenix/blob/master/lib/phoenix/endpoint/supervisor.ex#L24
And these information really clarified me, but didn’t solve the problem.
Exadra37
Try to replace the call to this function with
System.fetch_env!("ENV_VAR_NAME")or withSystem.get_env("ENV_VAR_NAME", "default_value").zoedsoupe
Same problem
Output:
Application.get_all_env/1:zoedsoupe
Well, Know I’m embarrassed with such a dumb error…
On
config.exsI correctly usedconfig :lovelace, LovelaceWeb.Endpoint, however, onruntime.exsI wrongly set up asconfig :lovelace, Lovelace.Endpoint…
Well sorry for this… I’m working on this project all day long, maybe it’s time to take a break haha
But all the research about compile time and runtime configs were so enlightening!
Exadra37
This command is missing the
HOSTenv var.zoedsoupe
nope! the
HOSTenv var is optional, default tolocalhostExadra37
But once you are using
MIX_ENV=prodI though you were trying to run it in production.