mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-16 05:00:24 +00:00
Update cowboy_middleware
This commit is contained in:
parent
5da7d1ef05
commit
3bcb1cb42e
1 changed files with 57 additions and 30 deletions
|
@ -2,47 +2,74 @@
|
||||||
|
|
||||||
== Name
|
== Name
|
||||||
|
|
||||||
cowboy_middleware - behaviour for middlewares
|
cowboy_middleware - Middlewares
|
||||||
|
|
||||||
== Description
|
== Description
|
||||||
|
|
||||||
The `cowboy_middleware` behaviour defines the interface used
|
The module `cowboy_middleware` defines a callback interface for
|
||||||
by Cowboy middleware modules.
|
Cowboy middlewares.
|
||||||
|
|
||||||
Middlewares process the request sequentially in the order they
|
Middlewares process the request sequentially in the order they
|
||||||
are configured.
|
are configured.
|
||||||
|
|
||||||
== Types
|
|
||||||
|
|
||||||
=== env() = [{atom(), any()}]
|
|
||||||
|
|
||||||
The environment variable.
|
|
||||||
|
|
||||||
One is created for every request. It is passed to each
|
|
||||||
middleware module executed and subsequently returned,
|
|
||||||
optionally with its contents modified.
|
|
||||||
|
|
||||||
== Callbacks
|
== Callbacks
|
||||||
|
|
||||||
=== execute(Req, Env) -> {ok, Req, Env} | {suspend, Module, Function, Args} | {stop, Req}
|
Middlewares implement the following interface:
|
||||||
|
|
||||||
Req = cowboy_req:req():: The Req object.
|
[source,erlang]
|
||||||
Env = env():: The request environment.
|
----
|
||||||
Module = module():: MFA to call when resuming the process.
|
execute(Req, Env)
|
||||||
Function = atom():: MFA to call when resuming the process.
|
-> {ok, Req, Env}
|
||||||
Args = [any()]:: MFA to call when resuming the process.
|
| {suspend, module(), atom(), [any()]}
|
||||||
|
| {stop, Req}
|
||||||
|
|
||||||
Execute the middleware.
|
Req :: cowboy_req:req()
|
||||||
|
Env :: cowboy_middleware:env()
|
||||||
|
----
|
||||||
|
|
||||||
The `ok` return value indicates that everything went well
|
The `execute/2` is the only callback that needs to be
|
||||||
and that Cowboy should continue processing the request. A
|
implemented. It must execute the middleware and return
|
||||||
response may or may not have been sent.
|
with instructions for Cowboy.
|
||||||
|
|
||||||
The `suspend` return value will hibernate the process until
|
ok::
|
||||||
an Erlang message is received. Note that when resuming, any
|
|
||||||
previous stacktrace information will be gone.
|
|
||||||
|
|
||||||
The `stop` return value stops Cowboy from doing any further
|
Cowboy should continue processing the request using the
|
||||||
processing of the request, even if there are middlewares
|
returned Req object and environment.
|
||||||
that haven't been executed yet. The connection may be left
|
|
||||||
open to receive more requests from the client.
|
suspend::
|
||||||
|
|
||||||
|
Cowboy will hibernate the process. When resuming, Cowboy
|
||||||
|
will apply the returned module, function and arguments.
|
||||||
|
|
||||||
|
stop::
|
||||||
|
|
||||||
|
Cowboy will stop middleware execution. No other middleware
|
||||||
|
will be executed. This effectively ends the processing of
|
||||||
|
the request.
|
||||||
|
|
||||||
|
== Types
|
||||||
|
|
||||||
|
=== env()
|
||||||
|
|
||||||
|
[source,erlang]
|
||||||
|
----
|
||||||
|
env() :: #{atom() => any()}
|
||||||
|
----
|
||||||
|
|
||||||
|
Middleware environment.
|
||||||
|
|
||||||
|
A new environment is created for every request. The initial
|
||||||
|
environment contained the user configured environment values
|
||||||
|
(like `dispatch` for example) plus the `listener` value which
|
||||||
|
contains the name of the listener for this connection.
|
||||||
|
|
||||||
|
Middlewares may modify the environment as necessary.
|
||||||
|
|
||||||
|
== Changelog
|
||||||
|
|
||||||
|
* *2.0*: The `env` type is now a map instead of a proplist.
|
||||||
|
* *1.0*: Behavior introduced.
|
||||||
|
|
||||||
|
== See also
|
||||||
|
|
||||||
|
link:man:cowboy(7)[cowboy(7)]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue