From 89447914193cd11bea41f06ff0213e5b3ffe265f Mon Sep 17 00:00:00 2001 From: zhaoyingbo Date: Fri, 26 Jul 2024 09:27:45 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E6=A3=80=E6=9F=A5=E5=B7=A5=E5=85=B7=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 为了支持路由前缀功能,添加了一个路径检查工具函数`makeCheckPathTool`,用于检查请求的路径是否匹配指定的前缀。 --- index.ts | 13 +++++++------ utils/pathTools.ts | 4 ++++ 2 files changed, 11 insertions(+), 6 deletions(-) create mode 100644 utils/pathTools.ts diff --git a/index.ts b/index.ts index 819fe06..9cd9027 100644 --- a/index.ts +++ b/index.ts @@ -2,22 +2,23 @@ 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 { - const url = new URL(req.url) - const prefix = "/gitlab_monitor" + const checkPath = makeCheckPathTool(req.url, PREFIX) // 根路由 - if (url.pathname === `${prefix}/`) - return netTool.ok("hello, glade to see you!") + if (checkPath("/")) return netTool.ok("hello, glade to see you!") // CI 监控 - if (url.pathname === `${prefix}/ci`) return manageCIMonitorReq(req) + if (checkPath("/ci")) return manageCIMonitorReq(req) // Gitlab 事件 - if (url.pathname === `${prefix}/event`) return manageGitlabEventReq(req) + if (checkPath("/event")) return manageGitlabEventReq(req) // 其他 return netTool.ok("hello, glade to see you!") } catch (error: any) { diff --git a/utils/pathTools.ts b/utils/pathTools.ts new file mode 100644 index 0000000..ee5b56d --- /dev/null +++ b/utils/pathTools.ts @@ -0,0 +1,4 @@ +export const makeCheckPathTool = (url: string, prefix: string) => { + const { pathname } = new URL(url) + return (path: string) => pathname === `${prefix}/${path}` +}