81 lines
2.4 KiB
Erlang
81 lines
2.4 KiB
Erlang
%%%-------------------------------------------------------------------
|
|
%% @doc sdlan public API
|
|
%% @end
|
|
%%%-------------------------------------------------------------------
|
|
|
|
-module(sdlan_app).
|
|
|
|
-behaviour(application).
|
|
|
|
-export([start/2, stop/1]).
|
|
|
|
start(_StartType, _StartArgs) ->
|
|
io:setopts([{encoding, unicode}]),
|
|
%% 启动mnesia数据库
|
|
mnesia:start(),
|
|
%% 加速内存的回收
|
|
erlang:system_flag(fullsweep_after, 16),
|
|
|
|
%% 启动注册表
|
|
sdlan_hostname_regedit:init(),
|
|
sdlan_domain_regedit:init(),
|
|
dns_pending_wheel:start(),
|
|
|
|
start_http_server(),
|
|
start_tcp_server(),
|
|
sdlan_sup:start_link().
|
|
|
|
stop(_State) ->
|
|
ok.
|
|
|
|
%% internal functions
|
|
|
|
%% 启动http服务
|
|
start_http_server() ->
|
|
{ok, Props} = application:get_env(sdlan, 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([
|
|
{'_', [
|
|
{"/file/[...]", file_handler, []},
|
|
{"/api/[...]", http_protocol, [api_handler]},
|
|
{"/network/[...]", http_protocol, [network_handler]},
|
|
{"/node/[...]", http_protocol, [node_handler]},
|
|
{"/test/[...]", http_protocol, [test_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("[iot_app] the http server start at: ~p, pid is: ~p", [Port, Pid]).
|
|
|
|
%% 启动tcp服务
|
|
start_tcp_server() ->
|
|
{ok, Props} = application:get_env(sdlan, tcp_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),
|
|
|
|
TransOpts = #{
|
|
num_acceptors => Acceptors,
|
|
max_connections => MaxConnections,
|
|
socket_opts => [
|
|
{port, Port},
|
|
{nodelay, false},
|
|
{backlog, Backlog}
|
|
]
|
|
},
|
|
{ok, _} = ranch:start_listener('sdlan/tcp_server', ranch_tcp, TransOpts, sdlan_channel, []),
|
|
|
|
lager:debug("[sdlan_app] the tcp server start at: ~p", [Port]). |