feat: add web backend — Express REST API, WebSocket, full wiring in entry point

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
saopig1andClaude Opus 4.6 committed 2026-03-30 00:47:19 +08:00
1 parent e5d5185f14
commit 0b54809113
7 files changed
+633

No files matched your search

+88
View File
@@ -0,0 +1,88 @@
import { Router } from "express";
import type { BotManager } from "../../bot/manager.js";
import type { BotConfig } from "../../data/config.js";
import type { Logger } from "../../logger.js";
export function createBotRouter(
botManager: BotManager,
config: BotConfig,
logger: Logger
): Router {
const router = Router();
router.get("/", (_req, res) => {
const bots = botManager.getAllBots().map((b) => b.getStatus());
res.json({ bots });
});
router.get("/:id", (req, res) => {
const bot = botManager.getBot(req.params.id);
if (!bot) {
res.status(404).json({ error: "Bot not found" });
return;
}
res.json(bot.getStatus());
});
router.post("/", async (req, res) => {
try {
const {
name,
serverAddress,
serverPort,
nickname,
defaultChannel,
channelPassword,
autoStart,
} = req.body;
if (!name || !serverAddress || !nickname) {
res
.status(400)
.json({ error: "name, serverAddress, and nickname are required" });
return;
}
const bot = await botManager.createBot({
name,
serverAddress,
serverPort: serverPort ?? 9987,
nickname,
defaultChannel,
channelPassword,
autoStart: autoStart ?? false,
});
res.status(201).json(bot.getStatus());
} catch (err) {
logger.error({ err }, "Failed to create bot");
res.status(500).json({ error: (err as Error).message });
}
});
router.delete("/:id", async (req, res) => {
try {
await botManager.removeBot(req.params.id);
res.json({ success: true });
} catch (err) {
res.status(500).json({ error: (err as Error).message });
}
});
router.post("/:id/start", async (req, res) => {
try {
await botManager.startBot(req.params.id);
res.json({ success: true });
} catch (err) {
res.status(500).json({ error: (err as Error).message });
}
});
router.post("/:id/stop", (req, res) => {
try {
botManager.stopBot(req.params.id);
res.json({ success: true });
} catch (err) {
res.status(500).json({ error: (err as Error).message });
}
});
return router;
}