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?