chore(gw): add http-api for gateway summary lists
This commit is contained in:
parent
836ca38c2b
commit
e239fb07cd
|
@ -115,7 +115,7 @@ do_start_listener(dtls, Name, ListenOn, SocketOpts, MFA) ->
|
|||
esockd:open_dtls(Name, ListenOn, SocketOpts, MFA).
|
||||
|
||||
name(GwName, LisName, Type) ->
|
||||
list_to_atom(lists:concat([GwName, ":", LisName, ":", Type])).
|
||||
list_to_atom(lists:concat([GwName, ":", Type, ":", LisName])).
|
||||
|
||||
stop_listener(GwName, {Type, LisName, ListenOn, SocketOpts, Cfg}) ->
|
||||
StopRet = stop_listener(GwName, Type, LisName, ListenOn, SocketOpts, Cfg),
|
||||
|
|
|
@ -374,8 +374,13 @@ schema_for_gateway_stats() ->
|
|||
%%--------------------------------------------------------------------
|
||||
%% http handlers
|
||||
|
||||
gateway(get, _Request) ->
|
||||
{200, ok}.
|
||||
gateway(get, Request) ->
|
||||
Params = cowboy_req:parse_qs(Request),
|
||||
Status = case proplists:get_value(<<"status">>, Params) of
|
||||
undefined -> all;
|
||||
S0 -> binary_to_existing_atom(S0, utf8)
|
||||
end,
|
||||
{200, emqx_gateway_intr:gateways(Status)}.
|
||||
|
||||
gateway_insta(delete, _Request) ->
|
||||
{200, ok};
|
||||
|
|
|
@ -0,0 +1,72 @@
|
|||
%%--------------------------------------------------------------------
|
||||
%% Copyright (c) 2021 EMQ Technologies Co., Ltd. All Rights Reserved.
|
||||
%%
|
||||
%% Licensed under the Apache License, Version 2.0 (the "License");
|
||||
%% you may not use this file except in compliance with the License.
|
||||
%% You may obtain a copy of the License at
|
||||
%%
|
||||
%% http://www.apache.org/licenses/LICENSE-2.0
|
||||
%%
|
||||
%% Unless required by applicable law or agreed to in writing, software
|
||||
%% distributed under the License is distributed on an "AS IS" BASIS,
|
||||
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
%% See the License for the specific language governing permissions and
|
||||
%% limitations under the License.
|
||||
%%--------------------------------------------------------------------
|
||||
|
||||
%% @doc Gateway Interface Module for HTTP-APIs
|
||||
-module(emqx_gateway_intr).
|
||||
|
||||
-export([ gateways/1
|
||||
]).
|
||||
|
||||
-type gateway_summary() ::
|
||||
#{ name := binary()
|
||||
, status := running | stopped | unloaded
|
||||
, started_at => binary()
|
||||
, max_connection => integer()
|
||||
, current_connect => integer()
|
||||
, listeners => []
|
||||
}.
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% APIs
|
||||
%%--------------------------------------------------------------------
|
||||
|
||||
-spec gateways(Status :: all | running | stopped | unloaded)
|
||||
-> [gateway_summary()].
|
||||
gateways(Status) ->
|
||||
Gateways = lists:map(fun({GwName, _}) ->
|
||||
case emqx_gateway:lookup(GwName) of
|
||||
undefined -> #{name => GwName, status => unloaded};
|
||||
GwInfo = #{rawconf := RawConf} ->
|
||||
GwInfo1 = maps:with(
|
||||
[name, started_at, craeted_at, status], GwInfo),
|
||||
GwInfo1#{listeners => get_listeners_status(GwName, RawConf)}
|
||||
|
||||
end
|
||||
end, emqx_gateway_registry:list()),
|
||||
case Status of
|
||||
all -> Gateways;
|
||||
_ ->
|
||||
[Gw || Gw = #{status := S} <- Gateways, S == Status]
|
||||
end.
|
||||
|
||||
%% @private
|
||||
get_listeners_status(GwName, RawConf) ->
|
||||
Listeners = emqx_gateway_utils:normalize_rawconf(RawConf),
|
||||
lists:map(fun({Type, LisName, ListenOn, _, _}) ->
|
||||
Name0 = listener_name(GwName, Type, LisName),
|
||||
Name = {Name0, ListenOn},
|
||||
case catch esockd:listener(Name) of
|
||||
_Pid when is_pid(_Pid) ->
|
||||
#{Name0 => <<"activing">>};
|
||||
_ ->
|
||||
#{Name0 => <<"inactived">>}
|
||||
|
||||
end
|
||||
end, Listeners).
|
||||
|
||||
%% @private
|
||||
listener_name(GwName, Type, LisName) ->
|
||||
list_to_atom(lists:concat([GwName, ":", Type, ":", LisName])).
|
|
@ -173,7 +173,7 @@ do_start_listener(dtls, Name, ListenOn, Opts, MFA) ->
|
|||
esockd:open_dtls(Name, ListenOn, Opts, MFA).
|
||||
|
||||
name(GwName, LisName, Type) ->
|
||||
list_to_atom(lists:concat([GwName, ":", LisName, ":", Type])).
|
||||
list_to_atom(lists:concat([GwName, ":", Type, ":", LisName])).
|
||||
|
||||
merge_default_by_type(Type, Options) when Type =:= tcp;
|
||||
Type =:= ssl ->
|
||||
|
|
|
@ -129,7 +129,7 @@ start_listener(GwName, Ctx, Type, LisName, ListenOn, SocketOpts, Cfg) ->
|
|||
end.
|
||||
|
||||
name(GwName, LisName, Type) ->
|
||||
list_to_atom(lists:concat([GwName, ":", LisName, ":", Type])).
|
||||
list_to_atom(lists:concat([GwName, ":", Type, ":", LisName])).
|
||||
|
||||
merge_default(Options) ->
|
||||
Default = emqx_gateway_utils:default_udp_options(),
|
||||
|
|
|
@ -128,7 +128,7 @@ start_listener(GwName, Ctx, Type, LisName, ListenOn, SocketOpts, Cfg) ->
|
|||
{emqx_gateway_conn, start_link, [NCfg]}).
|
||||
|
||||
name(GwName, LisName, Type) ->
|
||||
list_to_atom(lists:concat([GwName, ":", LisName, ":", Type])).
|
||||
list_to_atom(lists:concat([GwName, ":", Type, ":", LisName])).
|
||||
|
||||
merge_default(Options) ->
|
||||
Default = emqx_gateway_utils:default_udp_options(),
|
||||
|
|
|
@ -113,7 +113,7 @@ start_listener(GwName, Ctx, Type, LisName, ListenOn, SocketOpts, Cfg) ->
|
|||
{emqx_gateway_conn, start_link, [NCfg]}).
|
||||
|
||||
name(GwName, LisName, Type) ->
|
||||
list_to_atom(lists:concat([GwName, ":", LisName, ":", Type])).
|
||||
list_to_atom(lists:concat([GwName, ":", Type, ":", LisName])).
|
||||
|
||||
merge_default(Options) ->
|
||||
Default = emqx_gateway_utils:default_tcp_options(),
|
||||
|
|
Loading…
Reference in New Issue