chore(gw): integrate config-handler
This commit is contained in:
parent
86e28d5abb
commit
bce130d9f9
|
@ -74,13 +74,13 @@ start(Name) ->
|
||||||
stop(Name) ->
|
stop(Name) ->
|
||||||
emqx_gateway_sup:stop_gateway_insta(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
|
-> ok
|
||||||
| {error, any()}.
|
| {error, any()}.
|
||||||
update_rawconf(_Name, _RawConf) ->
|
update_rawconf(RawName, RawConfDiff) ->
|
||||||
%% TODO:
|
emqx:update_config([gateway], {RawName, RawConfDiff}).
|
||||||
ok.
|
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Internal funcs
|
%% Internal funcs
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
|
@ -357,13 +357,23 @@ gateway_insta(get, Request) ->
|
||||||
Name = binary_to_existing_atom(cowboy_req:binding(name, Request)),
|
Name = binary_to_existing_atom(cowboy_req:binding(name, Request)),
|
||||||
case emqx_gateway:lookup(Name) of
|
case emqx_gateway:lookup(Name) of
|
||||||
#{config := Config} ->
|
#{config := Config} ->
|
||||||
%% TODO: ??? RawConf or Config ??
|
%% TODO: ??? RawConf or Config or RunningState ???
|
||||||
{200, Config};
|
{200, Config};
|
||||||
undefined ->
|
undefined ->
|
||||||
{404, <<"Not Found">>}
|
{404, <<"Not Found">>}
|
||||||
end;
|
end;
|
||||||
gateway_insta(post, _Request) ->
|
gateway_insta(post, Request) ->
|
||||||
{200, ok}.
|
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) ->
|
gateway_insta_stats(get, _Req) ->
|
||||||
{401, <<"Implement it later (maybe 5.1)">>}.
|
{401, <<"Implement it later (maybe 5.1)">>}.
|
||||||
|
|
|
@ -17,23 +17,52 @@
|
||||||
-module(emqx_gateway_app).
|
-module(emqx_gateway_app).
|
||||||
|
|
||||||
-behaviour(application).
|
-behaviour(application).
|
||||||
|
-behaviour(emqx_config_handler).
|
||||||
|
|
||||||
-include_lib("emqx/include/logger.hrl").
|
-include_lib("emqx/include/logger.hrl").
|
||||||
|
|
||||||
|
|
||||||
-export([start/2, stop/1]).
|
-export([start/2, stop/1]).
|
||||||
|
|
||||||
|
-export([ pre_config_update/2
|
||||||
|
, post_config_update/3
|
||||||
|
]).
|
||||||
|
|
||||||
start(_StartType, _StartArgs) ->
|
start(_StartType, _StartArgs) ->
|
||||||
{ok, Sup} = emqx_gateway_sup:start_link(),
|
{ok, Sup} = emqx_gateway_sup:start_link(),
|
||||||
emqx_gateway_cli:load(),
|
emqx_gateway_cli:load(),
|
||||||
load_default_gateway_applications(),
|
load_default_gateway_applications(),
|
||||||
load_gateway_by_default(),
|
load_gateway_by_default(),
|
||||||
|
emqx_config_handler:add_handler([gateway], ?MODULE),
|
||||||
{ok, Sup}.
|
{ok, Sup}.
|
||||||
|
|
||||||
stop(_State) ->
|
stop(_State) ->
|
||||||
emqx_gateway_cli:unload(),
|
emqx_gateway_cli:unload(),
|
||||||
|
%% XXX: No api now
|
||||||
|
%emqx_config_handler:remove_handler([gateway], ?MODULE),
|
||||||
ok.
|
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
|
%% Internal funcs
|
||||||
|
|
||||||
|
|
|
@ -85,7 +85,7 @@ fields(mqttsn_predefined) ->
|
||||||
];
|
];
|
||||||
|
|
||||||
fields(coap_structs) ->
|
fields(coap_structs) ->
|
||||||
[ {heartbeat, t(duration(), undefined, "30s")}
|
[ {heartbeat, t(duration(), undefined, <<"30s">>)}
|
||||||
, {notify_type, t(union([non, con, qos]), undefined, qos)}
|
, {notify_type, t(union([non, con, qos]), undefined, qos)}
|
||||||
, {subscribe_qos, t(union([qos0, qos1, qos2, coap]), undefined, coap)}
|
, {subscribe_qos, t(union([qos0, qos1, qos2, coap]), undefined, coap)}
|
||||||
, {publish_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, t(boolean())}
|
||||||
, {proxy_protocol_timeout, t(duration())}
|
, {proxy_protocol_timeout, t(duration())}
|
||||||
, {backlog, t(integer(), undefined, 1024)}
|
, {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)}
|
, {send_timeout_close, t(boolean(), undefined, true)}
|
||||||
, {recbuf, t(bytesize())}
|
, {recbuf, t(bytesize())}
|
||||||
, {sndbuf, t(bytesize())}
|
, {sndbuf, t(bytesize())}
|
||||||
, {buffer, t(bytesize())}
|
, {buffer, t(bytesize())}
|
||||||
, {high_watermark, t(bytesize(), undefined, "1MB")}
|
, {high_watermark, t(bytesize(), undefined, <<"1MB">>)}
|
||||||
, {tune_buffer, t(boolean())}
|
, {tune_buffer, t(boolean())}
|
||||||
, {nodelay, t(boolean())}
|
, {nodelay, t(boolean())}
|
||||||
, {reuseaddr, t(boolean())}
|
, {reuseaddr, t(boolean())}
|
||||||
|
@ -189,7 +189,7 @@ fields(ssl_listener_settings) ->
|
||||||
[
|
[
|
||||||
%% some special confs for ssl listener
|
%% some special confs for ssl listener
|
||||||
] ++
|
] ++
|
||||||
ssl(undefined, #{handshake_timeout => "15s"
|
ssl(undefined, #{handshake_timeout => <<"15s">>
|
||||||
, depth => 10
|
, depth => 10
|
||||||
, reuse_sessions => true}) ++ fields(listener_settings);
|
, reuse_sessions => true}) ++ fields(listener_settings);
|
||||||
|
|
||||||
|
@ -202,7 +202,7 @@ fields(dtls_listener_settings) ->
|
||||||
[
|
[
|
||||||
%% some special confs for dtls listener
|
%% some special confs for dtls listener
|
||||||
] ++
|
] ++
|
||||||
ssl(undefined, #{handshake_timeout => "15s"
|
ssl(undefined, #{handshake_timeout => <<"15s">>
|
||||||
, depth => 10
|
, depth => 10
|
||||||
, reuse_sessions => true}) ++ fields(listener_settings);
|
, reuse_sessions => true}) ++ fields(listener_settings);
|
||||||
|
|
||||||
|
@ -241,7 +241,7 @@ authentication() ->
|
||||||
gateway_common_options() ->
|
gateway_common_options() ->
|
||||||
[ {enable, t(boolean(), undefined, true)}
|
[ {enable, t(boolean(), undefined, true)}
|
||||||
, {enable_stats, 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())}
|
, {mountpoint, t(binary())}
|
||||||
, {clientinfo_override, t(ref(clientinfo_override))}
|
, {clientinfo_override, t(ref(clientinfo_override))}
|
||||||
, {authentication, t(authentication(), undefined, undefined)}
|
, {authentication, t(authentication(), undefined, undefined)}
|
||||||
|
|
Loading…
Reference in New Issue