add http handler

This commit is contained in:
安礼成 2023-02-23 19:56:09 +08:00
parent 59ab5b7b26
commit e5731cada3
4 changed files with 55 additions and 5 deletions

View File

@ -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">>)}.

View File

@ -0,0 +1,24 @@
%%%-------------------------------------------------------------------
%%% @author licheng5
%%% @copyright (C) 2020, <COMPANY>
%%% @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">>)}.

View File

@ -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, <<AccData/binary, Data/binary>>, Req1};
{more, Data, Req1} ->
read_body(Req1, <<AccData/binary, Data/binary>>)
end.

View File

@ -35,7 +35,7 @@ start_http_server() ->
Dispatcher = cowboy_router:compile([
{'_', [
{"/api/[...]", http_protocol, [http_api_handler]}
{"/host/[...]", http_protocol, [http_host_handler]}
]}
]),
TransOpts = [