Plug.Builder
Conveniences for building plugs.
This module can be used into a module in order to build a plug stack:
defmodule MyApp do
use Plug.Builder
plug :hello, upper: true
def hello(conn, opts) do
body = if opts[:upper], do: "WORLD", else: "world"
send_resp(conn, 200, body)
end
end
Plug.Builder
will define a init/1
function (which is overridable)
and a call/2
function with the compiled stack. By implementing the
Plug API, Plug.Builder
guarantees this module can be handed to a web
server or used as part of another stack.
Note this module also exports a compile/1
function for those willing
to collect and compile their plugs manually.
Halting a Plug Stack
A Plug Stack can be halted with Plug.Conn.halt/1
. The Builder will prevent
further plugs downstream from being invoked and return current connection.
Summary
compile(stack) | Compiles a plug stack |
plug(plug, opts \\ []) | A macro that stores a new plug |