I have a few plugs that I call every time. I would like to create a single plug that calls all of them for me. How would I go about doing that?
This is what I've currently tried to do:
defmodule MyApp.SpecialPlug do
import Plug.Conn
def init(default), do: default
def call(conn, default) do
plug SimplePlug1
plug SimplePlug2, args: :something
end
end
but it throws a CompileError
, saying: undefined function plug
You can simply use Plug.Builder
for this:
defmodule MyApp.SpecialPlug do
use Plug.Builder
plug SimplePlug1
plug SimplePlug2, args: :something
end
This will define init
and call
automatically which will sequentially pass conn
to SimplePlug1
and then SimplePlug2
.
If you really want to call a plug manually, you can do something like this:
defmodule MyApp.SpecialPlug do
def init({opts1, opts2}) do
{SimplePlug1.init(opts1), SimplePlug2.init(opts2)}
end
def call(conn, {opts1, opts2}) do
case SimplePlug1.call(conn, opts1) do
%Plug.Conn{halted: true} = conn -> conn
conn -> SimplePlug2.call(conn, opts2)
end
end
end
Note that you will have to add the check for halted: true
yourself as above (unless you want to ignore halts for some reason). Plug.Builder
does the same for you automatically
To get the equivalent of:
plug SimplePlug1
plug SimplePlug2, args: :something
you can now do:
plug MyApp.SpecialPlug, {[], [args: :something]}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With