[erlang-questions] Cowboy routes with Method as a condition

Loïc Hoguin essen@REDACTED
Wed Feb 13 11:39:00 CET 2013


On 02/13/2013 11:34 AM, Max Lapshin wrote:
> handle(Req, State) ->
>      handle_method(cowboy_req:get(method, Req), Req, State).  will require
>
>
> handle_method(<<"GET">>, Req, State)
> handle_method(<<"DELETE">>, Req, State)
> handle_method(Other, Req, State)
>    here we copy from module to module default reply on "method not
> supported" or 404, whatever we choose.
>
>
> with method constraints code can look so:
>
>
> init(_, Req, [Action]) ->
>   {ok, Req, Action}.
>
>
> handle(Req, show) ->
>    ..
>
> handle(Req, destroy) ->
>    ..
>
> handle(Req, list) ->
>    ..
>    .
>
> And no "default handler" because it is not required. Only good requests
> can pass to this handler.

Then in your init function do something like your_lib:route_method(Req) 
which returns {ok, Req, Action} if it's good or {shutdown, ...} if not? 
It'll do exactly what you just said.

-- 
Loïc Hoguin
Erlang Cowboy
Nine Nines
http://ninenines.eu



More information about the erlang-questions mailing list