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.

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement