From bce130d9f959ad8736057ee4987f38af5c46a9bb Mon Sep 17 00:00:00 2001 From: JianBo He Date: Thu, 26 Aug 2021 11:34:12 +0800 Subject: [PATCH] chore(gw): integrate config-handler --- apps/emqx_gateway/src/emqx_gateway.erl | 8 ++--- apps/emqx_gateway/src/emqx_gateway_api.erl | 16 ++++++++-- apps/emqx_gateway/src/emqx_gateway_app.erl | 31 ++++++++++++++++++- apps/emqx_gateway/src/emqx_gateway_schema.erl | 12 +++---- 4 files changed, 53 insertions(+), 14 deletions(-) diff --git a/apps/emqx_gateway/src/emqx_gateway.erl b/apps/emqx_gateway/src/emqx_gateway.erl index 213208f19..d8b4125ce 100644 --- a/apps/emqx_gateway/src/emqx_gateway.erl +++ b/apps/emqx_gateway/src/emqx_gateway.erl @@ -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 %%-------------------------------------------------------------------- + diff --git a/apps/emqx_gateway/src/emqx_gateway_api.erl b/apps/emqx_gateway/src/emqx_gateway_api.erl index 19ea97e9e..89b17a6b9 100644 --- a/apps/emqx_gateway/src/emqx_gateway_api.erl +++ b/apps/emqx_gateway/src/emqx_gateway_api.erl @@ -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)">>}. diff --git a/apps/emqx_gateway/src/emqx_gateway_app.erl b/apps/emqx_gateway/src/emqx_gateway_app.erl index adc546767..8af5a1026 100644 --- a/apps/emqx_gateway/src/emqx_gateway_app.erl +++ b/apps/emqx_gateway/src/emqx_gateway_app.erl @@ -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 diff --git a/apps/emqx_gateway/src/emqx_gateway_schema.erl b/apps/emqx_gateway/src/emqx_gateway_schema.erl index d161c499c..f313058c1 100644 --- a/apps/emqx_gateway/src/emqx_gateway_schema.erl +++ b/apps/emqx_gateway/src/emqx_gateway_schema.erl @@ -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)}