0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 20:30:23 +00:00

Describe arguments of the HTTP handler callbacks

This commit is contained in:
Loïc Hoguin 2013-03-02 20:33:22 +01:00
parent 8c54c048f2
commit a165a0bf46
2 changed files with 20 additions and 9 deletions

View file

@ -10,17 +10,26 @@ Usage
----- -----
You need to implement three callbacks for HTTP handlers. The first, You need to implement three callbacks for HTTP handlers. The first,
`init/3`, is common to all handlers. In the context of HTTP handlers `init/3`, is common to all handlers. It receives three arguments:
this should be used for any initialization needs. a tuple containing the transport and protocol in use, the `Req` object
and the handler options you defined in the routes. In the context of
HTTP handlers this should be used for any initialization needs. For
example you can initialize here the `State` variable that will be
passed to the following functions.
The second callback, `handle/2`, is where most of your code should The second callback, `handle/2`, is where most of your code should
be. As the name explains, this is where you handle the request. be. It receives two arguments: the `Req` object and the `State`
previously defined. As the name explains, this is where you handle
the request.
The last callback, `terminate/3`, will be empty most of the time. The last callback, `terminate/3`, will be empty most of the time.
It's used for any needed cleanup. If you used the process dictionary, It receives three arguments: the `Reason` for termination, the `Req`
timers, monitors then you most likely want to stop them in this object and the `State` previously defined. This callback should be
callback, as Cowboy might end up reusing this process for subsequent used strictly for cleaning up. Replying using the `Req` is disallowed.
requests. Please see the Internals chapter for more information. If you used the process dictionary, timers, monitors then you most
likely want to stop them in this callback, as Cowboy might end up
reusing this process for subsequent requests. Please see the
Internals chapter for more information.
Of course the general advice is to not use the process dictionary, Of course the general advice is to not use the process dictionary,
and that any operation requiring reception of messages should be and that any operation requiring reception of messages should be

View file

@ -108,8 +108,10 @@ handlers, Websocket handlers, REST handlers and static handlers. Their
usage is documented in the respective sections of the guide. usage is documented in the respective sections of the guide.
Most applications use the plain HTTP handler, which has three callback Most applications use the plain HTTP handler, which has three callback
functions: init/3, handle/2 and terminate/3. Following is an example of functions: init/3, handle/2 and terminate/3. You can find more information
a simple handler module. about the arguments and possible return values of these callbacks in the
HTTP handlers section of this guide. Following is an example of a simple
HTTP handler module.
``` erlang ``` erlang
-module(my_handler). -module(my_handler).