All checks were successful
Egg Server MIflow / build-image (push) Successful in 1m5s
42 lines
1.4 KiB
TypeScript
42 lines
1.4 KiB
TypeScript
import loggerIns from "./log"
|
|
import { manageBotReq } from "./routes/bot"
|
|
import { manageMessageReq } from "./routes/message"
|
|
import { manageMicroAppReq } from "./routes/microApp"
|
|
import { manageSheetReq } from "./routes/sheet"
|
|
import { initSchedule } from "./schedule"
|
|
import genContext from "./utils/genContext"
|
|
import { makeCheckPathTool } from "./utils/pathTools"
|
|
|
|
initSchedule()
|
|
|
|
const server = Bun.serve({
|
|
async fetch(req) {
|
|
// 生成上下文
|
|
const ctx = await genContext(req)
|
|
try {
|
|
// 路由处理
|
|
const { exactCheck, startsWithCheck, fullCheck } = makeCheckPathTool(
|
|
req.url
|
|
)
|
|
// 非根路由打印
|
|
if (!fullCheck("/")) ctx.logger.info(`${req.method} ${req.url}`)
|
|
// 机器人
|
|
if (exactCheck("/bot")) return await manageBotReq(ctx)
|
|
// 消息代理发送
|
|
if (exactCheck("/message")) return await manageMessageReq(ctx)
|
|
// 表格代理操作
|
|
if (exactCheck("/sheet")) return await manageSheetReq(ctx)
|
|
// 小程序
|
|
if (startsWithCheck("/micro_app")) return await manageMicroAppReq(ctx)
|
|
// 其他
|
|
return ctx.genResp.ok("hello, there is egg, glade to serve you!")
|
|
} catch (error: any) {
|
|
// 错误处理
|
|
return ctx.genResp.serverError(error.message || "server error")
|
|
}
|
|
},
|
|
port: 3000,
|
|
})
|
|
|
|
loggerIns.info(`Listening on ${server.hostname}:${server.port}`)
|