Permalink
Browse files

allow user-defined increment

  • Loading branch information...
stolen committed Jun 28, 2014
1 parent f9ba3fb commit a1103c48fdacd7a443f43107b718072b226fb13c
Showing with 12 additions and 2 deletions.
  1. +2 −1 src/erdico.erl
  2. +10 −1 src/erdico_handler.erl
View
@@ -35,8 +35,9 @@ init(root) ->
%%% Internals
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
start_cowboy() ->
UserInc = {"/inc/:amount", erdico_handler, user_inc},
DefPath = {'_', erdico_handler, []}, % Catch-all path
Host = {'_', [DefPath]}, % No virtualhosts
Host = {'_', [UserInc, DefPath]}, % No virtualhosts
Dispatch = cowboy_router:compile([Host]),
Env = [{env, [{dispatch, Dispatch}]}],
Hooks = configured_hooks(),
View
@@ -3,13 +3,22 @@
 
-export([init/3, handle/2, terminate/3]).
init(_Type, Req, _Options) ->
init(_Type, Req, user_inc) ->
{ok, Req, user_inc};
init(_Type, Req, []) ->
{ok, Req, nostate}.
handle(Req, nostate) ->
{ok, CounterValue} = erdico_counter:inc(),
ResponseBody = <<"value = ", (integer_to_binary(CounterValue))/binary, "\n">>,
{ok, Replied} = cowboy_req:reply(200, [], ResponseBody, Req),
{ok, Replied, nostate};
handle(Req0, user_inc) ->
{AmountBin, Req} = cowboy_req:binding(amount, Req0),
{ok, CounterValue} = erdico_counter:inc(binary_to_integer(AmountBin)),
ResponseBody = <<"value = ", (integer_to_binary(CounterValue))/binary, "\n">>,
{ok, Replied} = cowboy_req:reply(200, [], ResponseBody, Req),
{ok, Replied, nostate}.
terminate(_Reason, _Req, nostate) ->

0 comments on commit a1103c4

Please sign in to comment.