sdlan/src/sdlan_app.erl
2026-04-16 15:27:18 +08:00

68 lines
1.9 KiB
Erlang

%%%-------------------------------------------------------------------
%% @doc sdlan public API
%% @end
%%%-------------------------------------------------------------------
-module(sdlan_app).
-behaviour(application).
-export([start/2, stop/1]).
start(_StartType, _StartArgs) ->
io:setopts([{encoding, unicode}]),
%% 加速内存的回收
erlang:system_flag(fullsweep_after, 16),
%% 设置环境变量
case sdlan_util:ipv6_assist_info() of
{ok, V6Info} ->
application:set_env(sdlan, ipv6_assist_info, V6Info);
undefined ->
ok
end,
%% 启动注册表
sdlan_hostname_regedit:init(),
sdlan_domain_regedit:init(),
sdlan_token_store:init(),
%% 权限的数据管理
identity_policy_ets:init(),
rule_ets:init(),
start_http_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([
{'_', [
{"/api/[...]", http_protocol, [api_handler]},
{"/binlog", http_protocol, [binlog_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}}),
logger:debug("[iot_app] the http server start at: ~p, pid is: ~p", [Port, Pid]).