diff --git a/README.md b/README.md index 4e21359..0c10efd 100644 --- a/README.md +++ b/README.md @@ -191,6 +191,7 @@ All optional; add them under `environment:` in your compose override next to `PO | `PORT` | `8080` | Port the gateway listens on. | | `WEB_STATIC` | on | Set to `0` to disable serving the built UI (`web/dist`) entirely — the gateway then only answers `/ws`, `/api/feedback` and `/healthz`. Use this when the UI is hosted elsewhere (e.g. a CDN/static host) and the gateway is purely a backend. | | `WEB_DIST` | `web/dist` next to the gateway | Overrides the directory served when `WEB_STATIC` is on. Only needed for non-standard layouts. | +| `DEFAULT_SERVER` / `DEFAULT_CHANNEL` | unset | Server (and optionally channel) every visitor auto-connects to — see [Bake in a default server](#2-bake-in-a-default-server). | | `BROADCAST_TOKEN` | unset | Bearer token required to call `/api/broadcast` (send an in-app notice to all connected clients before a maintenance restart). Endpoint 404s while unset. | | `TLS_CERT` / `TLS_KEY` | unset | Paths to serve the gateway over HTTPS/WSS directly instead of behind a reverse proxy/tunnel. Both must be set together. | | `STORE_ENABLED` | off | Set to `1` to turn this gateway's own `/api/store/themes` endpoint on. Only needed if you want to run your own Design Store instead of using the maintainer's shared one (see `STORE_URL` build arg above) — the endpoint 404s while unset. | @@ -307,13 +308,17 @@ This works on **any** deployment — no rebuild, no configuration. WebSpeak3 con ### 2. Bake in a default server -Running a dedicated instance for one community or event? Set a default at build time so **every** visitor auto-connects with zero parameters, no link needed: +Running a dedicated instance for one community or event? Set a default server so **every** visitor auto-connects with zero parameters, no link needed. With the Docker image that's just two environment variables, no rebuild: -```bash -docker compose build --build-arg DEFAULT_SERVER=voice.example.com --build-arg DEFAULT_CHANNEL=Lobby +```yaml + environment: + - DEFAULT_SERVER=voice.example.com + - DEFAULT_CHANNEL=Lobby ``` -Outside Docker, the same thing is a build-time variable: +The gateway hands them to the UI it serves itself, so this needs `WEB_STATIC` left on. Returning visitors' browsers keep the cached app (including its default) until the next image update, so a later change of these values reaches them only then — new visitors get it right away. + +Building it yourself, you can bake the default in instead: `docker compose build --build-arg DEFAULT_SERVER=voice.example.com --build-arg DEFAULT_CHANNEL=Lobby` (the environment variables above win over these if both are set). Outside Docker, the same thing is a build-time variable: ```bash VITE_DEFAULT_SERVER=voice.example.com VITE_DEFAULT_CHANNEL=Lobby npm run build diff --git a/gateway/src/index.ts b/gateway/src/index.ts index b071867..6ff473b 100644 --- a/gateway/src/index.ts +++ b/gateway/src/index.ts @@ -111,6 +111,19 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url)); const WEB_DIST = process.env.WEB_DIST ?? path.resolve(__dirname, "../../web/dist"); const SERVE_STATIC = process.env.WEB_STATIC !== "0"; +// Runtime counterpart of the web build's VITE_DEFAULT_SERVER/_CHANNEL (GitHub +// issue #12): set on the container, these end up in the served index.html as +// tags the client reads at startup, so changing them needs no rebuild. +// Meta tags rather than an inline script, which CONTENT_SECURITY_POLICY forbids. +const RUNTIME_CONFIG_META = Object.entries({ + "webspeak3-default-server": process.env.DEFAULT_SERVER, + "webspeak3-default-channel": process.env.DEFAULT_CHANNEL, +}) + .flatMap(([name, value]) => + value ? [`]/g, (c) => `&#${c.charCodeAt(0)};`)}">`] : [], + ) + .join(""); + const MIME_TYPES: Record = { ".html": "text/html", ".js": "text/javascript", @@ -475,6 +488,10 @@ const requestHandler = (req: IncomingMessage, res: ServerResponse) => { filePath = path.join(WEB_DIST, "index.html"); body = await readFile(filePath); } + if (RUNTIME_CONFIG_META && filePath === path.join(WEB_DIST, "index.html")) { + // Function replacer: a string one would expand "$&" etc. in the values. + body = Buffer.from(body.toString("utf8").replace("", () => `${RUNTIME_CONFIG_META}`)); + } const ext = path.extname(filePath); res.writeHead(200, { "Content-Type": MIME_TYPES[ext] ?? "application/octet-stream", diff --git a/web/src/App.tsx b/web/src/App.tsx index 01692f0..7ba411d 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -162,10 +162,14 @@ const AUTO_CONNECT_FROM_URL = (() => { // A self-hosted build can also bake in a default server so the page // auto-connects with zero URL params at all - set via `docker compose build // --build-arg DEFAULT_SERVER=...` (see Dockerfile/README) or plain -// VITE_DEFAULT_SERVER= at build time. AUTO_CONNECT_FROM_URL above always wins -// when a share link is present. -const DEFAULT_SERVER = import.meta.env.VITE_DEFAULT_SERVER || null; -const DEFAULT_CHANNEL = import.meta.env.VITE_DEFAULT_CHANNEL || ""; +// VITE_DEFAULT_SERVER= at build time. The Docker image can set the same thing +// at runtime instead (DEFAULT_SERVER/DEFAULT_CHANNEL env vars, issue #12): the +// gateway passes them in as tags, which take precedence over the build +// values. AUTO_CONNECT_FROM_URL above always wins when a share link is present. +const runtimeConfig = (name: string) => + document.querySelector(`meta[name="webspeak3-${name}"]`)?.content; +const DEFAULT_SERVER = runtimeConfig("default-server") || import.meta.env.VITE_DEFAULT_SERVER || null; +const DEFAULT_CHANNEL = runtimeConfig("default-channel") || import.meta.env.VITE_DEFAULT_CHANNEL || ""; // Terms of use, own-hosted instance only (see TODO.md #2/#7.1). Versioned so // a rule change can re-surface the modal: bump TOS_VERSION and everyone who