58 lines
1.7 KiB
TypeScript
58 lines
1.7 KiB
TypeScript
import logger from "@egg/logger"
|
|
|
|
import initAppConfig from "./constant/config"
|
|
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"
|
|
|
|
initSchedule()
|
|
|
|
await initAppConfig()
|
|
|
|
const server = Bun.serve({
|
|
async fetch(req) {
|
|
// 生成上下文
|
|
const ctx = await genContext(req)
|
|
const { path, genResp, logger } = ctx
|
|
// 非健康检查由打印必要信息
|
|
if (
|
|
path.exact("/bot") ||
|
|
path.exact("/message") ||
|
|
path.exact("/sheet") ||
|
|
path.startsWith("/micro_app")
|
|
) {
|
|
logger.info(`${req.method} ${req.url}`)
|
|
logger.debug(`req body: ${ctx.text}`)
|
|
}
|
|
// 逻辑处理
|
|
try {
|
|
// 机器人
|
|
if (path.exact("/bot")) return await manageBotReq(ctx)
|
|
// 消息代理发送
|
|
if (path.exact("/message")) return await manageMessageReq(ctx)
|
|
// 表格代理操作
|
|
if (path.exact("/sheet")) return await manageSheetReq(ctx)
|
|
// 小程序
|
|
if (path.startsWith("/micro_app")) return await manageMicroAppReq(ctx)
|
|
// 健康检查
|
|
if (path.full("/health")) return genResp.healthCheck()
|
|
// 其他
|
|
return genResp.healthCheck("hello, there is egg, glade to serve you!")
|
|
} catch (error: any) {
|
|
// 错误处理
|
|
return genResp.serverError(error.message || "server error")
|
|
}
|
|
},
|
|
error(error) {
|
|
logger.error(`Error: ${error}`)
|
|
logger.error(`Stack: ${error.stack}`)
|
|
return new Response("Internal Error", { status: 500 })
|
|
},
|
|
port: 3000,
|
|
})
|
|
|
|
logger.info(`Listening on ${server.hostname}:${server.port}`)
|