feat(install): build the Flutter web app in Docker, optional ./install --apk, chosen web password, startup report

This commit is contained in:
2026-10-23 21:45:14 +01:00
parent 3136132144
commit 7958d1830e
16 changed files with 776 additions and 29 deletions
+75 -15
View File
@@ -1,10 +1,12 @@
// Zero-dependency static file server + WebSocket proxy for the spike.
// The authenticated web front door for Pipelines: the sign-in page, the Flutter
// web app, and a proxy to the Pipelines server, with no dependencies.
// node serve.mjs [--port 8090] [--host 0.0.0.0] [--pipelines 127.0.0.1:8080]
// [--root DIR] [--updates DIR]
//
// Serves this directory, proxies GET/Upgrade /connect and GET/HEAD /artifacts/ to
// the Pipelines server, so the page, the WebSocket and artifact files share one
// origin. The API and its artifacts live on the server's port; the page is served
// from another, so artifact URLs resolved against the page would 404 without this.
// Serves the app (--root, default ../app/build/web) and proxies /connect,
// /artifacts/ and /chat-uploads to the Pipelines server, so the page, the
// WebSocket and artifact files share one origin. --updates serves the Android
// app's update feed at /updates/.
//
// Why the proxy exists: internal/server/websocket.go calls
// `websocket.Accept(w, r, nil)` (github.com/coder/websocket), which rejects an
@@ -17,7 +19,7 @@
// The default host is 127.0.0.1. Use --host 0.0.0.0 only on a trusted LAN/VPN.
import { createServer, request } from "node:http";
import { createHmac, timingSafeEqual } from "node:crypto";
import { readFileSync } from "node:fs";
import { createReadStream, readFileSync } from "node:fs";
import { homedir } from "node:os";
import { readFile, stat } from "node:fs/promises";
import { extname, join, normalize, resolve, sep } from "node:path";
@@ -25,7 +27,13 @@ import { fileURLToPath } from "node:url";
import net from "node:net";
import { sameOriginUpgrade } from "./src/proxySecurity.js";
const ROOT = resolve(fileURLToPath(new URL(".", import.meta.url)));
// The proxy's own directory holds the sign-in page's icons.
const HERE = resolve(fileURLToPath(new URL(".", import.meta.url)));
// The page root: `--root DIR` (the installer's Flutter web build), by default
// the checkout's app/build/web. `--updates DIR` serves the Android app's
// version.json and APK at /updates/, behind the same sign-in.
const ROOT = resolve(flag(process.argv, "--root", "") || join(HERE, "..", "app", "build", "web"));
const UPDATES = flag(process.argv, "--updates", "") ? resolve(flag(process.argv, "--updates", "")) : null;
// ---------------------------------------------------------------------------
// Access control
@@ -239,6 +247,14 @@ const MIME = {
".png": "image/png",
".ico": "image/x-icon",
".map": "application/json; charset=utf-8",
".wasm": "application/wasm",
".otf": "font/otf",
".ttf": "font/ttf",
".woff": "font/woff",
".woff2": "font/woff2",
".bin": "application/octet-stream",
".frag": "application/octet-stream",
".apk": "application/vnd.android.package-archive",
};
// A path is asset-looking when its last segment ends in a known file type, and a
@@ -288,9 +304,46 @@ function resolvePath(urlPath) {
return target;
}
/** One file of the Android app's update feed, or 404. */
async function serveUpdate(req, res, pathname) {
let name;
try {
name = decodeURIComponent(pathname.slice("/updates/".length));
} catch {
name = "";
}
const path = resolve(join(UPDATES, name));
const info = name && path.startsWith(UPDATES + sep) ? await stat(path).catch(() => null) : null;
if (!info?.isFile()) {
res.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" }).end("Not found");
return;
}
res.writeHead(200, {
"Content-Type": MIME[extname(path)] ?? "application/octet-stream",
"Content-Length": info.size,
"Cache-Control": "no-store",
});
if (req.method === "HEAD") {
res.end();
return;
}
createReadStream(path).pipe(res);
}
/** Send index.html (the page shell). The router reads the real URL in the browser. */
async function serveIndex(res) {
const html = await readFile(join(ROOT, "index.html"), "utf8");
const html = await readFile(join(ROOT, "index.html"), "utf8").catch(() => null);
if (html === null) {
// The installer builds the app in Docker; without it there is no page yet.
res.writeHead(503, { "Content-Type": "text/html; charset=utf-8", "Cache-Control": "no-store" });
res.end(
'<!doctype html><meta charset="utf-8"><title>Pipelines</title>' +
'<body style="font-family:system-ui;padding:2rem;background:#101013;color:#cfd9e6">' +
"<h1>The web app is not built yet</h1>" +
"<p>On the server, start Docker and run <code>./install --web</code>, then reload.</p>",
);
return;
}
// A deep URL such as /runs/440 would otherwise resolve the shell's relative
// `./src/app.js` against /runs/. A root <base> keeps the shell's own asset URLs
// absolute without changing the address bar (and without editing index.html).
@@ -328,14 +381,16 @@ function proxyArtifact(req, res) {
const server = createServer(async (req, res) => {
const pathname = (req.url ?? "/").split("?")[0];
// Brand icons are public so the sign-in page itself can show the app icon.
if (req.method === "GET" && (pathname === "/favicon.svg" || pathname === "/apple-touch-icon.png")) {
const body = await readFile(join(HERE, pathname.slice(1))).catch(() => null);
if (body) {
res.writeHead(200, { "Content-Type": MIME[extname(pathname)], "Cache-Control": "no-store" }).end(body);
return;
}
}
if (auth) {
// Brand assets are public so the sign-in page itself can show the app icon.
if (
req.method === "GET" &&
(pathname === "/favicon.svg" || pathname === "/apple-touch-icon.png")
) {
// fall through to the static handler below
} else if (pathname === "/login" && req.method === "GET") {
if (pathname === "/login" && req.method === "GET") {
if (isAuthed(req)) {
res.writeHead(302, { Location: "/" }).end();
return;
@@ -392,6 +447,10 @@ const server = createServer(async (req, res) => {
proxyArtifact(req, res);
return;
}
if (UPDATES && pathname.startsWith("/updates/") && (req.method === "GET" || req.method === "HEAD")) {
await serveUpdate(req, res, pathname);
return;
}
if (pathname === "/favicon.ico") {
res.writeHead(204).end(); // no icon; keeps the console free of a 404
return;
@@ -497,4 +556,5 @@ server.listen(port, host, () => {
console.log(`web spike: http://${shown}:${port}/ (bound ${host}:${port})`);
console.log(`proxying /connect (ws) and /artifacts/ (http) -> ${targetHost}:${targetPort}`);
console.log(auth ? "auth: sign-in required" : "auth: DISABLED (tests)");
console.log(`serving ${ROOT}${UPDATES ? `; app updates from ${UPDATES}` : ""}`);
});