I have a scenario where developers can configure the endpoint using environment variables. Environment variables are also used in deployments. So in file config/config.exs
, I have the following code:
force_ssl_config =
if System.get_env("FORCE_SSL", "") == "" do
[]
else
[
force_ssl: [
hsts: true,
host:
"#{System.get_env("PHX_HOST", "localhost")}:#{System.get_env("URL_PORT", "4000")}",
cipher_suite: :strong,
rewrite_on: [:x_forwarded_proto],
exclude:
"HSTS_EXCLUDED_HOSTS"
|> System.get_env(~s(["localhost"]))
# I can't call Jason.decode!/2 here
|> Jason.decode!()
]
]
end
config :my_app,
MyAppWeb.Endpoint,
[
url: [host: "localhost"],
render_errors: [
view: MyAppWeb.ErrorView,
accepts: ~w(html json),
layout: false
],
pubsub_server: MyApp.PubSub,
live_view: [signing_salt: "salt-value"]
] ++ force_ssl_config
Calling Jason.decode!/2
works in config/runtime.exs
but not in config/config.exs
while force_ssl
is a compile time config.
I’m aware of the {module], function, args}
setting of the exclude
key, but that will require doing decoding json on every request.
So here is what I need help/clarification with:
-
Is there a way to decode json in
config/config.exs
? -
If not, can I use the
{module, function, args}
config without decoding json on every request? -
Is decoding json on every request even a bad idea (I seem to think so, but I’m not sure)?
-
Any suggestion on how
force_ssl
can be configured using environment variables?