handlers.asciidoc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. [[handlers]]
  2. == Handlers
  3. Handlers are Erlang modules that handle HTTP requests.
  4. === Plain HTTP handlers
  5. The most basic handler in Cowboy implements the mandatory
  6. `init/2` callback, manipulates the request, optionally
  7. sends a response and then returns.
  8. This callback receives the xref:req[Req object] and the initial
  9. state defined in the xref:routing[router configuration].
  10. A handler that does nothing would look like this:
  11. [source,erlang]
  12. ----
  13. init(Req, State) ->
  14. {ok, Req, State}.
  15. ----
  16. Despite sending no reply, a `204 No Content` response will be
  17. sent to the client, as Cowboy makes sure that a response is
  18. sent for every request.
  19. We need to use the Req object to reply.
  20. [source,erlang]
  21. ----
  22. init(Req0, State) ->
  23. Req = cowboy_req:reply(200, #{
  24. <<"content-type">> => <<"text/plain">>
  25. }, <<"Hello World!">>, Req0),
  26. {ok, Req, State}.
  27. ----
  28. Cowboy will immediately send a response when `cowboy:reply/4`
  29. is called.
  30. We then return a 3-tuple. `ok` means that the handler ran
  31. successfully. We also give the modified Req back to Cowboy.
  32. The last value of the tuple is a state that will be used
  33. in every subsequent callbacks to this handler. Plain HTTP
  34. handlers only have one additional callback, the optional
  35. and rarely used `terminate/3`.
  36. === Other handlers
  37. The `init/2` callback can also be used to inform Cowboy
  38. that this is a different kind of handler and that Cowboy
  39. should switch to it. To do this you simply need to return
  40. the module name of the handler type you want to switch to.
  41. Cowboy comes with three handler types you can switch to:
  42. xref:rest_handlers[cowboy_rest], xref:ws_handlers[cowboy_websocket]
  43. and xref:loop_handlers[cowboy_loop]. In addition to those you
  44. can define your own handler types.
  45. Switching is simple. Instead of returning `ok`, you simply
  46. return the name of the handler type you want to use. The
  47. following snippet switches to a Websocket handler:
  48. [source,erlang]
  49. ----
  50. init(Req, State) ->
  51. {cowboy_websocket, Req, State}.
  52. ----
  53. === Cleaning up
  54. All handler types provide the optional `terminate/3` callback.
  55. [source,erlang]
  56. ----
  57. terminate(_Reason, _Req, _State) ->
  58. ok.
  59. ----
  60. This callback is strictly reserved for any required cleanup.
  61. You cannot send a response from this function. There is no
  62. other return value.
  63. This callback is optional because it is rarely necessary.
  64. Cleanup should be done in separate processes directly (by
  65. monitoring the handler process to detect when it exits).
  66. Cowboy does not reuse processes for different requests. The
  67. process will terminate soon after this call returns.