diff --git a/apps/light/src/http_protocol.erl b/apps/light/src/http_protocol.erl deleted file mode 100644 index 16e2ad4..0000000 --- a/apps/light/src/http_protocol.erl +++ /dev/null @@ -1,92 +0,0 @@ -%%%------------------------------------------------------------------- -%%% @author licheng5 -%%% @copyright (C) 2020, -%%% @doc -%%% -%%% @end -%%% Created : 26. 4月 2020 3:36 下午 -%%%------------------------------------------------------------------- --module(http_protocol). --author("licheng5"). - -%% API --export([init/2]). --export([json_data/1, json_error/2]). - -init(Req0, Opts = [Mod|_]) -> - Method = binary_to_list(cowboy_req:method(Req0)), - Path = binary_to_list(cowboy_req:path(Req0)), - - QueryStr = cowboy_req:qs(Req0), - {ok, Body, Req1} = parse_body(Req0), - - try Mod:handle_request(Method, Path, QueryStr, Body) of - {ok, StatusCode, Resp} -> - AcceptEncoding = cowboy_req:header(<<"accept-encoding">>, Req1, <<>>), - Req2 = case iolist_size(Resp) >= 1024 andalso supported_gzip(AcceptEncoding) of - true -> - Resp1 = zlib:gzip(Resp), - cowboy_req:reply(StatusCode, #{ - <<"Content-Type">> => <<"application/json;charset=utf-8">>, - <<"Content-Encoding">> => <<"gzip">> - }, Resp1, Req1); - false -> - cowboy_req:reply(StatusCode, #{ - <<"Content-Type">> => <<"application/json;charset=utf-8">> - }, Resp, Req1) - end, - {ok, Req2, Opts} - catch - throw:Error -> - ErrResp = json_error(-1, Error), - Req2 = cowboy_req:reply(404, #{ - <<"Content-Type">> => <<"application/json;charset=utf-8">> - }, ErrResp, Req1), - {ok, Req2, Opts}; - _:Error:Stack -> - lager:warning("[http_handler] get error: ~p, stack: ~p", [Error, Stack]), - Req2 = cowboy_req:reply(500, #{ - <<"Content-Type">> => <<"text/html;charset=utf-8">> - }, <<"Internal Server Error">>, Req1), - {ok, Req2, Opts} - end. - -%% 判断是否支持gzip -supported_gzip(AcceptEncoding) when is_binary(AcceptEncoding) -> - binary:match(AcceptEncoding, <<"gzip">>) =/= nomatch. - -parse_body(Req0) -> - ContentType = cowboy_req:header(<<"content-type">>, Req0), - case ContentType of - <<"application/x-www-form-urlencoded">> -> - {ok, PostParams0, Req1} = cowboy_req:read_urlencoded_body(Req0), - PostParams = maps:from_list(PostParams0), - {ok, jiffy:encode(PostParams, [force_utf8]), Req1}; - _ -> - {ok, Body, Req1} = read_body(Req0), - {ok, Body, Req1} - 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. - -json_data(Data) -> - jiffy:encode(#{ - <<"result">> => Data - }, [force_utf8]). - -json_error(ErrCode, ErrMessage) when is_integer(ErrCode), is_binary(ErrMessage) -> - jiffy:encode(#{ - <<"error">> => #{ - <<"code">> => ErrCode, - <<"message">> => ErrMessage - } - }, [force_utf8]). \ No newline at end of file diff --git a/apps/light/src/light.app.src b/apps/light/src/light.app.src index 1dccd3f..44e3925 100644 --- a/apps/light/src/light.app.src +++ b/apps/light/src/light.app.src @@ -5,12 +5,8 @@ {mod, {light_app, []}}, {applications, [ - hackney, - sync, jiffy, - ranch, emqtt, - cowboy, lager, kernel, stdlib diff --git a/apps/light/src/light_app.erl b/apps/light/src/light_app.erl index 6c4b676..6ab8582 100644 --- a/apps/light/src/light_app.erl +++ b/apps/light/src/light_app.erl @@ -10,35 +10,7 @@ -export([start/2, stop/1]). start(_StartType, _StartArgs) -> - SupResult = light_sup:start_link(), - start_http_server(), - SupResult. + light_sup:start_link(). stop(_State) -> ok. - -%% internal functions - -start_http_server() -> - {ok, Props} = application:get_env(light, http_server), - Acceptors = proplists:get_value(acceptors, Props, 50), - MaxConnections = proplists:get_value(max_connections, Props, 10240), - Backlog = proplists:get_value(backlog, Props, 1024), - Port = proplists:get_value(port, Props), - - Dispatcher = cowboy_router:compile([ - {'_', [ - %{"/api/[...]", http_protocol, [http_push_handler]} - ]} - ]), - - TransOpts = [ - {port, Port}, - {num_acceptors, Acceptors}, - {backlog, Backlog}, - {max_connections, MaxConnections} - ], - - {ok, Pid} = cowboy:start_clear(http_listener, TransOpts, #{env => #{dispatch => Dispatcher}}), - - lager:debug("[light_app] the http server start at: ~p, pid is: ~p", [Port, Pid]). diff --git a/config/sys-dev.config b/config/sys-dev.config index 43d3995..a8c3181 100644 --- a/config/sys-dev.config +++ b/config/sys-dev.config @@ -1,13 +1,6 @@ [ {light, [ - {http_server, [ - {port, 18082}, - {acceptors, 500}, - {max_connections, 10240}, - {backlog, 10240} - ]}, - %% 离线判断时间间隔,单位:(秒) {heartbeat_ticker, 120}, diff --git a/config/sys-prod.config b/config/sys-prod.config index e13abdf..1098232 100644 --- a/config/sys-prod.config +++ b/config/sys-prod.config @@ -1,13 +1,6 @@ [ {light, [ - {http_server, [ - {port, 18082}, - {acceptors, 500}, - {max_connections, 10240}, - {backlog, 10240} - ]}, - %% 离线判断时间间隔,单位:(秒) {heartbeat_ticker, 5400}, diff --git a/rebar.config b/rebar.config index 4e1c9d7..72f5d4a 100644 --- a/rebar.config +++ b/rebar.config @@ -1,13 +1,7 @@ {erl_opts, [debug_info]}. {deps, [ - {poolboy, ".*", {git, "https://github.com/devinus/poolboy.git", {tag, "1.5.1"}}}, - {hackney, ".*", {git, "https://github.com/benoitc/hackney.git", {tag, "1.16.0"}}}, - {sync, ".*", {git, "https://github.com/rustyio/sync.git", {branch, "master"}}}, {jiffy, ".*", {git, "https://github.com/davisp/jiffy.git", {tag, "1.1.1"}}}, - {mysql, ".*", {git, "https://github.com/mysql-otp/mysql-otp", {tag, "1.8.0"}}}, {emqtt, ".*", {git, "https://gitea.s5s8.com/anlicheng/emqtt.git", {tag, "v1.2"}}}, - {cowboy, ".*", {git, "https://github.com/ninenines/cowboy.git", {tag, "2.10.0"}}}, - {parse_trans, ".*", {git, "https://github.com/uwiger/parse_trans", {tag, "3.0.0"}}}, {lager, ".*", {git,"https://github.com/erlang-lager/lager.git", {tag, "3.9.2"}}} ]}. diff --git a/rebar.lock b/rebar.lock index 1700979..ff764d8 100644 --- a/rebar.lock +++ b/rebar.lock @@ -1,24 +1,9 @@ {"1.2.0", -[{<<"certifi">>,{pkg,<<"certifi">>,<<"2.5.2">>},1}, - {<<"cowboy">>, - {git,"https://github.com/ninenines/cowboy.git", - {ref,"9e600f6c1df3c440bc196b66ebbc005d70107217"}}, - 0}, - {<<"cowlib">>, - {git,"https://github.com/ninenines/cowlib", - {ref,"cc04201c1d0e1d5603cd1cde037ab729b192634c"}}, - 1}, - {<<"emqtt">>, +[{<<"emqtt">>, {git,"https://gitea.s5s8.com/anlicheng/emqtt.git", {ref,"c5a52dcb57cd23a4318e342b705f9f726e8d67a1"}}, 0}, - {<<"fs">>,{pkg,<<"fs">>,<<"6.1.1">>},1}, {<<"goldrush">>,{pkg,<<"goldrush">>,<<"0.1.9">>},1}, - {<<"hackney">>, - {git,"https://github.com/benoitc/hackney.git", - {ref,"f3e9292db22c807e73f57a8422402d6b423ddf5f"}}, - 0}, - {<<"idna">>,{pkg,<<"idna">>,<<"6.0.1">>},1}, {<<"jiffy">>, {git,"https://github.com/davisp/jiffy.git", {ref,"9ea1b35b6e60ba21dfd4adbd18e7916a831fd7d4"}}, @@ -26,48 +11,10 @@ {<<"lager">>, {git,"https://github.com/erlang-lager/lager.git", {ref,"459a3b2cdd9eadd29e5a7ce5c43932f5ccd6eb88"}}, - 0}, - {<<"metrics">>,{pkg,<<"metrics">>,<<"1.0.1">>},1}, - {<<"mimerl">>,{pkg,<<"mimerl">>,<<"1.2.0">>},1}, - {<<"mysql">>, - {git,"https://github.com/mysql-otp/mysql-otp", - {ref,"caf5ff96c677a8fe0ce6f4082bc036c8fd27dd62"}}, - 0}, - {<<"parse_trans">>, - {git,"https://github.com/uwiger/parse_trans", - {ref,"6f3645afb43c7c57d61b54ef59aecab288ce1013"}}, - 0}, - {<<"poolboy">>, - {git,"https://github.com/devinus/poolboy.git", - {ref,"3bb48a893ff5598f7c73731ac17545206d259fac"}}, - 0}, - {<<"ranch">>, - {git,"https://github.com/ninenines/ranch", - {ref,"a692f44567034dacf5efcaa24a24183788594eb7"}}, - 1}, - {<<"ssl_verify_fun">>,{pkg,<<"ssl_verify_fun">>,<<"1.1.6">>},1}, - {<<"sync">>, - {git,"https://github.com/rustyio/sync.git", - {ref,"7dc303ed4ce8d26db82e171dbbd7c41067852c65"}}, - 0}, - {<<"unicode_util_compat">>,{pkg,<<"unicode_util_compat">>,<<"0.5.0">>},2}]}. + 0}]}. [ {pkg_hash,[ - {<<"certifi">>, <<"B7CFEAE9D2ED395695DD8201C57A2D019C0C43ECAF8B8BCB9320B40D6662F340">>}, - {<<"fs">>, <<"9D147B944D60CFA48A349F12D06C8EE71128F610C90870BDF9A6773206452ED0">>}, - {<<"goldrush">>, <<"F06E5D5F1277DA5C413E84D5A2924174182FB108DABB39D5EC548B27424CD106">>}, - {<<"idna">>, <<"1D038FB2E7668CE41FBF681D2C45902E52B3CB9E9C77B55334353B222C2EE50C">>}, - {<<"metrics">>, <<"25F094DEA2CDA98213CECC3AEFF09E940299D950904393B2A29D191C346A8486">>}, - {<<"mimerl">>, <<"67E2D3F571088D5CFD3E550C383094B47159F3EEE8FFA08E64106CDF5E981BE3">>}, - {<<"ssl_verify_fun">>, <<"CF344F5692C82D2CD7554F5EC8FD961548D4FD09E7D22F5B62482E5AEAEBD4B0">>}, - {<<"unicode_util_compat">>, <<"8516502659002CEC19E244EBD90D312183064BE95025A319A6C7E89F4BCCD65B">>}]}, + {<<"goldrush">>, <<"F06E5D5F1277DA5C413E84D5A2924174182FB108DABB39D5EC548B27424CD106">>}]}, {pkg_hash_ext,[ - {<<"certifi">>, <<"3B3B5F36493004AC3455966991EAF6E768CE9884693D9968055AEEEB1E575040">>}, - {<<"fs">>, <<"EF94E95FFE79916860649FED80AC62B04C322B0BB70F5128144C026B4D171F8B">>}, - {<<"goldrush">>, <<"99CB4128CFFCB3227581E5D4D803D5413FA643F4EB96523F77D9E6937D994CEB">>}, - {<<"idna">>, <<"A02C8A1C4FD601215BB0B0324C8A6986749F807CE35F25449EC9E69758708122">>}, - {<<"metrics">>, <<"69B09ADDDC4F74A40716AE54D140F93BEB0FB8978D8636EADED0C31B6F099F16">>}, - {<<"mimerl">>, <<"F278585650AA581986264638EBF698F8BB19DF297F66AD91B18910DFC6E19323">>}, - {<<"ssl_verify_fun">>, <<"BDB0D2471F453C88FF3908E7686F86F9BE327D065CC1EC16FA4540197EA04680">>}, - {<<"unicode_util_compat">>, <<"D48D002E15F5CC105A696CF2F1BBB3FC72B4B770A184D8420C8DB20DA2674B38">>}]} + {<<"goldrush">>, <<"99CB4128CFFCB3227581E5D4D803D5413FA643F4EB96523F77D9E6937D994CEB">>}]} ].