[erlang-questions] Cowboy: init and handle — how to use them?

Max Lapshin <>
Wed Dec 26 17:03:09 CET 2012


I'm looking at
https://github.com/extend/cowboy/blob/master/examples/rest_hello_world/src/toppage_handler.erland
it looks like all logic should be done in rest_init with rendering in
specific handlers.




On Wed, Dec 26, 2012 at 7:48 PM, Loïc Hoguin <> wrote:

> On 12/26/2012 04:23 PM, Max Lapshin wrote:
>
>> Loic, would you kindly explain what are the exact rules to split code
>> between init/3 and handle/2?
>>
>> Frankly speaking, I never use terminate, because I also doesn't
>> understand what is it for, but most interesting is the question about
>> init and handle.
>>
>> Where should I put database select, for example?
>>
>
> In handle.
>
> It works like a gen_server with few differences. In init you would
> initialize the state if needed (plain HTTP requests may not need it, other
> kinds probably do), check that the request looks good and inform Cowboy
> what kind of handler this is. In handle you do whatever you need to do with
> the request. In terminate you cleanup. Like with gen_server, terminate will
> rarely do anything because most of the time when you need to clean things
> up you just have monitors reporting this process is 'DOWN' and do the
> cleanup there instead.
>
> --
> Loïc Hoguin
> Erlang Cowboy
> Nine Nines
> http://ninenines.eu
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20121226/a102ab8e/attachment.html>


More information about the erlang-questions mailing list