feat: 添加路径检查工具函数
为了支持路由前缀功能,添加了一个路径检查工具函数`makeCheckPathTool`,用于检查请求的路径是否匹配指定的前缀。
This commit is contained in:
parent
35bd784e6a
commit
8944791419
13
index.ts
13
index.ts
@ -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
4
utils/pathTools.ts
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
export const makeCheckPathTool = (url: string, prefix: string) => {
|
||||||
|
const { pathname } = new URL(url)
|
||||||
|
return (path: string) => pathname === `${prefix}/${path}`
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user