The plug specification.
There are two kind of plugs: function plugs and module plugs.
A function plug is any function that receives a connection and a set of options and returns a connection. Its type signature must be:
(Plug.Conn.t, Plug.opts) :: Plug.Conn.t
A module plug is an extension of the function plug. It is a module that must export:
call/2function with the signature defined above
init/1function which takes a set of options and initializes it.
The result returned by
init/1 is passed as second argument to
init/1 may be called during compilation and as such it must not return
pids, ports or values that are not specific to the runtime.
The API expected by a module plug is defined as a behaviour by the
Plug module (this module).
Here’s an example of a function plug:
def json_header_plug(conn, opts) do conn |> put_resp_content_type("application/json") end
Here’s an example of a module plug:
defmodule JSONHeaderPlug do def init(opts) do opts end def call(conn, _opts) do conn |> put_resp_content_type("application/json") end end
Plug.Builder module provides conveniences for building plug