解决uinx socket的通讯问题

This commit is contained in:
anlicheng 2025-09-23 11:13:45 +08:00
parent 2f241bb51e
commit 609386cc44

View File

@ -6,19 +6,19 @@
test() ->
try
Path = "/api/detail?user_id=a23147f0-2faa-40fc-b1ae-fa387bc7349f&id=16322",
catch request(get, "/manifest.json", undefined, [])
catch request("GET", "/containers/json", undefined, [])
catch _:Reason:Stack ->
lager:debug("reason: ~p", [Reason]),
lager:debug("stack: ~p", [Stack])
end.
%% Unix Socket Docker API
-spec request(Method :: atom(), Path :: string(), Body :: binary() | undefined, Headers :: list()) ->
-spec request(Method :: iolist(), Path :: string(), Body :: binary() | undefined, Headers :: list()) ->
{ok, StatusCode :: integer(), RespBody :: binary()} | {error, any()}.
request(Method, Path, Body, Headers) ->
request(Method, Path, Body, Headers) when is_list(Method); is_binary(Method), is_list(Path), is_binary(Body), is_list(Headers) ->
SocketPath = "/var/run/docker.sock",
%% 使 gun:open/2 + {local, Path}
case gun:open("127.0.0.1", 80) of
case gun:open_unix(SocketPath, #{}) of
{ok, ConnPid} ->
{ok, _} = Res = gun:await_up(ConnPid),
lager:debug("gun up: ~p, res: ~p", [ConnPid, Res]),
@ -28,7 +28,7 @@ request(Method, Path, Body, Headers) ->
B when is_binary(B) -> B
end,
%% HTTP
StreamRef = gun:request(ConnPid, <<"GET">>, Path, Headers, BodyBin),
StreamRef = gun:request(ConnPid, Method, Path, Headers, BodyBin),
lager:debug("stream is: ~p", [StreamRef]),
receive_response(ConnPid, StreamRef);
{error, Reason} ->
@ -44,7 +44,6 @@ receive_response(ConnPid, StreamRef) ->
after 5000 ->
{error, timeout11}
end.
receive_body(ConnPid, StreamRef, Status, Headers, Acc) ->
receive
{gun_data, ConnPid, StreamRef, fin, Data} ->