[erlang-questions] how to better organize code for a website

avinash D'silva evnix.com@REDACTED
Mon Dec 7 10:03:44 CET 2015


I am using cowboy+cowboy_session for building websites.

each route has something similar as shown below:



-record(state, {

init(_, Req, _Opts) ->
{ok, Req, #state{}}.

handle(Req, State=#state{}) ->

      case  cowboy_session:get("loggedin",  Req) of

         true ->
                       #do something here

          _ ->
                    #not logged in.


{ok, ReqFinal} = cowboy_req:reply(200,
[{<<"content-type">>, <<"text/html">>}],
        <<"Some response">>,

{ok, ReqFinal, State}.

terminate(_Reason, _Req, _State) ->

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?

what I am looking for is a way to check if the user is logged in and if
not, redirect to login route.

PS: I am new to Erlang

Avinash D' Silva
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20151207/19e9c6bb/attachment.htm>

More information about the erlang-questions mailing list