livery_middleware behaviour (livery v0.1.0)
View SourceTower-style middleware pipeline.
A stack is an ordered list of middleware entries, each of which is either:
- a
{Module, State}tuple whereModuleimplements thelivery_middlewarebehaviour (call/3); or - a fun
fun((Req, Next) -> Resp).
The pipeline terminates in a handler, which is either
{Module, Function} or a fun fun((Req) -> Resp). run/3 walks
the stack, threading a Next continuation that invokes the rest
of the stack plus the handler. Middleware can short-circuit
(never call Next), transform the request before calling Next,
transform the response after, or both.
Callback
call(Req, Next, State) -> Resp— invoke the middleware with the request, the next continuation, and the per-instance state.
Summary
Functions
Lift a response transformer into a middleware entry.
Lift a request transformer into a middleware entry.
Execute the middleware stack followed by the handler.
Wrap the downstream call in a try/catch.
Types
-type req() :: #livery_req{protocol :: h1 | h2 | h3, method :: binary(), scheme :: binary(), authority :: binary(), path :: binary(), raw_query :: binary(), bindings :: #{binary() => binary()}, headers :: [{binary(), binary()}], peer :: {inet:ip_address(), inet:port_number()} | undefined, tls :: undefined | map(), body :: empty | {buffered, iodata()} | {stream, term()}, adapter :: module() | undefined, stream :: term(), engine_pid :: pid() | undefined, notifier_pid :: pid() | undefined, disc_ref :: reference() | undefined, req_id :: binary(), started_at :: integer() | undefined, meta :: map()}.
-type resp() :: #livery_resp{status :: 100..599, headers :: [{binary(), binary()}], body :: {full, iodata()} | {chunked, fun((term()) -> ok | {error, term()})} | {sse, fun((term()) -> ok | {error, term()})} | {file, file:name_all(), undefined | {non_neg_integer(), non_neg_integer() | eof}} | {upgrade, ws | wt, term()} | empty | taken_over, trailers :: undefined | [{binary(), binary()}] | fun(() -> [{binary(), binary()}])}.
-type stack() :: [entry()].
Callbacks
Functions
Lift a response transformer into a middleware entry.
Lift a request transformer into a middleware entry.
The returned entry calls Fun(Req) to derive a new request, then
invokes the rest of the pipeline.
Execute the middleware stack followed by the handler.
Wrap the downstream call in a try/catch.
Fun is called as Fun(Class, Reason, Stacktrace) and must
return a #livery_resp{}. Intended for top-of-stack error
recovery. Class is throw | error | exit.