import { v4 as uuid } from "uuid" import loggerIns from "./log" import { manageCIMonitorReq } from "./routes/ci" import { manageGitlabEventReq } from "./routes/event" import initSchedule from "./schedule" import netTool from "./service/netTool" import { makeCheckPathTool } from "./utils/pathTools" // 启动定时任务 initSchedule() const PREFIX = "/gitlab_monitor" const server = Bun.serve({ async fetch(req) { // 添加请求ID const logger = loggerIns.child({ requestId: uuid() }) try { // 路由处理 const { exactCheck, fullCheck } = makeCheckPathTool(req.url, PREFIX) // 非根路由打印 if (!fullCheck("/")) logger.info(`${req.method} ${req.url}`) // CI 监控 if (exactCheck("/ci")) return manageCIMonitorReq(req) // Gitlab 事件 if (exactCheck("/event")) return manageGitlabEventReq(req, logger) // 其他 return netTool.ok("hello, there is gitlab monitor, glade to serve you!") } catch (error: any) { // 错误处理 logger.error(`server error: ${error.message}`) return netTool.serverError(error.message || "server error") } }, port: 3000, }) loggerIns.info(`Listening on ${server.hostname}:${server.port}`)