mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-03 03:52:50 +08:00
Initial release
This commit is contained in:
commit
8d9b8d5627
27 files changed
+8140
No files matched your search
@@ -0,0 +1,89 @@
|
||||
import express from "express";
|
||||
import { createServer as createHttpsServer } from "node:https";
|
||||
import { createServer as createHttpServer } from "node:http";
|
||||
import { readFileSync } from "node:fs";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { VoiceBridge, type VoiceBridgeOptions } from "./voice-bridge.js";
|
||||
import type { Logger } from "../logger.js";
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
export interface WebServerOptions {
|
||||
port: number;
|
||||
trustProxy: boolean;
|
||||
staticDir?: string;
|
||||
certDir?: string; // path to cert.pem + key.pem for HTTPS
|
||||
voiceBridgeOptions: VoiceBridgeOptions;
|
||||
logger: Logger;
|
||||
}
|
||||
|
||||
export interface WebServer {
|
||||
start(): Promise<void>;
|
||||
stop(): Promise<void>;
|
||||
}
|
||||
|
||||
export function createWebServer(options: WebServerOptions): WebServer {
|
||||
const app = express();
|
||||
const logger = options.logger.child({ component: "web" });
|
||||
|
||||
let server: ReturnType<typeof createHttpsServer> | ReturnType<typeof createHttpServer>;
|
||||
|
||||
if (options.certDir) {
|
||||
const cert = readFileSync(path.join(options.certDir, "cert.pem"));
|
||||
const key = readFileSync(path.join(options.certDir, "key.pem"));
|
||||
server = createHttpsServer({ cert, key }, app);
|
||||
logger.info("HTTPS enabled");
|
||||
} else {
|
||||
server = createHttpServer(app);
|
||||
}
|
||||
|
||||
if (options.trustProxy) {
|
||||
app.set("trust proxy", true);
|
||||
}
|
||||
|
||||
app.use(express.json({ limit: "100kb" }));
|
||||
|
||||
app.get("/api/health", (_req, res) => {
|
||||
res.json({ status: "ok", version: "0.1.0" });
|
||||
});
|
||||
|
||||
// The browser needs the gateway's default target so a user can edit it in
|
||||
// the join form. No credentials are exposed here.
|
||||
app.get("/api/public-config", (_req, res) => {
|
||||
res.json({
|
||||
tsHost: options.voiceBridgeOptions.tsHost,
|
||||
tsPort: options.voiceBridgeOptions.tsPort,
|
||||
tsServerProtocol: options.voiceBridgeOptions.tsServerProtocol ?? null,
|
||||
});
|
||||
});
|
||||
|
||||
// Serve static frontend
|
||||
if (options.staticDir) {
|
||||
app.use(express.static(options.staticDir));
|
||||
app.get(/^(?!\/api|\/ws)/, (_req, res) => {
|
||||
res.sendFile(path.join(options.staticDir!, "index.html"));
|
||||
});
|
||||
}
|
||||
|
||||
// Voice bridge WebSocket at /ws/voice
|
||||
const voiceBridge = new VoiceBridge(options.voiceBridgeOptions, logger);
|
||||
voiceBridge.attach(server);
|
||||
|
||||
return {
|
||||
start(): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
server.listen(options.port, () => {
|
||||
logger.info({ port: options.port }, "Web server started");
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
},
|
||||
async stop(): Promise<void> {
|
||||
voiceBridge.shutdown();
|
||||
return new Promise((resolve) => {
|
||||
server.close(() => resolve());
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user