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. --- gleam.toml | 2 ++ manifest.toml | 5 +++ src/http.ffi.mjs | 85 ++++++++++++++++++++++++++------------------ src/http_ffi.erl | 13 ++++--- src/lustre/try.gleam | 99 ++++++++++++++++++++++++++++++++++++++++++---------- 5 files changed, 149 insertions(+), 55 deletions(-) diff --git a/gleam.toml b/gleam.toml index 0c4250d..f288a24 100644 --- a/gleam.toml +++ b/gleam.toml @@ -17,3 +17,5 @@ internal_modules = [ [dependencies] gleam_stdlib = "~> 0.34" gleam_community_ansi = "~> 1.3" +glint = "~> 0.14" +argv = "~> 1.0" diff --git a/manifest.toml b/manifest.toml index ef7a2b2..0c1900d 100644 --- a/manifest.toml +++ b/manifest.toml @@ -2,11 +2,16 @@ # You typically do not need to edit this file packages = [ + { name = "argv", version = "1.0.1", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "A6E9009E50BBE863EB37D963E4315398D41A3D87D0075480FC244125808F964A" }, { name = "gleam_community_ansi", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "AB7C3CCC894653637E02DC455D5890C8CF3064E83E78CFE61145A4C458D02DE6" }, { name = "gleam_community_colour", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "A49A5E3AE8B637A5ACBA80ECB9B1AFE89FD3D5351FF6410A42B84F666D40D7D5" }, { name = "gleam_stdlib", version = "0.34.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "1FB8454D2991E9B4C0C804544D8A9AD0F6184725E20D63C3155F0AEB4230B016" }, + { name = "glint", version = "0.14.0", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_stdlib", "gleam_community_colour", "snag"], otp_app = "glint", source = "hex", outer_checksum = "21AB16D5A50D4EF34DF935915FDBEE06B2DAEDEE3FCC8584C6E635A866566B38" }, + { name = "snag", version = "0.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "snag", source = "hex", outer_checksum = "54D32E16E33655346AA3E66CBA7E191DE0A8793D2C05284E3EFB90AD2CE92BCC" }, ] [requirements] +argv = { version = "~> 1.0" } gleam_community_ansi = { version = "~> 1.3" } gleam_stdlib = { version = "~> 0.34" } +glint = { version = "~> 0.14" } 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, "