From 61d450fd89ae637347d2ef0442494f83d93e139b Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Thu, 18 Sep 2025 15:07:47 +0800 Subject: [PATCH] fix --- .../src/http_handlers/container_handler.erl | 76 +++++++------------ 1 file changed, 27 insertions(+), 49 deletions(-) diff --git a/apps/iot/src/http_handlers/container_handler.erl b/apps/iot/src/http_handlers/container_handler.erl index 1eb0e1f..081793f 100644 --- a/apps/iot/src/http_handlers/container_handler.erl +++ b/apps/iot/src/http_handlers/container_handler.erl @@ -15,8 +15,8 @@ %% 下发config.json, 微服务接受后,保存服务配置 handle_request("POST", "/container/push_config", _, - #{<<"uuid">> := UUID, <<"service_id">> := ServiceId, <<"config_json">> := ConfigJson, <<"timeout">> := Timeout0}) - when is_binary(UUID), is_binary(ServiceId), is_binary(ConfigJson), is_integer(Timeout0) -> + #{<<"uuid">> := UUID, <<"container_name">> := ContainerName, <<"config_json">> := ConfigJson, <<"timeout">> := Timeout0}) + when is_binary(UUID), is_binary(ContainerName), is_binary(ConfigJson), is_integer(Timeout0) -> %% 检查ConfigJson是否是合法的json字符串 true = iot_util:is_json(ConfigJson), @@ -25,7 +25,7 @@ handle_request("POST", "/container/push_config", _, {ok, 200, iot_util:json_error(-1, <<"host not found">>)}; Pid when is_pid(Pid) -> Timeout = Timeout0 * 1000, - case iot_host:async_service_config(Pid, ServiceId, ConfigJson, Timeout) of + case iot_host:config_container(Pid, ContainerName, ConfigJson) of {ok, Ref} -> case iot_host:await_reply(Ref, Timeout) of {ok, Result} -> @@ -39,14 +39,14 @@ handle_request("POST", "/container/push_config", _, end; %% 部署微服务 -handle_request("POST", "/container/deploy", _, #{<<"uuid">> := UUID, <<"task_id">> := TaskId, <<"service_id">> := ServiceId, <<"tar_url">> := TarUrl}) - when is_binary(UUID), is_integer(TaskId), is_binary(ServiceId), is_binary(TarUrl) -> +handle_request("POST", "/container/deploy", _, #{<<"uuid">> := UUID, <<"task_id">> := TaskId, <<"config">> := Config}) + when is_binary(UUID), is_integer(TaskId), is_binary(Config) -> case iot_host:get_pid(UUID) of undefined -> {ok, 200, iot_util:json_error(404, <<"host not found">>)}; Pid when is_pid(Pid) -> - case iot_host:deploy_service(Pid, TaskId, ServiceId, TarUrl) of + case iot_host:deploy_container(Pid, TaskId, Config) of {ok, Ref} -> case iot_host:await_reply(Ref, 5000) of {ok, Result} -> @@ -60,12 +60,12 @@ handle_request("POST", "/container/deploy", _, #{<<"uuid">> := UUID, <<"task_id" end; %% 启动服务 -handle_request("POST", "/container/start", _, #{<<"uuid">> := UUID, <<"service_id">> := ServiceId}) when is_binary(UUID), is_binary(ServiceId) -> +handle_request("POST", "/container/start", _, #{<<"uuid">> := UUID, <<"container_name">> := ContainerName}) when is_binary(UUID), is_binary(ContainerName) -> case iot_host:get_pid(UUID) of undefined -> {ok, 200, iot_util:json_error(404, <<"host not found">>)}; Pid when is_pid(Pid) -> - case iot_host:start_service(Pid, ServiceId) of + case iot_host:start_container(Pid, ContainerName) of {ok, Ref} -> case iot_host:await_reply(Ref, 5000) of {ok, Result} -> @@ -79,12 +79,12 @@ handle_request("POST", "/container/start", _, #{<<"uuid">> := UUID, <<"service_i end; %% 停止服务 -handle_request("POST", "/container/stop", _, #{<<"uuid">> := UUID, <<"service_id">> := ServiceId}) when is_binary(UUID), is_binary(ServiceId) -> +handle_request("POST", "/container/stop", _, #{<<"uuid">> := UUID, <<"container_name">> := ContainerName}) when is_binary(UUID), is_binary(ServiceId) -> case iot_host:get_pid(UUID) of undefined -> {ok, 200, iot_util:json_error(404, <<"host not found">>)}; Pid when is_pid(Pid) -> - case iot_host:stop_service(Pid, ServiceId) of + case iot_host:stop_container(Pid, ContainerName) of {ok, Ref} -> case iot_host:await_reply(Ref, 5000) of {ok, Result} -> @@ -97,45 +97,23 @@ handle_request("POST", "/container/stop", _, #{<<"uuid">> := UUID, <<"service_id end end; -%% 远程调用微服务, 返回值的格式为json -handle_request("POST", "/service/invoke", _, #{<<"uuid">> := UUID, <<"service_id">> := ServiceId, <<"payload">> := Payload, <<"timeout">> := Timeout0}) - when is_binary(UUID), is_binary(ServiceId), is_binary(Payload), is_integer(Timeout0) -> - - case iot_host:get_pid(UUID) of - undefined -> - {ok, 200, iot_util:json_error(404, <<"host not found">>)}; - Pid when is_pid(Pid) -> - Timeout = Timeout0 * 1000, - case iot_host:invoke_service(Pid, ServiceId, Payload, Timeout) of - {ok, Ref} -> - case iot_host:await_reply(Ref, Timeout) of - {ok, Result} -> - {ok, 200, iot_util:json_data(Result)}; - {error, Reason} -> - {ok, 200, iot_util:json_error(400, Reason)} - end; - {error, Reason} when is_binary(Reason) -> - {ok, 200, iot_util:json_error(400, Reason)} - end - end; - -handle_request("POST", "/container/task_log", _, #{<<"uuid">> := UUID, <<"task_id">> := TaskId}) when is_binary(UUID), is_integer(TaskId) -> - case iot_host:get_pid(UUID) of - undefined -> - {ok, 200, iot_util:json_error(404, <<"host not found">>)}; - Pid when is_pid(Pid) -> - case iot_host:task_log(Pid, TaskId) of - {ok, Ref} -> - case iot_host:await_reply(Ref, 5000) of - {ok, Result} -> - {ok, 200, iot_util:json_data(Result)}; - {error, Reason} -> - {ok, 200, iot_util:json_error(400, Reason)} - end; - {error, Reason} when is_binary(Reason) -> - {ok, 200, iot_util:json_error(400, Reason)} - end - end; +%handle_request("POST", "/container/task_log", _, #{<<"uuid">> := UUID, <<"task_id">> := TaskId}) when is_binary(UUID), is_integer(TaskId) -> +% case iot_host:get_pid(UUID) of +% undefined -> +% {ok, 200, iot_util:json_error(404, <<"host not found">>)}; +% Pid when is_pid(Pid) -> +% case iot_host:task_log(Pid, TaskId) of +% {ok, Ref} -> +% case iot_host:await_reply(Ref, 5000) of +% {ok, Result} -> +% {ok, 200, iot_util:json_data(Result)}; +% {error, Reason} -> +% {ok, 200, iot_util:json_error(400, Reason)} +% end; +% {error, Reason} when is_binary(Reason) -> +% {ok, 200, iot_util:json_error(400, Reason)} +% end +% end; handle_request(_, Path, _, _) -> Path1 = list_to_binary(Path),