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"
]
dispatchThe 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 -> ResponseAn HTTP request handler.
Examples
dispatch : Handler
dispatch req =
Server.empty 204 []Middleware
type alias Middleware =
Handler -> HandlerA function that wraps a handler.
Examples
cors "https://example.com"Values
run
run : List Middleware -> Handler -> HandlerCompose middlewares outside-in, then call inner.
Examples
run [ log, requestId ] dispatchcors
cors : String -> MiddlewareHandle 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 -> MiddlewareLike 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 : MiddlewareEcho x-request-id from the request, or mint one; set it on the response and Logger.metadata.
Examples
requestIdlog
log : MiddlewareLog method, url, status, and duration after the response.
Examples
logtrace
trace : MiddlewareAttach request id / trace attributes on the current auto span and logs.
Examples
trace