diff --git a/src/web/api/player.ts b/src/web/api/player.ts index 67e7ce7..003aa33 100644 --- a/src/web/api/player.ts +++ b/src/web/api/player.ts @@ -57,55 +57,46 @@ export function createPlayerRouter( } }); - router.post("/:botId/pause", async (req, res) => { - const bot = (req as any).bot; - const cmd = parseCommand("!pause", "!")!; - const response = await bot.executeCommand(cmd); - res.json({ message: response }); - }); + const simpleCommand = (cmdStr: string) => async (req: any, res: any) => { + try { + const bot = req.bot; + const cmd = parseCommand(cmdStr, "!")!; + const response = await bot.executeCommand(cmd); + res.json({ message: response }); + } catch (err) { + res.status(500).json({ error: (err as Error).message }); + } + }; - router.post("/:botId/resume", async (req, res) => { - const bot = (req as any).bot; - const cmd = parseCommand("!resume", "!")!; - const response = await bot.executeCommand(cmd); - res.json({ message: response }); - }); - - router.post("/:botId/next", async (req, res) => { - const bot = (req as any).bot; - const cmd = parseCommand("!next", "!")!; - const response = await bot.executeCommand(cmd); - res.json({ message: response }); - }); - - router.post("/:botId/prev", async (req, res) => { - const bot = (req as any).bot; - const cmd = parseCommand("!prev", "!")!; - const response = await bot.executeCommand(cmd); - res.json({ message: response }); - }); - - router.post("/:botId/stop", async (req, res) => { - const bot = (req as any).bot; - const cmd = parseCommand("!stop", "!")!; - const response = await bot.executeCommand(cmd); - res.json({ message: response }); - }); + router.post("/:botId/pause", simpleCommand("!pause")); + router.post("/:botId/resume", simpleCommand("!resume")); + router.post("/:botId/next", simpleCommand("!next")); + router.post("/:botId/prev", simpleCommand("!prev")); + router.post("/:botId/stop", simpleCommand("!stop")); + router.post("/:botId/clear", simpleCommand("!clear")); router.post("/:botId/volume", async (req, res) => { - const bot = (req as any).bot; - const { volume } = req.body; - const cmd = parseCommand(`!vol ${volume}`, "!")!; - const response = await bot.executeCommand(cmd); - res.json({ message: response }); + try { + const bot = (req as any).bot; + const { volume } = req.body; + const cmd = parseCommand(`!vol ${volume}`, "!")!; + const response = await bot.executeCommand(cmd); + res.json({ message: response }); + } catch (err) { + res.status(500).json({ error: (err as Error).message }); + } }); router.post("/:botId/mode", async (req, res) => { - const bot = (req as any).bot; - const { mode } = req.body; - const cmd = parseCommand(`!mode ${mode}`, "!")!; - const response = await bot.executeCommand(cmd); - res.json({ message: response }); + try { + const bot = (req as any).bot; + const { mode } = req.body; + const cmd = parseCommand(`!mode ${mode}`, "!")!; + const response = await bot.executeCommand(cmd); + res.json({ message: response }); + } catch (err) { + res.status(500).json({ error: (err as Error).message }); + } }); router.get("/:botId/queue", (req, res) => { @@ -113,18 +104,15 @@ export function createPlayerRouter( res.json({ queue: bot.getQueue(), status: bot.getStatus() }); }); - router.post("/:botId/clear", async (req, res) => { - const bot = (req as any).bot; - const cmd = parseCommand("!clear", "!")!; - const response = await bot.executeCommand(cmd); - res.json({ message: response }); - }); - router.delete("/:botId/queue/:index", async (req, res) => { - const bot = (req as any).bot; - const cmd = parseCommand(`!remove ${req.params.index}`, "!")!; - const response = await bot.executeCommand(cmd); - res.json({ message: response }); + try { + const bot = (req as any).bot; + const cmd = parseCommand(`!remove ${req.params.index}`, "!")!; + const response = await bot.executeCommand(cmd); + res.json({ message: response }); + } catch (err) { + res.status(500).json({ error: (err as Error).message }); + } }); router.post("/:botId/playlist", async (req, res) => { diff --git a/src/web/server.ts b/src/web/server.ts index a97f926..ce3373f 100644 --- a/src/web/server.ts +++ b/src/web/server.ts @@ -56,13 +56,13 @@ export function createWebServer(options: WebServerOptions): WebServer { if (options.staticDir) { app.use(express.static(options.staticDir)); - app.get("*", (_req, res) => { + app.get(/^(?!\/api|\/ws)/, (_req, res) => { res.sendFile(path.join(options.staticDir!, "index.html")); }); } const wss = new WebSocketServer({ server, path: "/ws" }); - setupWebSocket(wss, options.botManager, logger); + const cleanupWs = setupWebSocket(wss, options.botManager, logger); return { async start(): Promise { @@ -74,6 +74,7 @@ export function createWebServer(options: WebServerOptions): WebServer { }); }, stop(): void { + cleanupWs(); wss.close(); server.close(); }, diff --git a/src/web/websocket.ts b/src/web/websocket.ts index a8d5a14..af8934f 100644 --- a/src/web/websocket.ts +++ b/src/web/websocket.ts @@ -6,7 +6,7 @@ export function setupWebSocket( wss: WebSocketServer, botManager: BotManager, logger: Logger -): void { +): () => void { const clients = new Set(); wss.on("connection", (ws) => { @@ -65,6 +65,10 @@ export function setupWebSocket( } }; - setInterval(attachBotListeners, 5000); + const intervalId = setInterval(attachBotListeners, 5000); attachBotListeners(); + + return () => { + clearInterval(intervalId); + }; }