gitlab_monitor/index.ts
zhaoyingbo 92fa30ef3d
All checks were successful
CI Monitor MIflow / build-image (push) Successful in 2m42s
feat: 支持初步的CR
2024-08-12 12:24:45 +00:00

40 lines
1.2 KiB
TypeScript

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) {
try {
// 添加请求ID
const logger = loggerIns.child({ requestId: uuid() })
// 路由处理
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) {
// 错误处理
console.error("🚀 ~ serve ~ error", error)
return netTool.serverError(error.message || "server error")
}
},
port: 3000,
})
loggerIns.info(`Listening on ${server.hostname}:${server.port}`)