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