ridho9

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

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.

Last Post!

ridho9

ridho9

Ah, that makes sense. I tried to pass the &Mod.fun/ar instead and it works. It seems I have to find another way to pass the value. Thanks!

Where Next?

Popular in Questions Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49266 226
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement