Commit b0f55c6b authored by href's avatar href

...

parent 312bbe42
......@@ -35,7 +35,7 @@ start(_StartType, _StartArgs) ->
{ConnectDomain, [
{"/", efl_connect_h, []},
{"/resp/:id", efl_resp_h, []},
{"/resp/:id[/:part]", efl_resp_h, []},
{"/resp/:id[/:part]", efl_resp_h, []}
]},
{":subdomain." ++ RootDomain, [{'_', efl_proxy_h, []}]}
]),
......
......@@ -41,8 +41,9 @@ loop(Req0, State) ->
io:format("efl_connect_handler poll ~p~n", [Req0]),
receive
{reqid, Id, ForeignReq} ->
Data = term_to_binary({Id, ForeignReq}),
ForeignReq0 = maps:put(pid, undefined, ForeignReq),
Data = term_to_binary({Id, ForeignReq0}, [compressed]),
ok = cowboy_req:stream_body(Data, nofin, Req0),
loop(Req0, State);
loop(Req0, State)
end.
......@@ -17,15 +17,38 @@
-export([init/2]).
init(Req0, State) ->
case global:whereis_name(<<"uplink.randomlabs.dev">>) of
undefined ->
unavailable(Req0, State);
Pid when is_pid(Pid) ->
monitor(process, Pid),
wait_response(Req0, Pid, State)
end.
wait_response(Req0, Pid, State) ->
io:format("wait for resp ~p ~n", [self()]),
PidH = erlang:phash2(self()),
RandP = rand:uniform(9999999),
Id = list_to_binary(integer_to_list(PidH + RandP)),
yes = global:register_name({req,Id}, self()),
global:send(<<"uplink.randomlabs.dev">>, {reqid, Id, Req0}),
Pid ! {reqid, Id, Req0},
receive
{'DOWN', _Ref, process, _Pid2, _Reason} ->
unavailable(Req0, State);
{response, Resp} ->
{Status, Headers0, Body} = binary_to_term(Resp),
{Status, Headers0, Body} = binary_to_term(Resp, [safe]),
Headers = maps:put(<<"x-efl-request-id">>, Id, Headers0),
cowboy_req:reply(Status, Headers, Body, Req0)
{ok, cowboy_req:reply(Status, Headers, Body, Req0), State}
after
timer:minutes(2) -> timeout(Req0, State)
end.
unavailable(Req0, State) ->
reply(Req0, State, 503, "Remote unavailable/disconnected.").
timeout(Req0, State) ->
reply(Req0, State, 504, "Remote timeout").
reply(Req0, State, Code, Text) ->
{ok, cowboy_req:reply(Code, #{<<"content-type">> => <<"text/plain">>}, Text, Req0), State}.
......@@ -6,6 +6,7 @@
{applications,
[kernel,
stdlib,
sasl,
cowboy
]},
{env,[]},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment