Server.Middleware

HTML & web

Composable Request -> Response wrappers for handle.

Wire at the top of your handler before Server.Route.parse:

handle =
    Middleware.run
        [ Middleware.log
        , Middleware.requestId
        , Middleware.cors "https://example.com"
        ]
        dispatch

The first middleware in the list is outermost (same order as Phoenix plugs).

HTTP server spans are created automatically in the listener once OpenTelemetry.configure has run; trace only enriches correlation metadata.

Types

Handler

type alias Handler =
Request -> Response

An HTTP request handler.

Examples

dispatch : Handler
dispatch req =
    Server.empty 204 []

Middleware

type alias Middleware =
Handler -> Handler

A function that wraps a handler.

Examples

cors "https://example.com"

Values

run

run : List Middleware -> Handler -> Handler

Compose middlewares outside-in, then call inner.

Examples

run [ log, requestId ] dispatch

cors

cors : String -> Middleware

Handle OPTIONS with a fixed-origin preflight; otherwise attach CORS headers.

Pass a trusted/fixed origin string (a constant), not a reflected request Origin. For request-supplied origins use corsAllowlist.

Examples

cors "https://example.com"

corsAllowlist

corsAllowlist : List String -> Middleware

Like cors, but only when the request Origin is in allowed.

Use this when browsers send varying Origin values. Do not pipe the raw header into cors / Server.corsHeaders.

Examples

corsAllowlist [ "https://a.example", "https://b.example" ]

requestId

requestId : Middleware

Echo x-request-id from the request, or mint one; set it on the response and Logger.metadata.

Examples

requestId

log

log : Middleware

Log method, url, status, and duration after the response.

Examples

log

trace

trace : Middleware

Attach request id / trace attributes on the current auto span and logs.

Examples

trace