feat: 用环境变量控制日志输出级别
This commit is contained in:
1 parent
28d617413e
commit
9905d78e1a
6 files changed
+26
-5
No files matched your search
@@ -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
|
||||
```
|
||||
|
||||
|
||||
+2
-1
@@ -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`) |
|
||||
|
||||
@@ -92,6 +92,7 @@ export type EnvBindings = {
|
||||
OAUTH_ALLOW_ALL?: string; // 是否允许所有用户登录 (仅推荐用于测试演示环境)
|
||||
JWT_SECRET: string;
|
||||
ENCRYPTION_KEY: string;
|
||||
LOG_LEVEL?: string;
|
||||
};
|
||||
|
||||
// 自定义错误类
|
||||
|
||||
@@ -20,7 +20,15 @@ type Bindings = EnvBindings & { ASSETS: { fetch: (req: Request) => Promise<Respo
|
||||
const app = new Hono<{ Bindings: Bindings }>();
|
||||
|
||||
// 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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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=
|
||||
|
||||
Reference in new issue
Block a user