fix handler
This commit is contained in:
parent
ee9bfc3bc4
commit
fc53ed34b9
@ -35,7 +35,8 @@ start_http_server() ->
|
|||||||
|
|
||||||
Dispatcher = cowboy_router:compile([
|
Dispatcher = cowboy_router:compile([
|
||||||
{'_', [
|
{'_', [
|
||||||
{"/host/[...]", http_protocol, [http_host_handler]}
|
{"/host/[...]", http_protocol, [http_host_handler]},
|
||||||
|
{"/router/[...]", http_protocol, [http_router_handler]}
|
||||||
]}
|
]}
|
||||||
]),
|
]),
|
||||||
TransOpts = [
|
TransOpts = [
|
||||||
|
|||||||
@ -56,6 +56,14 @@ init_database() ->
|
|||||||
{type, ordered_set}
|
{type, ordered_set}
|
||||||
]),
|
]),
|
||||||
|
|
||||||
|
%% 转发规则表
|
||||||
|
mnesia:create_table(router, [
|
||||||
|
{attributes, record_info(fields, router)},
|
||||||
|
{record_name, router},
|
||||||
|
{disc_copies, [node()]},
|
||||||
|
{type, ordered_set}
|
||||||
|
]),
|
||||||
|
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
%% 加入集群
|
%% 加入集群
|
||||||
|
|||||||
@ -11,7 +11,7 @@
|
|||||||
-include("iot.hrl").
|
-include("iot.hrl").
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([insert_hosts/0, insert_services/1, insert_terminals/1, insert_routers/1]).
|
-export([insert_hosts/0, insert_services/1, insert_terminals/1, insert_routers/0]).
|
||||||
-export([rsa_encode/1]).
|
-export([rsa_encode/1]).
|
||||||
|
|
||||||
insert_hosts() ->
|
insert_hosts() ->
|
||||||
@ -53,8 +53,6 @@ insert_services(HostId) ->
|
|||||||
|
|
||||||
insert_terminals(HostId) ->
|
insert_terminals(HostId) ->
|
||||||
lists:foreach(fun(Id0) ->
|
lists:foreach(fun(Id0) ->
|
||||||
Q0 = queue:new(),
|
|
||||||
Q = queue:in({1234, 21}, Q0),
|
|
||||||
Terminal = #terminal{
|
Terminal = #terminal{
|
||||||
terminal_id = integer_to_binary(Id0),
|
terminal_id = integer_to_binary(Id0),
|
||||||
host_id = HostId,
|
host_id = HostId,
|
||||||
@ -71,14 +69,14 @@ insert_terminals(HostId) ->
|
|||||||
terminal_model:add_terminal(Terminal)
|
terminal_model:add_terminal(Terminal)
|
||||||
end, lists:seq(1, 100)).
|
end, lists:seq(1, 100)).
|
||||||
|
|
||||||
insert_routers(HostId) ->
|
insert_routers() ->
|
||||||
lists:foreach(fun(Id0) ->
|
lists:foreach(fun(Id0) ->
|
||||||
R = #router{
|
R = #router{
|
||||||
router_id = Id0,
|
router_id = Id0,
|
||||||
name = <<"计费电表"/utf8>>,
|
name = <<"计费电表"/utf8>>,
|
||||||
rule = <<"测试规则"/utf8>>,
|
rule = <<"测试规则"/utf8>>,
|
||||||
endpoint = #http_endpoint{url = <<"http://127.0.0.1:8080/data">>},
|
endpoint = #http_endpoint{url = <<"http://127.0.0.1:8080/data">>},
|
||||||
status = 0
|
status = 1
|
||||||
},
|
},
|
||||||
router_model:add_router(R)
|
router_model:add_router(R)
|
||||||
end, lists:seq(1, 100)).
|
end, lists:seq(1, 100)).
|
||||||
|
|||||||
@ -12,7 +12,7 @@
|
|||||||
-include_lib("stdlib/include/qlc.hrl").
|
-include_lib("stdlib/include/qlc.hrl").
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([get_router/1, get_routers/3, add_router/1, change_status/2, delete/1, table_size/0]).
|
-export([get_router/1, get_routers/3, add_router/1, change_status/2, delete/1, table_size/0, get_all_routers/0, get_all_valid_routers/0]).
|
||||||
-export([to_map/1]).
|
-export([to_map/1]).
|
||||||
|
|
||||||
get_router(RouterId) when is_integer(RouterId) ->
|
get_router(RouterId) when is_integer(RouterId) ->
|
||||||
@ -23,6 +23,34 @@ get_router(RouterId) when is_integer(RouterId) ->
|
|||||||
undefined
|
undefined
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
%% 获取app信息
|
||||||
|
-spec get_all_routers() -> {ok, Items :: list()} | {error, Reason :: any()}.
|
||||||
|
get_all_routers() ->
|
||||||
|
Fun = fun() ->
|
||||||
|
Q = qlc:q([E || E <- mnesia:table(router)]),
|
||||||
|
qlc:e(Q)
|
||||||
|
end,
|
||||||
|
case mnesia:transaction(Fun) of
|
||||||
|
{atomic, Routers} ->
|
||||||
|
{ok, Routers};
|
||||||
|
{aborted, _} ->
|
||||||
|
error
|
||||||
|
end.
|
||||||
|
|
||||||
|
%% 获取app信息
|
||||||
|
-spec get_all_valid_routers() -> {ok, Items :: list()} | {error, Reason :: any()}.
|
||||||
|
get_all_valid_routers() ->
|
||||||
|
Fun = fun() ->
|
||||||
|
Q = qlc:q([E || E <- mnesia:table(router), E#router.status =:= 1]),
|
||||||
|
qlc:e(Q)
|
||||||
|
end,
|
||||||
|
case mnesia:transaction(Fun) of
|
||||||
|
{atomic, Routers} ->
|
||||||
|
{ok, Routers};
|
||||||
|
{aborted, _} ->
|
||||||
|
error
|
||||||
|
end.
|
||||||
|
|
||||||
%% 获取app信息
|
%% 获取app信息
|
||||||
-spec get_routers(Filter :: any(), Start :: integer(), Limit :: integer()) ->
|
-spec get_routers(Filter :: any(), Start :: integer(), Limit :: integer()) ->
|
||||||
{ok, Items :: list(), TotalNum :: integer()} |
|
{ok, Items :: list(), TotalNum :: integer()} |
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user