%%%------------------------------------------------------------------- %%% @author anlicheng %%% @copyright (C) 2024, %%% @doc %%% %%% @end %%% Created : 09. 4月 2024 14:28 %%%------------------------------------------------------------------- -module(network_handler). -author("anlicheng"). %% API -export([handle_request/4]). handle_request("POST", "/network/create", _, #{<<"id">> := NetworkId}) when NetworkId > 0 -> case sdlan_network_sup:ensured_network_started(NetworkId) of {ok, Pid} when is_pid(Pid) -> {ok, 200, sdlan_util:json_data(<<"success">>)}; {error, Reason} -> logger:debug("[network_handler] create network: ~p, get error: ~p", [NetworkId, Reason]), {ok, 200, sdlan_util:json_error(-1, <<"error">>)} end; handle_request("POST", "/network/delete", _, #{<<"id">> := NetworkId}) when NetworkId > 0 -> case sdlan_network:get_pid(NetworkId) of undefined -> {ok, 200, sdlan_util:json_data(<<"success">>)}; NetworkPid when is_pid(NetworkPid) -> case sdlan_network_sup:delete_network(NetworkId) of ok -> {ok, 200, sdlan_util:json_data(<<"success">>)}; {error, Reason} -> logger:debug("[network_handler] delete network: ~p, get error: ~p", [NetworkId, Reason]), {ok, 200, sdlan_util:json_error(-1, <<"error">>)} end end; handle_request("POST", "/network/exit_node_control", _, #{<<"id">> := NetworkId}) when NetworkId > 0 -> case sdlan_network:get_pid(NetworkId) of undefined -> {ok, 200, sdlan_util:json_data(<<"network not found">>)}; NetworkPid when is_pid(NetworkPid) -> case sdlan_network_sup:delete_network(NetworkId) of ok -> {ok, 200, sdlan_util:json_data(<<"success">>)}; {error, Reason} -> logger:debug("[network_handler] delete network: ~p, get error: ~p", [NetworkId, Reason]), {ok, 200, sdlan_util:json_error(-1, <<"error">>)} end end; handle_request(_, Path, _, _) -> Path1 = list_to_binary(Path), {ok, 200, sdlan_util:json_error(-1, <<"url: ", Path1/binary, " not found">>)}.