[erlang-questions] Cowboy: init and handle — how to use them?
Loïc Hoguin
essen@REDACTED
Wed Dec 26 16:48:20 CET 2012
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
More information about the erlang-questions
mailing list