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

Types

plug :: module | atom

Functions

compile(stack)

Specs:

Compiles a plug stack.

It expects a reversed stack (with the last plug coming first) and returns a tuple containing the reference to the connection as first argument and the compiled quote stack.

Macros

plug(plug, opts \\ [])

A macro that stores a new plug.