-module(olive@proxy). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -define(FILEPATH, "src/olive/proxy.gleam"). -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, Message, Conn) -> 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>> ), mist:continue(Client); {error, _} -> olive@logging:error( erlang:element(2, Config), <<"Could not send message to client"/utf8>> ), mist:stop() end; closed -> olive@logging:warning( erlang:element(2, Config), <<"Client has disconnected unexpectedly"/utf8>> ), olive@client_registry:remove(Clients, Client), mist:stop(); shutdown -> olive@logging:warning( erlang:element(2, Config), <<"Client has disconnected unexpectedly"/utf8>> ), olive@client_registry:remove(Clients, Client), mist:stop(); {text, _} -> mist:continue(Client); {binary, _} -> mist: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:select(_pipe, Client@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) -> Priv_dir@1 = case fun gleam_erlang_ffi:priv_directory/1(<<"olive"/utf8>>) of {ok, Priv_dir} -> Priv_dir; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, file => <>, module => <<"olive/proxy"/utf8>>, function => <<"inject"/utf8>>, line => 107, value => _assert_fail, start => 3092, 'end' => 3146, pattern_start => 3103, pattern_end => 3115}) end, File_content@1 = case simplifile:read( <> ) of {ok, File_content} -> File_content; _assert_fail@1 -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, file => <>, module => <<"olive/proxy"/utf8>>, function => <<"inject"/utf8>>, line => 108, value => _assert_fail@1, start => 3149, 'end' => 3219, pattern_start => 3160, pattern_end => 3176}) end, Script = <<<<""/utf8>>, _pipe = Html, gleam@string:replace( _pipe, <<""/utf8>>, <