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

Loïc Hoguin essen@REDACTED
Wed Dec 26 17:06:46 CET 2012

REST handlers are different, these work like Webmachine, and the logic 
is spread over many different functions.

On 12/26/2012 05:03 PM, Max Lapshin wrote:
> I'm looking at
> https://github.com/extend/cowboy/blob/master/examples/rest_hello_world/src/toppage_handler.erl
> and 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 <essen@REDACTED
> <mailto:essen@REDACTED>> 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

Loïc Hoguin
Erlang Cowboy
Nine Nines

More information about the erlang-questions mailing list