From 775bc596ac84f5b28b4e0e1654525a360002711f Mon Sep 17 00:00:00 2001 From: Hayleigh Thompson Date: Fri, 5 Jan 2024 08:25:08 +0000 Subject: =?UTF-8?q?=F0=9F=94=80=20Add=20flags=20for=20configuring=20the=20?= =?UTF-8?q?server's=20host=20and=20port.=20(#33)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * :sparkles: Add the option to include lustre_ui styles from a CDN. * :heavy_plus_sign: Add glint and argv as dependencies. * :sparkles: Use glint for cli args parsing. This adds support for user-configurable host and port, as well as the ability to opt-in to including lustre_ui's stylesheet with the --include-styles flag. --- src/http.ffi.mjs | 85 ++++++++++++++++++++++++++------------------ src/http_ffi.erl | 13 ++++--- src/lustre/try.gleam | 99 ++++++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 142 insertions(+), 55 deletions(-) (limited to 'src') diff --git a/src/http.ffi.mjs b/src/http.ffi.mjs index d1989bf..aad5e3d 100644 --- a/src/http.ffi.mjs +++ b/src/http.ffi.mjs @@ -9,29 +9,14 @@ const root = Path.join(cwd, "build/dev/javascript"); const toml = readFileSync(Path.join(cwd, "gleam.toml"), "utf-8"); const name = toml.match(/name *= *"(.+)"/)[1]; -const html = ` - - - - - Lustre preview server - - - - -
- -`; +let html; const server = Http.createServer((req, res) => { - res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, private'); - res.setHeader('Pragma', 'no-cache'); + res.setHeader( + "Cache-Control", + "no-store, no-cache, must-revalidate, private" + ); + res.setHeader("Pragma", "no-cache"); switch (true) { case req.url === "/": { @@ -88,17 +73,51 @@ const server = Http.createServer((req, res) => { } }); -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); +export const serve = ( + { host, port, include_styles }, + on_start, + on_port_taken +) => { + let is_first_try = true; + + html = ` + + + + + Lustre preview server + + ${ + include_styles + ? `` + : "" + } + + + + +
+ +`; + + server + .on("error", (error) => { + if (error.code === "EADDRINUSE") { + if (is_first_try) { + on_port_taken(port); + is_first_try = false; + } + + server.listen(++port, host); } - tries++; - port++; - server.listen(port, host); - } - }).listen(port, host, () => { on_start(port) }); + }) + .listen(port, host, () => { + on_start(port); + }); }; diff --git a/src/http_ffi.erl b/src/http_ffi.erl index e9de7e7..355fabd 100644 --- a/src/http_ffi.erl +++ b/src/http_ffi.erl @@ -1,7 +1,7 @@ -module(http_ffi). --export([serve/4, response_default_headers/0]). +-export([serve/3, response_default_headers/0]). -serve(Host, Port, OnStart, OnPortTaken) -> +serve({options, Host, Port, IncludeStyles}, OnStart, OnPortTaken) -> {ok, Pattern} = re:compile("name *= *\"(?.+)\""), {ok, Toml} = file:read_file("gleam.toml"), {match, [Name]} = re:run(Toml, Pattern, [{capture, all_names, binary}]), @@ -13,8 +13,13 @@ serve(Host, Port, OnStart, OnPortTaken) -> "\n" " \n" " \n" - " Lustre preview server\n" - "\n" + " Lustre preview server\n", + case IncludeStyles of + true -> + <<" \n">>; + false -> + <<"">> + end/binary, "