cowboy_middleware.asciidoc 1.6 KB

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