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.mjs | |
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.mjs')
-rw-r--r-- | src/http.ffi.mjs | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/src/http.ffi.mjs b/src/http.ffi.mjs index 06d5b31..3f59933 100644 --- a/src/http.ffi.mjs +++ b/src/http.ffi.mjs @@ -85,6 +85,17 @@ const server = Http.createServer((req, res) => { } }); -export const serve = (port) => { - server.listen(port, "localhost"); +export const serve = (host, port, on_start, on_port_taken) => { + let tries = 1; + server.on("error", (error) => { + if (error.code === "EADDRINUSE") { + let is_first_try = tries === 1; + if (is_first_try) { + on_port_taken(port); + } + tries++; + port++; + server.listen(port, host); + } + }).listen(port, host, () => { on_start(port) }); }; |