| 1 |
= cowboy_middleware(3) |
| 2 |
|
| 3 |
== Name |
| 4 |
|
| 5 |
cowboy_middleware - Middlewares |
| 6 |
|
| 7 |
== Description |
| 8 |
|
| 9 |
The module `cowboy_middleware` defines a callback interface for |
| 10 |
Cowboy middlewares. |
| 11 |
|
| 12 |
Middlewares process the request sequentially in the order they |
| 13 |
are configured. |
| 14 |
|
| 15 |
== Callbacks |
| 16 |
|
| 17 |
Middlewares implement the following interface: |
| 18 |
|
| 19 |
[source,erlang] |
| 20 |
---- |
| 21 |
execute(Req, Env) |
| 22 |
-> {ok, Req, Env} |
| 23 |
| {suspend, module(), atom(), [any()]} |
| 24 |
| {stop, Req} |
| 25 |
|
| 26 |
Req :: cowboy_req:req() |
| 27 |
Env :: cowboy_middleware:env() |
| 28 |
---- |
| 29 |
|
| 30 |
The `execute/2` is the only callback that needs to be |
| 31 |
implemented. It must execute the middleware and return |
| 32 |
with instructions for Cowboy. |
| 33 |
|
| 34 |
ok:: |
| 35 |
|
| 36 |
Cowboy should continue processing the request using the |
| 37 |
returned Req object and environment. |
| 38 |
|
| 39 |
suspend:: |
| 40 |
|
| 41 |
Cowboy will hibernate the process. When resuming, Cowboy |
| 42 |
will apply the returned module, function and arguments. |
| 43 |
|
| 44 |
stop:: |
| 45 |
|
| 46 |
Cowboy will stop middleware execution. No other middleware |
| 47 |
will be executed. This effectively ends the processing of |
| 48 |
the request. |
| 49 |
|
| 50 |
// @todo No need to return the Req when stopping. Fix in 3.0. |
| 51 |
|
| 52 |
== Types |
| 53 |
|
| 54 |
=== env() |
| 55 |
|
| 56 |
[source,erlang] |
| 57 |
---- |
| 58 |
env() :: #{atom() => any()} |
| 59 |
---- |
| 60 |
|
| 61 |
Middleware environment. |
| 62 |
|
| 63 |
A new environment is created for every request. The initial |
| 64 |
environment contained the user configured environment values |
| 65 |
(like `dispatch` for example) plus the `listener` value which |
| 66 |
contains the name of the listener for this connection. |
| 67 |
|
| 68 |
Middlewares may modify the environment as necessary. |
| 69 |
|
| 70 |
== Changelog |
| 71 |
|
| 72 |
* *2.0*: The `env` type is now a map instead of a proplist. |
| 73 |
* *1.0*: Behavior introduced. |
| 74 |
|
| 75 |
== See also |
| 76 |
|
| 77 |
link:man:cowboy(7)[cowboy(7)] |