From 9905d78e1a47c6af3b5cdf560b07e85f2a91a46f Mon Sep 17 00:00:00 2001 From: nap0o Date: Sun, 15 Mar 2026 13:32:30 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=94=A8=E7=8E=AF=E5=A2=83=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E6=8E=A7=E5=88=B6=E6=97=A5=E5=BF=97=E8=BE=93=E5=87=BA?= =?UTF-8?q?=E7=BA=A7=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ README_EN.md | 3 ++- backend/src/app/config.ts | 1 + backend/src/app/index.ts | 10 +++++++++- backend/src/app/server.ts | 12 +++++++++--- docker-compose.yml | 3 +++ 6 files changed, 26 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 96720df..2a15a71 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,7 @@ services: - OAUTH_GITHUB_CLIENT_ID=你的ID - OAUTH_GITHUB_CLIENT_SECRET=你的Secret - OAUTH_GITHUB_REDIRECT_URI=https://你的域名/oauth/callback + - LOG_LEVEL=info restart: unless-stopped ``` 2. 运行 `docker compose up -d` 即可。 @@ -150,6 +151,7 @@ docker run -d --name 2fauth-worker \ -e OAUTH_GITHUB_CLIENT_ID= \ -e OAUTH_GITHUB_CLIENT_SECRET= \ -e OAUTH_GITHUB_REDIRECT_URI=https://你的域名/oauth/callback \ + -e LOG_LEVEL=info \ nap0o/2fauth-worker:latest ``` diff --git a/README_EN.md b/README_EN.md index 149bdb8..b388375 100644 --- a/README_EN.md +++ b/README_EN.md @@ -136,6 +136,7 @@ services: - OAUTH_GITHUB_CLIENT_ID=your_id - OAUTH_GITHUB_CLIENT_SECRET=your_secret - OAUTH_GITHUB_REDIRECT_URI=[https://your-domain.com/oauth/callback](https://your-domain.com/oauth/callback) + - LOG_LEVEL=info restart: unless-stopped ``` 2. Run `docker compose up -d`. @@ -151,6 +152,7 @@ docker run -d --name 2fauth-worker \ -e OAUTH_GITHUB_CLIENT_ID= \ -e OAUTH_GITHUB_CLIENT_SECRET= \ -e OAUTH_GITHUB_REDIRECT_URI=[https://your-domain.com/oauth/callback](https://your-domain.com/oauth/callback) \ + -e LOG_LEVEL=info \ nap0o/2fauth-worker:latest ``` @@ -226,7 +228,6 @@ Regardless of the deployment method, these parameters are critical: | `JWT_SECRET` | Auth Token Secret | Requirement: 32+ characters. | | `OAUTH_ALLOWED_USERS` | **Whitelist**: Access Control | Emails or Telegram IDs, separated by commas. | - ### Variable Mapping for Different Providers (Choose at least one): | Provider | Client ID Variable | Client Secret Variable | Redirect URI (Example: `https://xxx.dev/oauth/callback`) | diff --git a/backend/src/app/config.ts b/backend/src/app/config.ts index 8e7f2d0..af2c07c 100644 --- a/backend/src/app/config.ts +++ b/backend/src/app/config.ts @@ -92,6 +92,7 @@ export type EnvBindings = { OAUTH_ALLOW_ALL?: string; // 是否允许所有用户登录 (仅推荐用于测试演示环境) JWT_SECRET: string; ENCRYPTION_KEY: string; + LOG_LEVEL?: string; }; // 自定义错误类 diff --git a/backend/src/app/index.ts b/backend/src/app/index.ts index 7a96d8d..97592c1 100644 --- a/backend/src/app/index.ts +++ b/backend/src/app/index.ts @@ -20,7 +20,15 @@ type Bindings = EnvBindings & { ASSETS: { fetch: (req: Request) => Promise(); // 1. 全局中间件 -app.use('*', logger()); // 自动打印请求日志 +// 仅在日志级别允许时开启请求日志 (默认开启,除非显式指定为 error 或 warn) +app.use('*', async (c, next) => { + const level = (c.env?.LOG_LEVEL || (typeof process !== 'undefined' ? process.env.LOG_LEVEL : 'info') || 'info').toLowerCase(); + if (level !== 'error' && level !== 'warn') { + // 由于 Hono 的 logger 是一个普通的中间件,我们可以直接调用它 + return logger()(c, next); + } + await next(); +}); app.use('/api/*', cors({ origin: (origin) => origin, // 允许携带 Cookie 时,Origin 不能为 *,这里改为动态反射 credentials: true, // 允许浏览器发送 Cookie diff --git a/backend/src/app/server.ts b/backend/src/app/server.ts index cd81de3..b2754b5 100644 --- a/backend/src/app/server.ts +++ b/backend/src/app/server.ts @@ -9,6 +9,8 @@ import fs from 'fs'; import path from 'path'; import { migrateDatabase } from '@/shared/db/migrator.js'; +const logLevel = (process.env.LOG_LEVEL || 'info').toLowerCase(); + // 1. Resolve paths // In Docker, we run from /app, and frontend is at /app/frontend/dist // The server.js is at /app/backend/dist/server.js @@ -16,8 +18,10 @@ const baseDir = process.cwd(); // Should be /app in Docker const frontendDistPath = path.resolve(baseDir, 'frontend/dist'); const dataDir = path.resolve(baseDir, 'data'); -console.log(`[Docker Server] Base directory: ${baseDir}`); -console.log(`[Docker Server] Frontend dist path: ${frontendDistPath}`); +if (logLevel !== 'error' && logLevel !== 'warn') { + console.log(`[Docker Server] Base directory: ${baseDir}`); + console.log(`[Docker Server] Frontend dist path: ${frontendDistPath}`); +} // 2. Ensure data directory exists and is writable if (!fs.existsSync(dataDir)) { @@ -96,7 +100,9 @@ try { (async () => { try { await migrateDatabase(dbExecutor as any); - console.log('[Database] Migrations verified/applied.'); + if (logLevel !== 'error' && logLevel !== 'warn') { + console.log('[Database] Migrations verified/applied.'); + } } catch (e: any) { console.error('[Database] Critical: Migration failed:', e.message); process.exit(1); diff --git a/docker-compose.yml b/docker-compose.yml index c6c46b8..f8657d1 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -21,6 +21,9 @@ services: # [必填] 允许登录系统的用户邮箱或Telegram用户名,多个用逗号隔开 - OAUTH_ALLOWED_USERS=your_email@example.com + # [可选] 日志级别: debug, info, warn, error. + - LOG_LEVEL=info + # =========== 登录供应商配置 (至少选择一种,推荐使用 Github) =========== # Google 登录配置 # - OAUTH_GOOGLE_CLIENT_ID=