mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-03 01:42:50 +08:00
Add Docker deployment and fix crash in insecure (non-HTTPS) contexts
Adds a multi-stage Dockerfile (Rust connector + gateway + web build) and docker-compose.yml for running the whole stack as a single container behind a reverse proxy; the gateway now serves the built web app and the WebSocket endpoint (/ws) from one port, and the frontend derives its gateway URL from window.location in production instead of a hardcoded localhost address. Also guards all navigator.mediaDevices usage against being undefined, which happens when the page is loaded over plain HTTP on a non-localhost address (not a secure context) - previously this crashed the whole React app on mount instead of just disabling the affected voice features.
This commit is contained in:
1 parent
3715a52de6
commit
c48534dbc4
8 files changed
+129
-8
No files matched your search
+52
-2
@@ -1,11 +1,61 @@
|
||||
import { createServer } from "node:http";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { WebSocketServer, type WebSocket } from "ws";
|
||||
import { Ts3Connection, type Ts3ConnectOptions } from "./ts3/connection.js";
|
||||
|
||||
const PORT = Number(process.env.PORT ?? 8080);
|
||||
|
||||
const wss = new WebSocketServer({ port: PORT });
|
||||
// In production (Docker), the built web app lives alongside the gateway and
|
||||
// is served from the same port as the WebSocket endpoint, so a single
|
||||
// reverse-proxied origin (e.g. a Zoraxy subdomain) is enough for everything.
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
const WEB_DIST = process.env.WEB_DIST ?? path.resolve(__dirname, "../../web/dist");
|
||||
|
||||
console.log(`Gateway listening on ws://localhost:${PORT}`);
|
||||
const MIME_TYPES: Record<string, string> = {
|
||||
".html": "text/html",
|
||||
".js": "text/javascript",
|
||||
".css": "text/css",
|
||||
".svg": "image/svg+xml",
|
||||
".png": "image/png",
|
||||
".ico": "image/x-icon",
|
||||
".json": "application/json",
|
||||
".woff2": "font/woff2",
|
||||
};
|
||||
|
||||
const server = createServer((req, res) => {
|
||||
void (async () => {
|
||||
try {
|
||||
const url = new URL(req.url ?? "/", "http://localhost");
|
||||
let filePath = path.join(WEB_DIST, decodeURIComponent(url.pathname));
|
||||
if (!filePath.startsWith(WEB_DIST)) {
|
||||
res.writeHead(403);
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
let body: Buffer;
|
||||
try {
|
||||
body = await readFile(filePath);
|
||||
} catch {
|
||||
// SPA fallback: unknown paths (client-side routes, or "/") serve index.html.
|
||||
filePath = path.join(WEB_DIST, "index.html");
|
||||
body = await readFile(filePath);
|
||||
}
|
||||
res.writeHead(200, { "Content-Type": MIME_TYPES[path.extname(filePath)] ?? "application/octet-stream" });
|
||||
res.end(body);
|
||||
} catch {
|
||||
res.writeHead(404);
|
||||
res.end("Not found");
|
||||
}
|
||||
})();
|
||||
});
|
||||
|
||||
const wss = new WebSocketServer({ server, path: "/ws" });
|
||||
|
||||
server.listen(PORT, () => {
|
||||
console.log(`Gateway listening on http://localhost:${PORT} (WebSocket at /ws)`);
|
||||
});
|
||||
|
||||
wss.on("connection", (socket: WebSocket) => {
|
||||
let connection: Ts3Connection | undefined;
|
||||
|
||||
@@ -62,11 +62,13 @@ export interface Ts3ConnectOptions {
|
||||
}
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
const CONNECTOR_BIN = path.resolve(
|
||||
__dirname,
|
||||
"../../../connector/target/debug",
|
||||
process.platform === "win32" ? "ts-connector.exe" : "ts-connector"
|
||||
);
|
||||
const CONNECTOR_BIN =
|
||||
process.env.CONNECTOR_BIN ??
|
||||
path.resolve(
|
||||
__dirname,
|
||||
"../../../connector/target/debug",
|
||||
process.platform === "win32" ? "ts-connector.exe" : "ts-connector"
|
||||
);
|
||||
|
||||
export class Ts3Connection {
|
||||
private listeners = new Set<(event: Ts3ConnectionEvent) => void>();
|
||||
|
||||
Reference in new issue
Block a user