add gun support

This commit is contained in:
anlicheng 2025-09-23 11:07:18 +08:00
parent a7d565b38f
commit 2f241bb51e
4 changed files with 61 additions and 0 deletions

View File

@ -66,6 +66,7 @@ create_container(ContainerName, ContainerDir, Config) when is_binary(ContainerNa
PortSettings = [stream, exit_status, stderr_to_stdout, use_stdio, binary], PortSettings = [stream, exit_status, stderr_to_stdout, use_stdio, binary],
ExecCmd = "docker create --name " ++ binary_to_list(ContainerName) ++ " " ++ binary_to_list(CreateArgs), ExecCmd = "docker create --name " ++ binary_to_list(ContainerName) ++ " " ++ binary_to_list(CreateArgs),
lager:debug("create_container cmd : ~p", [ExecCmd]), lager:debug("create_container cmd : ~p", [ExecCmd]),
case catch erlang:open_port({spawn, ExecCmd}, PortSettings) of case catch erlang:open_port({spawn, ExecCmd}, PortSettings) of
Port when is_port(Port) -> Port when is_port(Port) ->
case gather_output(Port) of case gather_output(Port) of

View File

@ -0,0 +1,57 @@
%%% docker_http.erl
-module(docker_http).
-export([request/4]).
-export([test/0]).
test() ->
try
Path = "/api/detail?user_id=a23147f0-2faa-40fc-b1ae-fa387bc7349f&id=16322",
catch request(get, "/manifest.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()) ->
{ok, StatusCode :: integer(), RespBody :: binary()} | {error, any()}.
request(Method, Path, Body, Headers) ->
SocketPath = "/var/run/docker.sock",
%% 使 gun:open/2 + {local, Path}
case gun:open("127.0.0.1", 80) of
{ok, ConnPid} ->
{ok, _} = Res = gun:await_up(ConnPid),
lager:debug("gun up: ~p, res: ~p", [ConnPid, Res]),
%% Body undefined <<>>
BodyBin = case Body of
undefined -> <<>>;
B when is_binary(B) -> B
end,
%% HTTP
StreamRef = gun:request(ConnPid, <<"GET">>, Path, Headers, BodyBin),
lager:debug("stream is: ~p", [StreamRef]),
receive_response(ConnPid, StreamRef);
{error, Reason} ->
{error, Reason}
end.
receive_response(ConnPid, StreamRef) ->
receive
{gun_response, ConnPid, StreamRef, nofin, Status, Headers} ->
receive_body(ConnPid, StreamRef, Status, Headers, <<>>);
{gun_down, ConnPid, _, Reason, _} ->
{error, {http_closed, Reason}}
after 5000 ->
{error, timeout11}
end.
receive_body(ConnPid, StreamRef, Status, Headers, Acc) ->
receive
{gun_data, ConnPid, StreamRef, fin, Data} ->
{ok, Status, Headers, <<Acc/binary, Data/binary>>};
{gun_data, ConnPid, StreamRef, nofin, Data} ->
NewAcc = <<Acc/binary, Data/binary>>,
receive_body(ConnPid, StreamRef, Status, Headers, NewAcc)
after 10000 ->
{error, timeout22}
end.

View File

@ -12,6 +12,8 @@
cowboy, cowboy,
ranch, ranch,
crypto, crypto,
gun,
cowlib,
inets, inets,
ssl, ssl,
public_key, public_key,

View File

@ -3,6 +3,7 @@
{sync, ".*", {git, "https://github.com/rustyio/sync.git", {branch, "master"}}}, {sync, ".*", {git, "https://github.com/rustyio/sync.git", {branch, "master"}}},
{jiffy, ".*", {git, "https://github.com/davisp/jiffy.git", {tag, "1.1.2"}}}, {jiffy, ".*", {git, "https://github.com/davisp/jiffy.git", {tag, "1.1.2"}}},
{cowboy, ".*", {git, "https://github.com/ninenines/cowboy.git", {tag, "2.10.0"}}}, {cowboy, ".*", {git, "https://github.com/ninenines/cowboy.git", {tag, "2.10.0"}}},
{gun, ".*", {git, "https://github.com/ninenines/gun.git", {tag, "2.2.0"}}},
{parse_trans, ".*", {git, "https://github.com/uwiger/parse_trans", {tag, "3.0.0"}}}, {parse_trans, ".*", {git, "https://github.com/uwiger/parse_trans", {tag, "3.0.0"}}},
{lager, ".*", {git,"https://github.com/erlang-lager/lager.git", {tag, "3.9.2"}}} {lager, ".*", {git,"https://github.com/erlang-lager/lager.git", {tag, "3.9.2"}}}
]}. ]}.