diff --git a/.env b/.env index 14f5aab..85b3b8d 100644 --- a/.env +++ b/.env @@ -4,4 +4,6 @@ # Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB. # See the documentation for all the connection string options: https://pris.ly/d/connection-strings +NODE_ENV=dev + DATABASE_URL="mysql://root:rootpassword@localhost:3306/testdb" \ No newline at end of file diff --git a/.env.prod b/.env.prod new file mode 100644 index 0000000..995fca4 --- /dev/null +++ b/.env.prod @@ -0,0 +1 @@ +NODE_ENV=production \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 04bbd13..c63497e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,3 @@ { - "cSpell.words": ["bunx", "oxlint"] + "cSpell.words": ["bunx", "commitlint", "dotenvx", "oxlint"] } diff --git a/bun.lockb b/bun.lockb index 0e2dd0e..08d5c0e 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/bunfig.toml b/bunfig.toml index 76ef786..5ce510c 100644 --- a/bunfig.toml +++ b/bunfig.toml @@ -1,2 +1,5 @@ [install] -registry = "https://registry.npmjs.org" \ No newline at end of file +registry = "https://registry.npmjs.org" + +[install.scopes] +"@egg" = "https://git.yingbo.im:333/api/packages/zhaoyingbo/npm/" \ No newline at end of file diff --git a/index.ts b/index.ts index 0fea807..e900d7d 100644 --- a/index.ts +++ b/index.ts @@ -1,14 +1,27 @@ import { initSchedule } from "./schedule" +import genContext from "./utils/genContext" initSchedule() Bun.serve({ async fetch(req) { - const url = new URL(req.url) - // 根路由 - if (url.pathname === "/") return new Response("hello, glade to see you!") - if (url.pathname === "/ci") return new Response("OK") - return new Response("OK") + const ctx = await genContext(req) + const { path, genResp, logger } = ctx + if (path.exact("/")) { + logger.info(`${req.method} ${req.url}`) + logger.debug(`req body: ${ctx.text}`) + } + // 逻辑处理 + try { + // 健康检查 + if (path.full("/health")) return genResp.healthCheck() + // 其他 + return genResp.healthCheck("hello, there is egg, glade to serve you!") + } catch (error: any) { + // 错误处理 + logger.error(error.message) + return genResp.serverError(error.message || "server error") + } }, port: 3000, }) diff --git a/package.json b/package.json index af7995a..9681fc1 100644 --- a/package.json +++ b/package.json @@ -3,8 +3,8 @@ "module": "index.ts", "type": "module", "scripts": { - "start": "NODE_ENV=production bun run index.ts", - "dev": "NODE_ENV=dev bun run index.ts --watch", + "start": "dotenvx run -f .env.prod -- bun run index.ts", + "dev": "dotenvx run -f .env -- bun run index.ts --watch", "lint": "oxlint --fix .", "prepare": "husky", "prettier": "prettier --write .", @@ -22,6 +22,7 @@ "@commitlint/config-conventional": "^19.5.0", "@types/lodash": "^4.14.202", "@types/node-schedule": "^2.1.6", + "@types/uuid": "^10.0.0", "@typescript-eslint/eslint-plugin": "^7.8.0", "@typescript-eslint/parser": "^7.8.0", "bun-types": "latest", @@ -36,9 +37,15 @@ "typescript": "^5.0.0" }, "dependencies": { + "@dotenvx/dotenvx": "^1.24.0", + "@egg/hooks": "^1.2.0", + "@egg/logger": "^1.4.4", + "@egg/net-tool": "^1.9.2", + "@egg/path-tool": "^1.4.1", "@prisma/client": "^5.22.0", "lodash": "^4.17.21", "node-schedule": "^2.1.1", - "pocketbase": "^0.21.1" + "pocketbase": "^0.21.1", + "uuid": "^11.0.3" } -} +} \ No newline at end of file diff --git a/utils/genContext.ts b/utils/genContext.ts new file mode 100644 index 0000000..4301670 --- /dev/null +++ b/utils/genContext.ts @@ -0,0 +1,51 @@ +import loggerIns from "@egg/logger" +import { NetTool } from "@egg/net-tool" +import { PathCheckTool } from "@egg/path-tool" +import { v4 as uuid } from "uuid" +import { Logger } from "winston" + +export interface Context { + req: Request + requestId: string + logger: Logger + genResp: NetTool + body: any + text: string + path: PathCheckTool + searchParams: URLSearchParams +} + +/** + * 生成请求上下文。 + * + * @param {Request} req - 请求对象。 + * @returns {Promise} 返回包含请求上下文的对象。 + */ +const genContext = async (req: Request) => { + let body: any = null + let text: string = "" + try { + text = await req.text() + body = JSON.parse(text) + } catch { + /* empty */ + } + const searchParams = new URL(req.url).searchParams + const requestId = uuid() + const logger = loggerIns.child({ requestId }) + const genResp = new NetTool({ requestId }) + const path = new PathCheckTool(req.url) + + return { + req, + path, + requestId, + logger, + genResp, + body, + text, + searchParams, + } as Context +} + +export default genContext