From 012fa97e8b4495acdf097728f3515b925816aa46 Mon Sep 17 00:00:00 2001 From: zhaoyingbo Date: Tue, 30 Jul 2024 09:29:45 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E6=94=B9=E8=B7=AF=E7=94=B1?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E6=96=B9=E5=BC=8F=20&=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=85=A5=E5=8F=A3=E8=B7=AF=E7=94=B1=E6=89=93=E5=8D=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.ts | 13 +++++++------ utils/pathTools.ts | 11 +++++++++-- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/index.ts b/index.ts index 9cd9027..ae67950 100644 --- a/index.ts +++ b/index.ts @@ -12,15 +12,16 @@ const PREFIX = "/gitlab_monitor" const server = Bun.serve({ async fetch(req) { try { - const checkPath = makeCheckPathTool(req.url, PREFIX) - // 根路由 - if (checkPath("/")) return netTool.ok("hello, glade to see you!") + // 打印当前路由 + console.log("🚀 ~ serve ~ req.url", req.url) + // 路由处理 + const { exactCheck } = makeCheckPathTool(req.url, PREFIX) // CI 监控 - if (checkPath("/ci")) return manageCIMonitorReq(req) + if (exactCheck("/ci")) return manageCIMonitorReq(req) // Gitlab 事件 - if (checkPath("/event")) return manageGitlabEventReq(req) + if (exactCheck("/event")) return manageGitlabEventReq(req) // 其他 - return netTool.ok("hello, glade to see you!") + return netTool.ok("hello, there is gitlab monitor, glade to serve you!") } catch (error: any) { // 错误处理 console.error("🚀 ~ serve ~ error", error) diff --git a/utils/pathTools.ts b/utils/pathTools.ts index ee5b56d..eafb389 100644 --- a/utils/pathTools.ts +++ b/utils/pathTools.ts @@ -1,4 +1,11 @@ -export const makeCheckPathTool = (url: string, prefix: string) => { +export const makeCheckPathTool = (url: string, prefix?: string) => { const { pathname } = new URL(url) - return (path: string) => pathname === `${prefix}/${path}` + const prefixPath = prefix ? `${prefix}/` : "" + return { + // 精确匹配 + exactCheck: (path: string) => pathname === `${prefixPath}${path}`, + // 前缀匹配 + startsWithCheck: (path: string) => + pathname.startsWith(`${prefixPath}${path}`), + } }