[erlang-questions] how to better organize code for a website
avinash D'silva
evnix.com@REDACTED
Mon Dec 7 10:03:44 CET 2015
Hi,
I am using cowboy+cowboy_session for building websites.
each route has something similar as shown below:
-module(get_pageX).
-behaviour(cowboy_http_handler).
-export([init/3]).
-export([handle/2]).
-export([terminate/3]).
-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.
end,
{ok, ReqFinal} = cowboy_req:reply(200,
[{<<"content-type">>, <<"text/html">>}],
<<"Some response">>,
Req),
{ok, ReqFinal, State}.
terminate(_Reason, _Req, _State) ->
ok.
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
Regards,
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