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

avinash D'silva evnix.com@REDACTED
Mon Dec 7 11:39:07 CET 2015


wow!! thanks!

It worked perfectly.


is there a way to assign the middleware to specific routes?  currently all
the public routes are blocked due to the login middleware.




On Mon, Dec 7, 2015 at 3:08 PM, Graham Hay <grahamrhay@REDACTED> wrote:

> 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
>>
>>
>


-- 
Powered By codologic <http://codologic.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20151207/a423f3bf/attachment.htm>


More information about the erlang-questions mailing list