<div dir="ltr">Hey,<div>I think you can use cowboy middleware <a href="https://github.com/ninenines/cowboy/tree/master/examples/markdown_middleware">https://github.com/ninenines/cowboy/tree/master/examples/markdown_middleware</a></div><div><br></div><div>or you can also use on_request option in cowboy:start_http()</div><div><br></div><div><br></div><div>Regards,</div><div>Vansh</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Dec 7, 2015 at 2:33 PM, avinash D'silva <span dir="ltr"><<a href="mailto:evnix.com@gmail.com" target="_blank">evnix.com@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Hi,</div><div><br></div><div><br></div><div>I am using cowboy+cowboy_session for building websites. </div><div><br></div><div>each route has something similar as shown below:</div><div><br></div><div><br></div><div><div>-module(get_pageX).</div><div>-behaviour(cowboy_http_handler).</div><div><br></div><div>-export([init/3]).</div><div>-export([handle/2]).</div><div>-export([terminate/3]).</div><div><br></div><div>-record(state, {</div><div>}).</div><div><br></div><div>init(_, Req, _Opts) -></div><div><span style="white-space:pre-wrap"> </span>{ok, Req, #state{}}.</div><div><br></div><div>handle(Req, State=#state{}) -></div><div><br></div><div> </div><div><br></div><div> case cowboy_session:get("loggedin", Req) of</div><div><br></div><div> true -> </div><div> #do something here</div><div> </div><div> _ -></div><div> #not logged in.</div><div><br></div><div> end,</div><div><br></div><div><br></div><div><br></div><div><span style="white-space:pre-wrap"> </span>{ok, ReqFinal} = cowboy_req:reply(200,</div><div><span style="white-space:pre-wrap"> </span>[{<<"content-type">>, <<"text/html">>}],</div><div> <span style="white-space:pre-wrap"> <<"Some response">></span>,</div><div> <span style="white-space:pre-wrap"> </span>Req),</div><div><br></div><div><span style="white-space:pre-wrap"> </span>{ok, ReqFinal, State}.</div><div><br></div><div>terminate(_Reason, _Req, _State) -></div><div><span style="white-space:pre-wrap"> </span>ok.</div></div><div><br></div><div><br></div><div>The above CASE(code) is repeated for every route to check if the user is logged in, is there a better way to organize this?</div><div><br></div><div>what I am looking for is a way to check if the user is logged in and if not, redirect to login route.</div><div><br></div><div><br></div><div>PS: I am new to Erlang</div><div><br></div><div><br></div><div>Regards,</div><div>Avinash D' Silva <br></div><div><br></div><div><br></div>
</div>
<br>_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br></blockquote></div><br></div>