Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
add basic action handling
  • Loading branch information
janl committed Oct 31, 2014
1 parent 58c4948 commit e8c49669f950b5461ef79dfefd4767010f92c216
Showing 1 changed file with 34 additions and 1 deletion.
@@ -15,5 +15,38 @@
-export([handle_setup_req/1]).

handle_setup_req(Req) ->
io:format("~nHandle Req: ~p~n", [Req]).
ok = chttpd:verify_is_server_admin(Req),
% TBD uncomment after devving
%couch_httpd:validate_ctype(Req, "application/json"),
Setup = get_body(Req),
io:format("~nSetup: ~p~n", [Setup]),
Action = binary_to_list(couch_util:get_value(<<"action">>, Setup, <<"missing">>)),
case handle_action(Action, Setup) of
ok ->
chttpd:send_json(Req, 201, {[{ok, true}]});
{error, Message} ->
couch_httpd:send_error(Req, 400, <<"bad_request">>, Message)
end.


handle_action("enable_cluster", Setup) ->
io:format("~nenable_cluster: ~p~n", [Setup]);
handle_action("finish_cluster", Setup) ->
io:format("~nfinish_cluster: ~p~n", [Setup]);
handle_action("add_node", Setup) ->
io:format("~nadd_node: ~p~n", [Setup]);
handle_action("remove_node", Setup) ->
io:format("~nremove_node: ~p~n", [Setup]);
handle_action(_, _) ->
io:format("~ninvalid_action: ~n", []),
{error, <<"Invalid Action'">>}.


get_body(Req) ->
case catch couch_httpd:json_body_obj(Req) of
{Body} ->
Body;
Else ->
io:format("~nBody Fail: ~p~n", [Else]),
couch_httpd:send_error(Req, 400, <<"bad_request">>, <<"Missing JSON body'">>)
end.

0 comments on commit e8c4966

Please sign in to comment.