chore(gw): integrate config-handler

This commit is contained in:
JianBo He 2021-08-26 11:34:12 +08:00 committed by turtleDeng
parent 86e28d5abb
commit bce130d9f9
4 changed files with 53 additions and 14 deletions

View File

@ -74,13 +74,13 @@ start(Name) ->
stop(Name) ->
emqx_gateway_sup:stop_gateway_insta(Name).
-spec update_rawconf(gateway_name(), emqx_config:raw_config())
-spec update_rawconf(binary(), emqx_config:raw_config())
-> ok
| {error, any()}.
update_rawconf(_Name, _RawConf) ->
%% TODO:
ok.
update_rawconf(RawName, RawConfDiff) ->
emqx:update_config([gateway], {RawName, RawConfDiff}).
%%--------------------------------------------------------------------
%% Internal funcs
%%--------------------------------------------------------------------

View File

@ -357,13 +357,23 @@ gateway_insta(get, Request) ->
Name = binary_to_existing_atom(cowboy_req:binding(name, Request)),
case emqx_gateway:lookup(Name) of
#{config := Config} ->
%% TODO: ??? RawConf or Config ??
%% TODO: ??? RawConf or Config or RunningState ???
{200, Config};
undefined ->
{404, <<"Not Found">>}
end;
gateway_insta(post, _Request) ->
{200, ok}.
gateway_insta(post, Request) ->
Name = binary_to_existing_atom(cowboy_req:binding(name, Request)),
{ok, RawConf, _NRequest} = cowboy_req:read_body(Request),
%% XXX: Consistence ??
case emqx_gateway:update_rawconf(Name, RawConf) of
ok ->
{200, ok};
{error, not_found} ->
{404, <<"Not Found">>};
{error, Reason} ->
{500, Reason}
end.
gateway_insta_stats(get, _Req) ->
{401, <<"Implement it later (maybe 5.1)">>}.

View File

@ -17,23 +17,52 @@
-module(emqx_gateway_app).
-behaviour(application).
-behaviour(emqx_config_handler).
-include_lib("emqx/include/logger.hrl").
-export([start/2, stop/1]).
-export([ pre_config_update/2
, post_config_update/3
]).
start(_StartType, _StartArgs) ->
{ok, Sup} = emqx_gateway_sup:start_link(),
emqx_gateway_cli:load(),
load_default_gateway_applications(),
load_gateway_by_default(),
emqx_config_handler:add_handler([gateway], ?MODULE),
{ok, Sup}.
stop(_State) ->
emqx_gateway_cli:unload(),
%% XXX: No api now
%emqx_config_handler:remove_handler([gateway], ?MODULE),
ok.
%%--------------------------------------------------------------------
%% Config Handler
%% All of update_request is created by emqx_gateway_xx_api.erl module
-spec pre_config_update(emqx_config:update_request(), emqx_config:raw_config()) ->
{ok, emqx_config:update_request()} | {error, term()}.
pre_config_update({RawName, RawConfDiff}, RawConf) ->
{ok, emqx_map_lib:deep_merge(RawConf, #{RawName => RawConfDiff})}.
-spec post_config_update(emqx_config:update_request(), emqx_config:config(),
emqx_config:config()) -> ok | {ok, Result::any()} | {error, Reason::term()}.
post_config_update({RawName, _}, NewConfig, OldConfig) ->
GwName = binary_to_existing_atom(RawName),
SubConf = maps:get(GwName, NewConfig),
case maps:get(GwName, OldConfig, undefined) of
undefined ->
emqx_gateway:load(GwName, SubConf);
_ ->
emqx_gateway:update(GwName, SubConf)
end.
%%--------------------------------------------------------------------
%% Internal funcs

View File

@ -85,7 +85,7 @@ fields(mqttsn_predefined) ->
];
fields(coap_structs) ->
[ {heartbeat, t(duration(), undefined, "30s")}
[ {heartbeat, t(duration(), undefined, <<"30s">>)}
, {notify_type, t(union([non, con, qos]), undefined, qos)}
, {subscribe_qos, t(union([qos0, qos1, qos2, coap]), undefined, coap)}
, {publish_qos, t(union([qos0, qos1, qos2, coap]), undefined, coap)}
@ -169,12 +169,12 @@ fields(listener_settings) ->
, {proxy_protocol, t(boolean())}
, {proxy_protocol_timeout, t(duration())}
, {backlog, t(integer(), undefined, 1024)}
, {send_timeout, t(duration(), undefined, "15s")} %% FIXME: mapping it
, {send_timeout, t(duration(), undefined, <<"15s">>)}
, {send_timeout_close, t(boolean(), undefined, true)}
, {recbuf, t(bytesize())}
, {sndbuf, t(bytesize())}
, {buffer, t(bytesize())}
, {high_watermark, t(bytesize(), undefined, "1MB")}
, {high_watermark, t(bytesize(), undefined, <<"1MB">>)}
, {tune_buffer, t(boolean())}
, {nodelay, t(boolean())}
, {reuseaddr, t(boolean())}
@ -189,7 +189,7 @@ fields(ssl_listener_settings) ->
[
%% some special confs for ssl listener
] ++
ssl(undefined, #{handshake_timeout => "15s"
ssl(undefined, #{handshake_timeout => <<"15s">>
, depth => 10
, reuse_sessions => true}) ++ fields(listener_settings);
@ -202,7 +202,7 @@ fields(dtls_listener_settings) ->
[
%% some special confs for dtls listener
] ++
ssl(undefined, #{handshake_timeout => "15s"
ssl(undefined, #{handshake_timeout => <<"15s">>
, depth => 10
, reuse_sessions => true}) ++ fields(listener_settings);
@ -241,7 +241,7 @@ authentication() ->
gateway_common_options() ->
[ {enable, t(boolean(), undefined, true)}
, {enable_stats, t(boolean(), undefined, true)}
, {idle_timeout, t(duration(), undefined, "30s")}
, {idle_timeout, t(duration(), undefined, <<"30s">>)}
, {mountpoint, t(binary())}
, {clientinfo_override, t(ref(clientinfo_override))}
, {authentication, t(authentication(), undefined, undefined)}