Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -module(hello_handler).
- -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{}) ->
- {QueryBin, Req1} = cowboy_req:qs(Req),
- QueryStr = binary_to_list(QueryBin),
- QueryTokens = string:tokens(QueryStr, "%20"),
- ReplyStr = string:split(QueryTokens, "&", all),
- Op = lists:nth(1, ReplyStr),
- Result = case Op of
- "sum" -> integer_to_binary(list_to_integer(lists:nth(2, ReplyStr)) + list_to_integer(lists:nth(3, ReplyStr)));
- "sub" -> integer_to_binary(list_to_integer(lists:nth(2, ReplyStr)) - list_to_integer(lists:nth(3, ReplyStr)));
- "mul" -> integer_to_binary(list_to_integer(lists:nth(2, ReplyStr)) * list_to_integer(lists:nth(3, ReplyStr)));
- "div" -> float_to_binary(list_to_integer(lists:nth(2, ReplyStr)) / list_to_integer(lists:nth(3, ReplyStr)))
- end,
- io:fwrite("~p~n",[Result]),
- {ok, Req2} = cowboy_req:reply(200,
- [{<<"content-type">>, <<"text/plain">>}],
- [Result],
- Req1),
- {ok, Req2, State}.
- terminate(_Reason, _Req, _State) ->
- ok.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement