移除endpoint对iot的依赖

This commit is contained in:
anlicheng 2026-05-11 18:10:28 +08:00
parent 07027d071f
commit d02028ff1d
11 changed files with 60 additions and 10 deletions

View File

@ -14,6 +14,7 @@
brod,
hackney,
gproc,
crypto,
kernel,
stdlib
]},

View File

@ -68,7 +68,7 @@ get_protocol(#endpoint{config = #kafka_endpoint{}}) ->
-spec is_support(Protocol :: atom()) -> boolean().
is_support(Protocol) when is_atom(Protocol) ->
{ok, Props} = application:get_env(iot, endpoints),
{ok, Props} = application:get_env(endpoint, endpoints),
SupportProtocols = proplists:get_value(support_protocols, Props, []),
lists:member(Protocol, SupportProtocols).

View File

@ -42,7 +42,7 @@
-spec new(Endpoint :: #endpoint{}, WindowSize :: integer()) -> Buffer :: #buffer{}.
new(Endpoint = #endpoint{id = Id}, WindowSize) when is_integer(WindowSize), WindowSize > 0 ->
%%
{ok, Endpoints} = application:get_env(iot, endpoints),
{ok, Endpoints} = application:get_env(endpoint, endpoints),
RootDir = proplists:get_value(root_dir, Endpoints),
OutboxDir = filename:join(RootDir, integer_to_list(Id)),

View File

@ -45,7 +45,7 @@ start_link(LocalName, Endpoint = #endpoint{config = #http_endpoint{}}) ->
{ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
{stop, Reason :: term()} | ignore).
init([Endpoint = #endpoint{matcher = Matcher, config = #http_endpoint{pool_size = PoolSize}}]) ->
ok = iot_log:set_metadata(),
ok = endpoint_util:set_metadata(),
endpoint_subscription:subscribe(Matcher, self()),
Buffer = endpoint_buffer:new(Endpoint, PoolSize),
{ok, #state{endpoint = Endpoint, buffer = Buffer}}.
@ -151,7 +151,7 @@ read_response_body(ClientRef) ->
-spec patch_headers(Metric :: binary(), any()) -> list().
patch_headers(Metric, Token) when is_binary(Token), Token /= <<>> ->
Sign = iot_util:sha256(erlang:iolist_to_binary([Token, Metric, Token])),
Sign = endpoint_util:sha256(erlang:iolist_to_binary([Token, Metric, Token])),
[{<<"X-Signature">>, Sign}];
patch_headers(_, _) ->
[].

View File

@ -49,7 +49,7 @@ callback_mode() ->
-spec init(term()) -> gen_statem:init_result(kafka_state(), #state{}).
init([Endpoint = #endpoint{id = Id, matcher = Matcher}]) ->
ok = iot_log:set_metadata(),
ok = endpoint_util:set_metadata(),
erlang:process_flag(trap_exit, true),
ok = endpoint_subscription:subscribe(Matcher, self()),
Buffer = endpoint_buffer:new(Endpoint, 10),

View File

@ -51,7 +51,7 @@ unmatched_publish(RouteKey, Content) when is_binary(RouteKey), is_binary(Content
-spec init(term()) -> {ok, #state{}}.
init([]) ->
ok = iot_log:set_metadata(),
ok = endpoint_util:set_metadata(),
case open_log() of
ok ->
{ok, #state{enabled = true}};
@ -126,7 +126,7 @@ open_log() ->
-spec log_config() -> proplists:proplist().
log_config() ->
case application:get_env(iot, endpoint_log) of
case application:get_env(endpoint, endpoint_log) of
{ok, Config} when is_list(Config) ->
Config;
_ ->
@ -140,7 +140,7 @@ log_path(Config) ->
-spec endpoint_root_dir() -> file:filename_all().
endpoint_root_dir() ->
case application:get_env(iot, endpoints) of
case application:get_env(endpoint, endpoints) of
{ok, Endpoints} ->
proplists:get_value(root_dir, Endpoints, ?DEFAULT_ENDPOINT_ROOT_DIR);
undefined ->

View File

@ -50,7 +50,7 @@ callback_mode() ->
-spec init(term()) -> gen_statem:init_result(mqtt_state(), #state{}).
init([Endpoint = #endpoint{matcher = Matcher}]) ->
ok = iot_log:set_metadata(),
ok = endpoint_util:set_metadata(),
erlang:process_flag(trap_exit, true),
ok = endpoint_subscription:subscribe(Matcher, self()),
Buffer = endpoint_buffer:new(Endpoint, 10),

View File

@ -141,7 +141,7 @@ start_link() ->
{ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
{stop, Reason :: term()} | ignore).
init([]) ->
ok = iot_log:set_metadata(),
ok = endpoint_util:set_metadata(),
ExactTid = ets:new(?EXACT_TAB, [named_table, protected, bag, {read_concurrency, true}]),
EdgeTid = ets:new(?TRIE_EDGE_TAB, [named_table, protected, set, {read_concurrency, true}]),
TrieSubTid = ets:new(?TRIE_SUB_TAB, [named_table, protected, bag, {read_concurrency, true}]),

View File

@ -0,0 +1,18 @@
%%%-------------------------------------------------------------------
%%% @doc Endpoint-local utility functions.
%%%-------------------------------------------------------------------
-module(endpoint_util).
-export([set_metadata/0, sha256/1]).
-spec set_metadata() -> ok.
set_metadata() ->
logger:set_process_metadata(#{domain => [iot]}).
-spec sha256(string() | binary()) -> binary().
sha256(Str) when is_list(Str) ->
sha256(unicode:characters_to_binary(Str));
sha256(Bin) when is_binary(Bin) ->
HashBin = crypto:hash(sha256, Bin),
HexStr = lists:flatten([io_lib:format("~2.16.0B", [B]) || B <- binary:bin_to_list(HashBin)]),
list_to_binary(string:lowercase(HexStr)).

View File

@ -52,6 +52,22 @@
]}
]},
{endpoint, [
%% 支持的协议
{endpoints, [
{root_dir, "/usr/local/code/database/"},
{support_protocols, [
http
]}
]},
{endpoint_log, [
{path, "${endpoint_root}/endpoint_log/unmatched_publish.log"},
{max_bytes, 10485760},
{max_files, 10}
]}
]},
%% 系统日志配置,使用 OTP logger
{kernel, [
%% 设置 Logger 的 primary log level

View File

@ -47,6 +47,21 @@
]}
]},
{endpoint, [
{endpoints, [
{root_dir, "/usr/local/code/database/"},
{support_protocols, [
http
]}
]},
{endpoint_log, [
{path, "${endpoint_root}/endpoint_log/unmatched_publish.log"},
{max_bytes, 10485760},
{max_files, 10}
]}
]},
%% 系统日志配置,使用 OTP logger
{kernel, [
%% 设置 Logger 的 primary log level