Hologram.Middleware behaviour (hologram v0.10.1)

Copy Markdown View Source

The behaviour and construct for module middleware - reusable, server-side logic that runs before a page renders and before a command executes.

use Hologram.Middleware makes a module a middleware, written one of two ways.

A leaf implements the call/2 callback, receiving the Hologram.Server struct and the declared options and returning a (possibly transformed) server struct:

defmodule MyApp.RequireAdmin do
  use Hologram.Middleware

  @impl Hologram.Middleware
  def call(server, _opts) do
    if admin?(server) do
      server
    else
      put_status(server, :forbidden)
    end
  end
end

A composite declares a sub-chain with the middleware macros and has its call/2 generated to fold that chain, so the composite is itself an ordinary middleware that attaches as a single unit:

defmodule MyApp.AdminStack do
  use Hologram.Middleware

  middleware MyApp.RequireLogin
  middleware MyApp.RequireAdmin
  middleware MyApp.AuditLog
end

use Hologram.Middleware injects the behaviour, the Hologram.Server response helpers (so a leaf writes put_status/2, put_redirect/2, put_stash/3, ... without qualification), and the middleware declaration macros. The call/2 is generated only for a module that declared middleware and did not define its own - a module that does neither raises the standard "call/2 not implemented" behaviour warning, so a forgotten gate never silently passes.

A middleware module is attached to a page or component with the middleware macros.

Summary

Callbacks

Runs the middleware against the server struct.

Callbacks

call(server, opts)

@callback call(server :: Hologram.Server.t(), opts :: term()) :: Hologram.Server.t()

Runs the middleware against the server struct.

Receives the server and the declared options, and returns the server struct - possibly carrying a terminal response that short-circuits the remaining middleware.