<div dir="ltr"><div>I'm looking at <a href="https://github.com/extend/cowboy/blob/master/examples/rest_hello_world/src/toppage_handler.erl">https://github.com/extend/cowboy/blob/master/examples/rest_hello_world/src/toppage_handler.erl</a> and it looks like all logic should be done in rest_init with rendering in specific handlers.<br>
<br></div><br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Dec 26, 2012 at 7:48 PM, Loïc Hoguin <span dir="ltr"><<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On 12/26/2012 04:23 PM, Max Lapshin wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Loic, would you kindly explain what are the exact rules to split code<br>
between init/3 and handle/2?<br>
<br>
Frankly speaking, I never use terminate, because I also doesn't<br>
understand what is it for, but most interesting is the question about<br>
init and handle.<br>
<br>
Where should I put database select, for example?<br>
</blockquote>
<br></div></div>
In handle.<br>
<br>
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.<span class="HOEnZb"><font color="#888888"><br>
<br>
-- <br>
Loïc Hoguin<br>
Erlang Cowboy<br>
Nine Nines<br>
<a href="http://ninenines.eu" target="_blank">http://ninenines.eu</a><br>
</font></span></blockquote></div><br></div>