Plug.Builder

Conveniences for building plugs.

This module can be used into a module in order to build a plug pipeline:

defmodule MyApp do
  use Plug.Builder

  plug Plug.Logger
  plug :hello, upper: true

  def hello(conn, opts) do
    body = if opts[:upper], do: "WORLD", else: "world"
    send_resp(conn, 200, body)
  end
end

Multiple plugs can be defined with the plug/2 macro, forming a pipeline. Plug.Builder also imports the Plug.Conn module, making functions like send_resp/3 available.

Plug behaviour

Internally, Plug.Builder implements the Plug behaviour, which means both init/1 and call/2 functions are defined. By implementing the Plug API, Plug.Builder guarantees this module can be handed to a web server or used as part of another pipeline.

Halting a Plug pipeline

A Plug pipeline can be halted with Plug.Conn.halt/1. The builder will prevent further plugs downstream from being invoked and return the current connection.

Summary

compile(pipeline)

Compiles a plug pipeline

plug(plug, opts \\ [])

A macro that stores a new plug

Types

plug :: module | atom

Functions

compile(pipeline)

Specs:

Compiles a plug pipeline.

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

Macros

plug(plug, opts \\ [])

A macro that stores a new plug.