gitlab_monitor/index.ts
zhaoyingbo b530229e90
All checks were successful
CI Monitor MIflow / build-image (push) Successful in 59s
feat: 修改打印逻辑 & 请求等待结果
2024-08-13 01:30:15 +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) {
// 添加请求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}`)