egg_server/index.ts
zhaoyingbo b7437f47e4
All checks were successful
Egg CI/CD / build-image (push) Successful in 49s
Egg CI/CD / deploy (push) Successful in 23s
feat: 优化请求处理 & 拆分Type
2024-06-08 09:15:14 +00:00

30 lines
994 B
TypeScript

import { manageBotReq } from "./routes/bot";
import { manageMessageReq } from "./routes/message";
import { manageMicroAppReq } from "./routes/microApp";
import { initSchedule } from "./schedule";
initSchedule();
Bun.serve({
async fetch(req) {
try {
const url = new URL(req.url);
// 根路由
if (url.pathname === "/") return new Response("hello, glade to see you!");
// 机器人
if (url.pathname === "/bot") return await manageBotReq(req);
// 消息代理发送
if (url.pathname === "/message") return await manageMessageReq(req);
// 小程序
if (url.pathname.startsWith("/micro_app")) return await manageMicroAppReq(req);
// 其他
return new Response("hello, glade to see you!");
} catch (error: any) {
// 错误处理
console.error("🚀 ~ serve ~ error", error);
return new Response(error.message || "server error", { status: 500 });
}
},
port: 3000,
});