ridho9
Env variable is nil when passed through plug opts on runtime when using release
Hello, so I have few apps under an umbrella, lets say the names are linesdk, hibiki, and hibiki_web. So linesdk have a plug where you pass a secret key as the opts. hibiki_web is the app which runs the cowboy server and routes request to the linesdk plug. The secret key that needs to be passed is inside the :hibiki :channel_secret config. Here are some snippets
defmodule Hibiki.Config do
def channel_secret, do: Application.fetch_env!(:hibiki, :channel_secret)
end
defmodule HibikiWeb.Router do
...
post("/hibiki",
to: LineSdk.Plug,
init_opts: [
channel_secret: Hibiki.Config.channel_secret(),
handler: Hibiki.WebhookHandler
]
)
...
end
defmodule LineSdk.Plug do
use Plug.Builder
...
plug(:validate, builder_opts())
...
def validate(
%Plug.Conn{
assigns: %{raw_body: raw_body}
} = conn,
opts
) do
with {:ok, signature} <- get_signature(conn),
{:ok, _} <-
LineSdk.Auth.validate_message(
raw_body,
opts[:channel_secret],
signature
) do
conn
else
{:error, err} ->
conn
|> send_resp(400, "#{err}")
|> halt
end
end
...
end
And the plug access the channel_secret using opts[:channel_secret].
Now, these piece of code is running fine in my local development. But when I build the release into a docker image and deploy it, somehow the opts[:channel_secret] became nil while the opts[:handler] is not changing.
The weider thing is when use the release binary remote command and run Hibiki.Config.channel_secret() it does returns the correct value.
This is after I refactor the plug from hibiki_web to linesdk. Before it was fine. But in both cases, it is still the hibiki_web app that calls the Hibiki.Config.channel_secret() but somehow when the released app is running it returns nil. Can anybody help me? Thank you very much!
First Post!
Nicd
Since you are using the function in your router’s post macro for init values, most likely the value is set at compile time (plug init calls are made at compile time). So check that the value is set correctly when you are compiling the release. Or if you only want to set the value on your target machine, you will need to refactor your code to grab it at runtime.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








