fix http handler
This commit is contained in:
parent
8b0dcb8c4f
commit
28ff371de5
@ -15,16 +15,14 @@
|
||||
init(Req0, 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, ReqBody, Req1} = parse_body(Req0),
|
||||
Sign = cowboy_req:header(<<"sign">>, Req1, <<>>),
|
||||
case njau_bot_signer:sign(ReqBody) =:= Sign of
|
||||
true ->
|
||||
{ok, StatusCode, Resp} = handle_request(Method, Path, GetParams, ReqBody),
|
||||
lager:debug("[http_protocol] request path: ~p, get_params: ~p, post_params: ~p, response: ~ts",
|
||||
[Path, GetParams, ReqBody, Resp]),
|
||||
{ok, StatusCode, Resp} = handle_request(Method, Path, ReqBody),
|
||||
lager:debug("[http_protocol] request path: ~p, post_params: ~p, response: ~ts",
|
||||
[Path, ReqBody, Resp]),
|
||||
Req2 = cowboy_req:reply(StatusCode, #{
|
||||
<<"Content-Type">> => <<"application/json">>
|
||||
}, Resp, Req1),
|
||||
@ -37,14 +35,54 @@ init(Req0, Opts) ->
|
||||
{ok, Req2, Opts}
|
||||
end.
|
||||
|
||||
handle_request("POST", "/api/device_info", _, Params) ->
|
||||
njau_bot_logger:write(jiffy:encode(Params, [force_utf8])),
|
||||
%% 场地信息
|
||||
handle_request("POST", "/storeInfo", ReqBody) when is_binary(ReqBody) ->
|
||||
#{<<"storeInfos">> := StoreInfos} = jiffy:decode(ReqBody, [return_maps]),
|
||||
lager:debug("[api_handler] get storeInfo: ~p", [StoreInfos]),
|
||||
|
||||
{ok, 200, json_data(<<"ok">>)};
|
||||
njau_bot_logger:write(ReqBody),
|
||||
|
||||
handle_request(_, Path, _, _) ->
|
||||
{ok, 200, json_reply(true, <<"接收成功"/utf8>>)};
|
||||
|
||||
%% 设备信息
|
||||
handle_request("POST", "/equip", ReqBody) ->
|
||||
#{<<"equips">> := Equips} = jiffy:decode(ReqBody, [return_maps]),
|
||||
lager:debug("[api_handler] get equips: ~p", [Equips]),
|
||||
|
||||
njau_bot_logger:write(ReqBody),
|
||||
|
||||
{ok, 200, json_reply(true, <<"接收成功"/utf8>>)};
|
||||
|
||||
%% 设备离线,在线状态
|
||||
handle_request("POST", "/online", ReqBody) ->
|
||||
#{<<"equipStatus">> := Equips} = jiffy:decode(ReqBody, [return_maps]),
|
||||
lager:debug("[api_handler] get online: ~p", [Equips]),
|
||||
|
||||
njau_bot_logger:write(ReqBody),
|
||||
|
||||
{ok, 200, json_reply(true, <<"接收成功"/utf8>>)};
|
||||
|
||||
%% 设备运行状态
|
||||
handle_request("POST", "/runStatus", ReqBody) ->
|
||||
#{<<"equipRunStatus">> := EquipRunStatus} = jiffy:decode(ReqBody, [return_maps]),
|
||||
lager:debug("[api_handler] get equipRunStatus: ~p", [EquipRunStatus]),
|
||||
|
||||
njau_bot_logger:write(ReqBody),
|
||||
|
||||
{ok, 200, json_reply(true, <<"接收成功"/utf8>>)};
|
||||
|
||||
%% 订单信息推送
|
||||
handle_request("POST", "/order", ReqBody) ->
|
||||
%#{<<"equipRunStatus">> := EquipRunStatus} = jiffy:decode(ReqBody, [return_maps]),
|
||||
lager:debug("[api_handler] get order: ~p", [ReqBody]),
|
||||
|
||||
njau_bot_logger:write(ReqBody),
|
||||
|
||||
{ok, 200, json_reply(true, <<"接收成功"/utf8>>)};
|
||||
|
||||
handle_request(_, Path, _) ->
|
||||
Path1 = list_to_binary(Path),
|
||||
{ok, 200, json_error(-1, <<"url: ", Path1/binary, " not found">>)}.
|
||||
{ok, 200, json_reply(false, <<"url: ", Path1/binary, " not found">>)}.
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%% helper methods
|
||||
@ -71,14 +109,9 @@ read_body(Req, AccData) ->
|
||||
read_body(Req1, <<AccData/binary, Data/binary>>)
|
||||
end.
|
||||
|
||||
json_data(Data) ->
|
||||
Json = jiffy:encode(#{<<"result">> => Data}, [force_utf8]),
|
||||
iolist_to_binary(Json).
|
||||
|
||||
json_error(ErrCode, ErrMessage) when is_integer(ErrCode), is_binary(ErrMessage) ->
|
||||
jiffy:encode(#{
|
||||
<<"error">> => #{
|
||||
<<"code">> => ErrCode,
|
||||
<<"message">> => ErrMessage
|
||||
}
|
||||
}, [force_utf8]).
|
||||
json_reply(Result, Message) when is_boolean(Result), is_binary(Message) ->
|
||||
Json = jiffy:encode(#{
|
||||
<<"result">> => Result,
|
||||
<<"message">> => Message
|
||||
}, [force_utf8]),
|
||||
iolist_to_binary(Json).
|
||||
@ -35,7 +35,7 @@ start_http_server() ->
|
||||
|
||||
Dispatcher = cowboy_router:compile([
|
||||
{'_', [
|
||||
{"/api/[...]", api_handler, []}
|
||||
{"/[...]", api_handler, []}
|
||||
]}
|
||||
]),
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user