fireproofsocks

fireproofsocks

Passing options to a plug inside Plug.Builder

I’m wanting to put a few static files behind auth so they aren’t available to the public and only available to authorized users.

I put together a plug using Plug.Builder:

defmodule MyApp.PrivateStaticPlug do
  @moduledoc """
  Serves static files to authenticated users.
  """
  use Plug.Builder

  plug(MyApp.AuthPlug)

  plug(Plug.Static,
    at: "/private",
    from: {:my_app, "priv/private_static"},
    gzip: false
  )

  plug(:not_found)

  def not_found(conn, _) do
    conn
    |> send_resp(404, "Not found")
    |> halt()
  end
end

And then I can reference the plug in my app’s endpoint:

plug(MyApp.PrivateStaticPlug)

However, it would be nicer if I could set the options in the endpoint.ex and pass them into the plug. I tried this:

  plug(Plug.Static)

  def call(conn, opts) do
    conn
    |> super(opts)
    |> assign(:called_all_plugs, true)
  end

But overriding the call/2 doesn’t work for Plug.Static because Plug.Static requires options at compile time. Is there a way to do this?

Where Next?

Popular in Questions Top

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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54260 488
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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

We're in Beta

About us Mission Statement