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

Graham Hay grahamrhay@REDACTED
Mon Dec 7 10:38:46 CET 2015


You might be able to do something with a cowboy middleware:

http://ninenines.eu/docs/en/cowboy/HEAD/guide/middlewares/

Or, failing that, you could just extract that case to a module somewhere,
and pass in "do something here" as a fun.

On 7 December 2015 at 09:03, avinash D'silva <evnix.com@REDACTED> wrote:

> 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
>
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20151207/cce293b6/attachment.htm>


More information about the erlang-questions mailing list