scope "/api", TweetBotWeb do
pipe_through :api
post "/#{Application.get_env(:tweet_bot, :webhook)}", TwitterController, :index
end
In dev.exs I have:
config :tweet_bot,
webhook: "twitter"
It works fine in dev env.
In prod.secret.exs file I have:
config :tweet_bot,
webhook: "${WEBHOOK}"
With REPLACE_OS_VARS=true I thought the release built with distillery should read the webhook from env variables, but seems not the case, the router doesn’t exists. I just got 404 when posting to the api.
If I change it to something static character, it would work fine.
Application.get_env(:tweet_bot, :webhook) would try and read :webhook at compile time (but REPLACE_OS_VARS=true works for runtime env vars?) since it’s called from inside a macro. Do you have WEBHOOK set at compile time?
In your case your configuration might be replaced with
config :tweet_bot,
webhook: System.get_env("WEBHOOK") || raise("expected WEBHOOK env var to be set")
to make it more clear when it’s read (during compilation).
Then, during compilation, the routes macros
scope "/api", TweetBotWeb do
pipe_through :api
post "/#{Application.get_env(:tweet_bot, :webhook)}", TwitterController, :index
end
would look into the ets table holing application config state (Application.get_env(:tweet_bot, :webhook)), get your webhook, and interpolate it into the post route.
And the application configuration for :tweet_bot itself would be populated from your env vars (again, during compilation).
REPLACE_OS_VARS=true is specified to distillery, with it I can use env variables in vm.args file. What confuses me here is all the other env works just fine,
I think REPLACE_OS_VARS=true is for runtime env vars (since it’s for vm args), not compile time. The rest are probably used during runtime, so that’s why they work and WEBHOOK doesn’t.
Maybe phoenix doesn’t support router created from env?
I don’t think phoenix should care … And it doesn’t really know that the information comes from env since it reads :webhook from application config, which uses an ets table.
Try replacing config with
config :tweet_bot,
webhook: System.get_env("webhook") || raise("no webhook env var set")
and see if it raises.
Before you had WEBHOOK env var, now you use webhook. Maybe that’s why it doesn’t work in dev?
You’d need to recompile the project. mix phx.routes doesn’t recompile by default, it seems. But that’s a different problem.
I’ve recompiled and got
$ WEBHOOK=telega mix do compile --force, phx.routes
Compiling 10 files (.ex)
Generated env_test app
webhook_path GET /api/telega EnvTestWeb.WebhookController :index
And that makes sense, since mix phx.routes seems to just return the routes from the currently compiled router.
Or maybe that’s not about mix phx.routes at all but rather nothing happens since there was no change in code (only an env var WEBHOOK was changed). So there is no way for elixir to know that the project should be recompiled.
But again, that’s a different problem if a problem at all.