module Bezel::Plug

A step of an endpoint's pipeline: takes the connection and returns it changed, as Elixir's Plug does. A plug that answers the request calls Conn#halt (or one of Conn#text, Conn#html, Conn#json, Conn#redirect, Conn#send_resp, which halt), and the plugs after it do not run.

Plugs are Shareable values, made once when the endpoint starts and used by every request: their settings are fields, their state per request goes in the Conn.

A plug that must act after the rest of the pipeline (logging the status, a time limit) overrides wrap, which is given the rest as a procedure. The rest does not raise: an exception a later plug raised comes back as the connection's Conn#error (the connection halted), which wrap may inspect, or replace by clearing error and answering itself (out.with(error: nil).html(page, status: 404)); an endpoint's "no route" Bezel::NotFound comes back so too.

data Hello(name: String)
  include Bezel::Plug
  def call(conn: Bezel::Conn) -> Bezel::Conn = conn.text("hello #{name}")
end

conn = Bezel::Plug.run([Hello.new("you")], Bezel::Conn.build(HTTP::Server::Request.get("/")))
conn.to_response.body          # => HTTP::Content::Text(text: "hello you")

src/plug.qz:27

Classes and modules

Class methods

def self.run(plugs: Array[Bezel::Plug], conn: Bezel::Conn) -> Bezel::Conn

Runs plugs on conn in order, until one halts. A StandardError a plug raises stops the pipeline: the result is the connection as that plug received it (with what the plugs before it did: the request id, headers), halted, the exception its error.

src/plug.qz:44

def self.run(plugs: Array[Bezel::Plug], conn: Bezel::Conn, rest: (Bezel::Conn) -> Bezel::Conn) -> Bezel::Conn

Runs plugs on conn in order, then rest, until one halts: the plugs' wraps are around rest too.

src/plug.qz:48

Methods

abstract def call(conn: Bezel::Conn) -> Bezel::Conn

The connection changed by this plug.

src/plug.qz:31

def wrap(conn: Bezel::Conn, rest: (Bezel::Conn) -> Bezel::Conn) -> Bezel::Conn

Runs this plug, then rest (the plugs after it) unless it halted. Override it to act around the rest: its result is the request's.

src/plug.qz:35