feat: 添加路径检查工具函数
Some checks failed
CI Monitor CI/CD / build-image (push) Successful in 31s
CI Monitor MIflow / build-image (push) Successful in 34s
CI Monitor CI/CD / deploy (push) Failing after 1m5s

为了支持路由前缀功能,添加了一个路径检查工具函数`makeCheckPathTool`,用于检查请求的路径是否匹配指定的前缀。
This commit is contained in:
zhaoyingbo 2024-07-26 09:27:45 +00:00
parent 35bd784e6a
commit 8944791419
2 changed files with 11 additions and 6 deletions

View File

@ -2,22 +2,23 @@ import { manageCIMonitorReq } from "./routes/ci"
import { manageGitlabEventReq } from "./routes/event" import { manageGitlabEventReq } from "./routes/event"
import initSchedule from "./schedule" import initSchedule from "./schedule"
import netTool from "./service/netTool" import netTool from "./service/netTool"
import { makeCheckPathTool } from "./utils/pathTools"
// 启动定时任务 // 启动定时任务
initSchedule() initSchedule()
const PREFIX = "/gitlab_monitor"
const server = Bun.serve({ const server = Bun.serve({
async fetch(req) { async fetch(req) {
try { try {
const url = new URL(req.url) const checkPath = makeCheckPathTool(req.url, PREFIX)
const prefix = "/gitlab_monitor"
// 根路由 // 根路由
if (url.pathname === `${prefix}/`) if (checkPath("/")) return netTool.ok("hello, glade to see you!")
return netTool.ok("hello, glade to see you!")
// CI 监控 // CI 监控
if (url.pathname === `${prefix}/ci`) return manageCIMonitorReq(req) if (checkPath("/ci")) return manageCIMonitorReq(req)
// Gitlab 事件 // Gitlab 事件
if (url.pathname === `${prefix}/event`) return manageGitlabEventReq(req) if (checkPath("/event")) return manageGitlabEventReq(req)
// 其他 // 其他
return netTool.ok("hello, glade to see you!") return netTool.ok("hello, glade to see you!")
} catch (error: any) { } catch (error: any) {

4
utils/pathTools.ts Normal file
View File

@ -0,0 +1,4 @@
export const makeCheckPathTool = (url: string, prefix: string) => {
const { pathname } = new URL(url)
return (path: string) => pathname === `${prefix}/${path}`
}