[erlang-questions] how to better organize code for a website
Vanshdeep Singh
kansi13@REDACTED
Mon Dec 7 10:13:05 CET 2015
Hey,
I think you can use cowboy middleware
https://github.com/ninenines/cowboy/tree/master/examples/markdown_middleware
or you can also use on_request option in cowboy:start_http()
Regards,
Vansh
On Mon, Dec 7, 2015 at 2:33 PM, 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/d0770a4b/attachment.htm>
More information about the erlang-questions
mailing list