diff options
author | Giacomo Cavalieri <giacomo.cavalieri@icloud.com> | 2024-01-04 19:13:14 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-04 18:13:14 +0000 |
commit | a38889c285bef95359e36c93da37221126f96556 (patch) | |
tree | 33f07bcccc5a2f46e2cda51cac7a86b22885337f /src/http_ffi.erl | |
parent | a07bba0954e440ccd3eb4d167e1eacb0065cc0af (diff) | |
download | lustre-a38889c285bef95359e36c93da37221126f96556.tar.gz lustre-a38889c285bef95359e36c93da37221126f96556.zip |
🔀 Add logging to `lustre/try` (#30)
* :heavy_plus_sign: Add `gleam_community_ansi` dependency
* :sparkles: Add log message when server is started
* :sparkles: Add retry policy and logging if port is taken
Diffstat (limited to 'src/http_ffi.erl')
-rw-r--r-- | src/http_ffi.erl | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/src/http_ffi.erl b/src/http_ffi.erl index 507ee06..5304ee4 100644 --- a/src/http_ffi.erl +++ b/src/http_ffi.erl @@ -1,7 +1,7 @@ -module(http_ffi). --export([serve/1]). +-export([serve/4]). -serve(Port) -> +serve(Host, Port, OnStart, OnPortTaken) -> {ok, Pattern} = re:compile("name *= *\"(?<Name>.+)\""), {ok, Toml} = file:read_file("gleam.toml"), {match, [Name]} = re:run(Toml, Pattern, [{capture, all_names, binary}]), @@ -43,25 +43,51 @@ serve(Port) -> inets:start(), Address = {127, 0, 0, 1}, + ActualPort = + case port_available(Port) of + true -> + Port; + false -> + OnPortTaken(Port), + first_available_port(Port + 1) + end, + {ok, Pid} = httpd:start_service([ {bind_address, Address}, {document_root, AbsPath}, {server_root, AbsPath}, {directory_index, ["index.html"]}, - {server_name, "localhost"}, - {port, Port}, + {server_name, binary_to_list(Host)}, + {port, ActualPort}, {default_type, "text/html"}, {mime_types, mime_types()}, {modules, [mod_alias, mod_dir, mod_get]} ]), + OnStart(ActualPort), + receive {From, shutdown} -> ok = httpd:stop_service(Pid), From ! done end. +port_available(Port) -> + case gen_tcp:listen(Port, []) of + {ok, Sock} -> + ok = gen_tcp:close(Sock), + true; + _ -> + false + end. + +first_available_port(Port) -> + case port_available(Port) of + true -> Port; + false -> first_available_port(Port + 1) + end. + mime_types() -> [ {"html", "text/html"}, |