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 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 52 insertions(+), 33 deletions(-) (limited to 'src/http.ffi.mjs') 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); + }); }; -- cgit v1.2.3