diff --git a/.gitignore b/.gitignore index 54b76a2..41f0697 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,6 @@ docker-compose.override.yml .claude/ CLAUDE.md .serena/ + +# TLS material for local HTTPS - a private key never belongs in the repo. +gateway/certs/ diff --git a/gateway/src/index.ts b/gateway/src/index.ts index 18512af..f648041 100644 --- a/gateway/src/index.ts +++ b/gateway/src/index.ts @@ -1,4 +1,6 @@ import { createServer, type IncomingMessage, type ServerResponse } from "node:http"; +import { createServer as createSecureServer } from "node:https"; +import { readFileSync } from "node:fs"; import { readFile, appendFile, rename, stat } from "node:fs/promises"; import path from "node:path"; import { fileURLToPath } from "node:url"; @@ -249,7 +251,26 @@ const DEV_HINT = ` { res.end(DEV_HINT); } })(); -}); +}; + +// The frontend derives ws:// or wss:// from the page's own scheme, so the +// WebSocket follows this choice without any further configuration. +const server = tlsOptions + ? createSecureServer(tlsOptions, requestHandler) + : createServer(requestHandler); const wss = new WebSocketServer({ server, path: "/ws" }); @@ -319,7 +346,10 @@ const heartbeatTimer = setInterval(() => { wss.on("close", () => clearInterval(heartbeatTimer)); server.listen(PORT, () => { - console.log(`WebSpeak3 gateway listening on http://localhost:${PORT} (WebSocket at /ws)`); + const scheme = tlsOptions ? "https" : "http"; + console.log( + `WebSpeak3 gateway listening on ${scheme}://localhost:${PORT} (WebSocket at /ws)` + ); if (SERVE_STATIC) { console.log(`Serving static UI from ${WEB_DIST}`); console.log(`Dev tip: use http://localhost:5173/ for live UI; rebuild web/dist after UI changes if you open :${PORT}`);