2016-01-14 13:35:25 +01:00
|
|
|
= cowboy_middleware(3)
|
|
|
|
|
|
|
|
== Name
|
|
|
|
|
2016-12-23 18:52:02 +01:00
|
|
|
cowboy_middleware - Middlewares
|
2016-01-14 13:35:25 +01:00
|
|
|
|
|
|
|
== Description
|
2014-07-06 13:10:35 +02:00
|
|
|
|
2016-12-23 18:52:02 +01:00
|
|
|
The module `cowboy_middleware` defines a callback interface for
|
|
|
|
Cowboy middlewares.
|
2014-07-06 13:10:35 +02:00
|
|
|
|
|
|
|
Middlewares process the request sequentially in the order they
|
|
|
|
are configured.
|
|
|
|
|
2016-12-23 18:52:02 +01:00
|
|
|
== Callbacks
|
2014-07-06 13:10:35 +02:00
|
|
|
|
2016-12-23 18:52:02 +01:00
|
|
|
Middlewares implement the following interface:
|
2014-07-06 13:10:35 +02:00
|
|
|
|
2016-12-23 18:52:02 +01:00
|
|
|
[source,erlang]
|
|
|
|
----
|
|
|
|
execute(Req, Env)
|
|
|
|
-> {ok, Req, Env}
|
|
|
|
| {suspend, module(), atom(), [any()]}
|
|
|
|
| {stop, Req}
|
2014-07-06 13:10:35 +02:00
|
|
|
|
2016-12-23 18:52:02 +01:00
|
|
|
Req :: cowboy_req:req()
|
|
|
|
Env :: cowboy_middleware:env()
|
|
|
|
----
|
2014-07-06 13:10:35 +02:00
|
|
|
|
2016-12-23 18:52:02 +01:00
|
|
|
The `execute/2` is the only callback that needs to be
|
|
|
|
implemented. It must execute the middleware and return
|
|
|
|
with instructions for Cowboy.
|
|
|
|
|
|
|
|
ok::
|
|
|
|
|
|
|
|
Cowboy should continue processing the request using the
|
|
|
|
returned Req object and environment.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2019-10-10 15:53:26 +02:00
|
|
|
// @todo No need to return the Req when stopping. Fix in 3.0.
|
|
|
|
|
2016-12-23 18:52:02 +01:00
|
|
|
== Types
|
|
|
|
|
|
|
|
=== env()
|
|
|
|
|
|
|
|
[source,erlang]
|
|
|
|
----
|
|
|
|
env() :: #{atom() => any()}
|
|
|
|
----
|
|
|
|
|
|
|
|
Middleware environment.
|
2014-07-06 13:10:35 +02:00
|
|
|
|
2016-12-23 18:52:02 +01:00
|
|
|
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.
|
2014-07-06 13:10:35 +02:00
|
|
|
|
2016-12-23 18:52:02 +01:00
|
|
|
Middlewares may modify the environment as necessary.
|
2014-07-06 13:10:35 +02:00
|
|
|
|
2016-12-23 18:52:02 +01:00
|
|
|
== Changelog
|
2014-07-06 13:10:35 +02:00
|
|
|
|
2016-12-23 18:52:02 +01:00
|
|
|
* *2.0*: The `env` type is now a map instead of a proplist.
|
|
|
|
* *1.0*: Behavior introduced.
|
2014-07-06 13:10:35 +02:00
|
|
|
|
2016-12-23 18:52:02 +01:00
|
|
|
== See also
|
2014-07-06 13:10:35 +02:00
|
|
|
|
2016-12-23 18:52:02 +01:00
|
|
|
link:man:cowboy(7)[cowboy(7)]
|