From e5731cada3afcea8df19bb8873c2276f0771378d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=89=E7=A4=BC=E6=88=90?= Date: Thu, 23 Feb 2023 19:56:09 +0800 Subject: [PATCH] add http handler --- apps/iot/src/http_api_handler.erl | 4 +++- apps/iot/src/http_host_handler.erl | 24 ++++++++++++++++++++++++ apps/iot/src/http_protocol.erl | 30 +++++++++++++++++++++++++++--- apps/iot/src/iot_app.erl | 2 +- 4 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 apps/iot/src/http_host_handler.erl diff --git a/apps/iot/src/http_api_handler.erl b/apps/iot/src/http_api_handler.erl index 327f1b4..ce9c853 100644 --- a/apps/iot/src/http_api_handler.erl +++ b/apps/iot/src/http_api_handler.erl @@ -16,5 +16,7 @@ %% helper methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -handle_request("GET", "/api/booking", _, _Params) -> +handle_request(_, "/api/booking", _, _Params) -> + {ok, 200, iot_util:json_data(<<"success">>)}; +handle_request("POST", "/api/booking", _, _Params) -> {ok, 200, iot_util:json_data(<<"success">>)}. \ No newline at end of file diff --git a/apps/iot/src/http_host_handler.erl b/apps/iot/src/http_host_handler.erl new file mode 100644 index 0000000..c856cfd --- /dev/null +++ b/apps/iot/src/http_host_handler.erl @@ -0,0 +1,24 @@ +%%%------------------------------------------------------------------- +%%% @author licheng5 +%%% @copyright (C) 2020, +%%% @doc +%%% +%%% @end +%%% Created : 26. 4月 2020 3:36 下午 +%%%------------------------------------------------------------------- +-module(http_host_handler). +-author("licheng5"). + +%% API +-export([handle_request/4]). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% helper methods +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +handle_request(_, "/api/booking", _, _Params) -> + {ok, 200, iot_util:json_data(<<"success">>)}; +handle_request("POST", "/host/update", _, _Params) -> + lager:debug("[host_handler] post params is: ~p", [_Params]), + + {ok, 200, iot_util:json_data(<<"success">>)}. \ No newline at end of file diff --git a/apps/iot/src/http_protocol.erl b/apps/iot/src/http_protocol.erl index 4b1500c..65ca36e 100644 --- a/apps/iot/src/http_protocol.erl +++ b/apps/iot/src/http_protocol.erl @@ -14,15 +14,13 @@ init(Req0, Opts = [Mod|_]) -> StartTs = erlang:monotonic_time(), - lager:debug("opts is: ~p", [Opts]), Method = binary_to_list(cowboy_req:method(Req0)), Path = binary_to_list(cowboy_req:path(Req0)), GetParams0 = cowboy_req:parse_qs(Req0), GetParams = maps:from_list(GetParams0), + {ok, PostParams, Req1} = parse_body(Req0), - {ok, PostParams0, Req1} = cowboy_req:read_urlencoded_body(Req0), - PostParams = maps:from_list(PostParams0), try Mod:handle_request(Method, Path, GetParams, PostParams) of {ok, StatusCode, Resp} -> lager:debug("[http_protocol] get a request, path: ~p, get params: ~p, post params: ~p, response: ~ts", @@ -66,4 +64,30 @@ supported_gzip(AcceptEncoding) when is_binary(AcceptEncoding) -> false; _ -> true + end. + +parse_body(Req0) -> + ContentType = cowboy_req:header(<<"content-type">>, Req0), + case ContentType of + <<"application/json">> -> + {ok, Body, Req1} = read_body(Req0), + {ok, catch jiffy:decode(Body, [return_maps]), Req1}; + <<"application/x-www-form-urlencoded">> -> + {ok, PostParams0, Req1} = cowboy_req:read_urlencoded_body(Req0), + PostParams = maps:from_list(PostParams0), + {ok, PostParams, Req1}; + _ -> + lager:warning("[http_handler] unsupport content-type is: ~p", [ContentType]), + {ok, #{}, Req0} + end. + +%% 读取请求体 +read_body(Req) -> + read_body(Req, <<>>). +read_body(Req, AccData) -> + case cowboy_req:read_body(Req) of + {ok, Data, Req1} -> + {ok, <>, Req1}; + {more, Data, Req1} -> + read_body(Req1, <>) end. \ No newline at end of file diff --git a/apps/iot/src/iot_app.erl b/apps/iot/src/iot_app.erl index 46d6d65..1a118a2 100644 --- a/apps/iot/src/iot_app.erl +++ b/apps/iot/src/iot_app.erl @@ -35,7 +35,7 @@ start_http_server() -> Dispatcher = cowboy_router:compile([ {'_', [ - {"/api/[...]", http_protocol, [http_api_handler]} + {"/host/[...]", http_protocol, [http_host_handler]} ]} ]), TransOpts = [