-module(olive@proxy).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([start_http/2]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" The `proxy` module starts the proxy server that redirects to the \"main\" server\n"
" It also injects a websocket connection and handles it, via the `client_registry`\n"
" module, to add / remove them, and send them a websocket message to trigger the\n"
" actual reload of the browser.\n"
).
-file("src/olive/proxy.gleam", 37).
-spec handle_websocket(
gleam@http@request:request(mist@internal@http:connection()),
olive@client_registry:client_registry(),
olive@config:config()
) -> gleam@http@response:response(mist:response_data()).
handle_websocket(Req, Clients, Config) ->
mist:websocket(Req, fun(Client, Conn, Message) -> case Message of
{custom, reload} ->
case mist:send_text_frame(Conn, <<"reload"/utf8>>) of
{ok, _} ->
olive@logging:notice(
erlang:element(2, Config),
<<"Successfully sent reload message to client"/utf8>>
),
gleam@otp@actor:continue(Client);
{error, _} ->
olive@logging:error(
erlang:element(2, Config),
<<"Could not send message to client"/utf8>>
),
{stop, normal}
end;
closed ->
olive@logging:notice(
erlang:element(2, Config),
<<"Client has disconnected"/utf8>>
),
olive@client_registry:remove(Clients, Client),
{stop, normal};
shutdown ->
olive@logging:notice(
erlang:element(2, Config),
<<"Client has disconnected"/utf8>>
),
olive@client_registry:remove(Clients, Client),
{stop, normal};
{text, _} ->
gleam@otp@actor:continue(Client);
{binary, _} ->
gleam@otp@actor:continue(Client)
end end, fun(_) ->
Client@1 = gleam@erlang@process:new_subject(),
olive@client_registry:add(Clients, Client@1),
Selector = begin
_pipe = gleam_erlang_ffi:new_selector(),
gleam@erlang@process:selecting(
_pipe,
Client@1,
fun gleam@function:identity/1
)
end,
{Client@1, {some, Selector}}
end, fun(Client@2) ->
olive@client_registry:remove(Clients, Client@2)
end).
-file("src/olive/proxy.gleam", 106).
-spec inject(binary()) -> binary().
inject(Html) ->
Script = <<""/utf8>>,
_pipe = Html,
gleam@string:replace(
_pipe,
<<""/utf8>>,
<