mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-01 00:42:49 +08:00
Default server/channel as runtime config for the Docker image (#12)
DEFAULT_SERVER/DEFAULT_CHANNEL used to be build args only, so pointing a self-hosted instance at its server meant cloning the repo and rebuilding the image. The gateway now reads them from its own environment and puts them into the index.html it serves as <meta> tags (HTML-escaped; an inline script would be blocked by the page's CSP). The client prefers those over the build-time VITE_ values, which keep working as before. Closes #12 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU
This commit is contained in:
1 parent
a9f0586b88
commit
8e934c6646
3 files changed
+34
-8
No files matched your search
@@ -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
|
||||
|
||||
@@ -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
|
||||
// <meta> 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 ? [`<meta name="${name}" content="${value.replace(/[&"<>]/g, (c) => `&#${c.charCodeAt(0)};`)}">`] : [],
|
||||
)
|
||||
.join("");
|
||||
|
||||
const MIME_TYPES: Record<string, string> = {
|
||||
".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("</head>", () => `${RUNTIME_CONFIG_META}</head>`));
|
||||
}
|
||||
const ext = path.extname(filePath);
|
||||
res.writeHead(200, {
|
||||
"Content-Type": MIME_TYPES[ext] ?? "application/octet-stream",
|
||||
|
||||
+8
-4
@@ -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 <meta> 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<HTMLMetaElement>(`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
|
||||
|
||||
Reference in new issue
Block a user