feat: 修改路由判断方式 & 增加入口路由打印
All checks were successful
Egg Server MIflow / build-image (push) Successful in 42s
All checks were successful
Egg Server MIflow / build-image (push) Successful in 42s
This commit is contained in:
parent
9e6b4f4f5d
commit
6ac6bfd8ab
19
index.ts
19
index.ts
@ -4,26 +4,27 @@ import { manageMicroAppReq } from "./routes/microApp"
|
||||
import { manageSheetReq } from "./routes/sheet"
|
||||
import { initSchedule } from "./schedule"
|
||||
import netTool from "./services/netTool"
|
||||
import { makeCheckPathTool } from "./utils/pathTools"
|
||||
|
||||
initSchedule()
|
||||
|
||||
const server = Bun.serve({
|
||||
async fetch(req) {
|
||||
try {
|
||||
const url = new URL(req.url)
|
||||
// 根路由
|
||||
if (url.pathname === "/") return netTool.ok("hello, glade to see you!")
|
||||
// 打印当前路由
|
||||
console.log("🚀 ~ serve ~ req.url", req.url)
|
||||
// 路由处理
|
||||
const { exactPath, startsWithPath } = makeCheckPathTool(req.url)
|
||||
// 机器人
|
||||
if (url.pathname === "/bot") return await manageBotReq(req)
|
||||
if (exactPath("/bot")) return await manageBotReq(req)
|
||||
// 消息代理发送
|
||||
if (url.pathname === "/message") return await manageMessageReq(req)
|
||||
if (exactPath("/message")) return await manageMessageReq(req)
|
||||
// 表格代理操作
|
||||
if (url.pathname === "/sheet") return await manageSheetReq(req)
|
||||
if (exactPath("/sheet")) return await manageSheetReq(req)
|
||||
// 小程序
|
||||
if (url.pathname.startsWith("/micro_app"))
|
||||
return await manageMicroAppReq(req)
|
||||
if (startsWithPath("/micro_app")) return await manageMicroAppReq(req)
|
||||
// 其他
|
||||
return netTool.ok("hello, glade to see you!")
|
||||
return netTool.ok("hello, there is egg, glade to serve you!")
|
||||
} catch (error: any) {
|
||||
// 错误处理
|
||||
console.error("🚀 ~ serve ~ error", error)
|
||||
|
@ -1,6 +1,6 @@
|
||||
import service from "../../services"
|
||||
import netTool from "../../services/netTool"
|
||||
import { trimPathPrefix } from "../../utils/pathTools"
|
||||
import { makeCheckPathTool } from "../../utils/pathTools"
|
||||
|
||||
/**
|
||||
* 处理登录请求
|
||||
@ -79,14 +79,13 @@ const manageBatchUser = async (req: Request) => {
|
||||
* @returns
|
||||
*/
|
||||
export const manageMicroAppReq = async (req: Request) => {
|
||||
const url = new URL(req.url)
|
||||
const withoutPrefix = trimPathPrefix(url.pathname, "/micro_app")
|
||||
const { exactPath } = makeCheckPathTool(req.url, "/micro_app")
|
||||
// 处理登录请求
|
||||
if (withoutPrefix === "/login") {
|
||||
if (exactPath("/login")) {
|
||||
return manageLogin(req)
|
||||
}
|
||||
// 处理批量获取用户信息请求
|
||||
if (withoutPrefix === "/batch_user") {
|
||||
if (exactPath("/batch_user")) {
|
||||
return manageBatchUser(req)
|
||||
}
|
||||
return netTool.ok()
|
||||
|
@ -1,8 +1,3 @@
|
||||
// 裁剪指定prefix路径
|
||||
export function trimPathPrefix(path: string, prefix: string): string {
|
||||
return path.startsWith(prefix) ? path.slice(prefix.length) : path
|
||||
}
|
||||
|
||||
export const safeJsonStringify = (obj: any) => {
|
||||
try {
|
||||
return JSON.stringify(obj)
|
||||
@ -10,3 +5,15 @@ export const safeJsonStringify = (obj: any) => {
|
||||
return String(obj)
|
||||
}
|
||||
}
|
||||
|
||||
export const makeCheckPathTool = (url: string, prefix?: string) => {
|
||||
const { pathname } = new URL(url)
|
||||
const prefixPath = prefix ? `${prefix}/` : ""
|
||||
return {
|
||||
// 精确匹配
|
||||
exactPath: (path: string) => pathname === `${prefixPath}${path}`,
|
||||
// 前缀匹配
|
||||
startsWithPath: (path: string) =>
|
||||
pathname.startsWith(`${prefixPath}${path}`),
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user