解决uinx socket的通讯问题
This commit is contained in:
parent
2f241bb51e
commit
609386cc44
@ -6,19 +6,19 @@
|
|||||||
test() ->
|
test() ->
|
||||||
try
|
try
|
||||||
Path = "/api/detail?user_id=a23147f0-2faa-40fc-b1ae-fa387bc7349f&id=16322",
|
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 ->
|
catch _:Reason:Stack ->
|
||||||
lager:debug("reason: ~p", [Reason]),
|
lager:debug("reason: ~p", [Reason]),
|
||||||
lager:debug("stack: ~p", [Stack])
|
lager:debug("stack: ~p", [Stack])
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%% 通过 Unix Socket 调用 Docker API
|
%% 通过 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()}.
|
{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",
|
SocketPath = "/var/run/docker.sock",
|
||||||
%% 使用 gun:open/2 + {local, Path} 方式
|
%% 使用 gun:open/2 + {local, Path} 方式
|
||||||
case gun:open("127.0.0.1", 80) of
|
case gun:open_unix(SocketPath, #{}) of
|
||||||
{ok, ConnPid} ->
|
{ok, ConnPid} ->
|
||||||
{ok, _} = Res = gun:await_up(ConnPid),
|
{ok, _} = Res = gun:await_up(ConnPid),
|
||||||
lager:debug("gun up: ~p, res: ~p", [ConnPid, Res]),
|
lager:debug("gun up: ~p, res: ~p", [ConnPid, Res]),
|
||||||
@ -28,7 +28,7 @@ request(Method, Path, Body, Headers) ->
|
|||||||
B when is_binary(B) -> B
|
B when is_binary(B) -> B
|
||||||
end,
|
end,
|
||||||
%% 发送 HTTP 请求
|
%% 发送 HTTP 请求
|
||||||
StreamRef = gun:request(ConnPid, <<"GET">>, Path, Headers, BodyBin),
|
StreamRef = gun:request(ConnPid, Method, Path, Headers, BodyBin),
|
||||||
lager:debug("stream is: ~p", [StreamRef]),
|
lager:debug("stream is: ~p", [StreamRef]),
|
||||||
receive_response(ConnPid, StreamRef);
|
receive_response(ConnPid, StreamRef);
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
@ -44,7 +44,6 @@ receive_response(ConnPid, StreamRef) ->
|
|||||||
after 5000 ->
|
after 5000 ->
|
||||||
{error, timeout11}
|
{error, timeout11}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
receive_body(ConnPid, StreamRef, Status, Headers, Acc) ->
|
receive_body(ConnPid, StreamRef, Status, Headers, Acc) ->
|
||||||
receive
|
receive
|
||||||
{gun_data, ConnPid, StreamRef, fin, Data} ->
|
{gun_data, ConnPid, StreamRef, fin, Data} ->
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user