diff --git a/.env.example b/.env.example index 7ac66d6..01ad544 100644 --- a/.env.example +++ b/.env.example @@ -1,6 +1,9 @@ # 敏感配置模板:复制为 .env 并填入真实值(.env 已被 gitignore,不会提交) # 环境变量优先于 config.yaml 中的对应配置 +# 进程时区(IANA 名称),日志时间戳与 /status 页所有时间均按此显示 +TZ=Asia/Shanghai + # 服务访问密钥(/status、/self-info 等端点的鉴权) # FQ_SERVER_ACCESS_KEY=your-access-key diff --git a/cmd/fanqie/main.go b/cmd/fanqie/main.go index a8ddc70..328b407 100644 --- a/cmd/fanqie/main.go +++ b/cmd/fanqie/main.go @@ -14,6 +14,9 @@ import ( "os/signal" "syscall" "time" + + // 嵌入 tzdata,容器内无系统时区库时 TZ 仍可用 + _ "time/tzdata" ) // Version / BuildTime 由编译时 -ldflags "-X main.Version=... -X main.BuildTime=..." 注入 @@ -26,6 +29,9 @@ var ( ) func main() { + // 时区生效须先于一切时间显示(.env 中的 TZ) + config.ApplyTimezone() + logger.Info(fmt.Sprintf("番茄小说API服务 版本=%s 编译时间=%s", Version, BuildTime)) // 加载配置文件 diff --git a/config/config.yaml b/config/config.yaml index 5c45c57..abe1dfe 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -5,10 +5,12 @@ server: port: "40180" # 监听端口 host: "0.0.0.0" # 监听地址 access_key: "" # 访问密钥,为空则不启用验证(用于 /status) + rate_limit_qps: 0 # 每 IP 每秒请求上限(令牌桶,突发 10),0=不限制 # 请求日志(只记元数据与错误摘要,不存响应体;在 /status 页"最近请求"展示) + # 注意:日志时间显示与"近 7 天请求"统计依赖 .env 中的 TZ 时区 request_log: - enabled: false + enabled: true retention_days: 7 # 日志保留天数,自动清理 # 番茄小说配置 diff --git a/deploy.sh b/deploy.sh index ce00c16..c754e6b 100755 --- a/deploy.sh +++ b/deploy.sh @@ -60,6 +60,7 @@ BUILD_ONLY=false ROLLBACK=false ROLLBACK_VERSION="" LIST=false +FORCE=false print_help() { cat <=400 时截断保留 + ErrorMsg string `json:"error_msg"` // 仅 status>=400 时截断保留 + UserAgent string `json:"user_agent"` // 客户端 UA,排障时区分调用方 } // ErrMsgMax 错误响应摘要的最大保留字节数 @@ -52,7 +54,10 @@ func InitRequestLog() error { query TEXT, status INTEGER, duration_ms INTEGER, - error_msg TEXT + error_msg TEXT, + user_agent TEXT, + created_at INTEGER DEFAULT (strftime('%s', 'now')), + updated_at INTEGER DEFAULT (strftime('%s', 'now')) )` if dbDriver == "pgx" { ddl = `CREATE TABLE IF NOT EXISTS request_log ( @@ -64,14 +69,49 @@ func InitRequestLog() error { query TEXT, status INTEGER, duration_ms BIGINT, - error_msg TEXT + error_msg TEXT, + user_agent TEXT, + created_at BIGINT DEFAULT extract(epoch FROM now())::bigint, + updated_at BIGINT DEFAULT extract(epoch FROM now())::bigint )` } if _, err := cacheDB.Exec(ddl); err != nil { return fmt.Errorf("创建request_log表失败: %w", err) } - if _, err := cacheDB.Exec(`CREATE INDEX IF NOT EXISTS idx_request_log_ts ON request_log(ts)`); err != nil { - cacheLogger.Warning("创建request_log索引失败: " + err.Error()) + // 旧表补列(表已存在时 CREATE 不会加列;PG 支持 IF NOT EXISTS,SQLite 报错即视为已存在) + if dbDriver == "pgx" { + for _, col := range []string{ + `ALTER TABLE request_log ADD COLUMN IF NOT EXISTS user_agent TEXT`, + `ALTER TABLE request_log ADD COLUMN IF NOT EXISTS created_at BIGINT DEFAULT 0`, + `ALTER TABLE request_log ADD COLUMN IF NOT EXISTS updated_at BIGINT DEFAULT 0`, + } { + if _, err := cacheDB.Exec(col); err != nil { + cacheLogger.Warning("补加列失败: " + err.Error()) + } + } + } else { + for _, col := range []string{ + `ALTER TABLE request_log ADD COLUMN user_agent TEXT`, + `ALTER TABLE request_log ADD COLUMN created_at INTEGER DEFAULT 0`, + `ALTER TABLE request_log ADD COLUMN updated_at INTEGER DEFAULT 0`, + } { + if _, err := cacheDB.Exec(col); err != nil { + cacheLogger.Debug("补列跳过(已存在): " + err.Error()) + } + } + } + // append-only 日志表:旧行按 ts 回填两个时间列(幂等) + if _, err := cacheDB.Exec(q(`UPDATE request_log SET created_at = ts, updated_at = ts WHERE created_at = 0`)); err != nil { + cacheLogger.Warning("request_log时间列回填失败: " + err.Error()) + } + for _, idx := range []string{ + `CREATE INDEX IF NOT EXISTS idx_request_log_ts ON request_log(ts)`, + `CREATE INDEX IF NOT EXISTS idx_request_log_ip ON request_log(ip)`, + `CREATE INDEX IF NOT EXISTS idx_request_log_status ON request_log(status)`, + } { + if _, err := cacheDB.Exec(idx); err != nil { + cacheLogger.Warning("创建request_log索引失败: " + err.Error()) + } } reqLogCh = make(chan RequestLogEntry, reqLogChanSize) @@ -109,7 +149,7 @@ func GetRecentRequests(limit int) []RequestLogEntry { return nil } rows, err := cacheDB.Query( - q("SELECT ts, ip, method, path, query, status, duration_ms, COALESCE(error_msg, '') FROM request_log ORDER BY id DESC LIMIT ?"), limit) + q("SELECT ts, ip, method, path, query, status, duration_ms, COALESCE(error_msg, ''), COALESCE(user_agent, '') FROM request_log ORDER BY id DESC LIMIT ?"), limit) if err != nil { cacheLogger.Warning("查询请求日志失败: " + err.Error()) return nil @@ -119,7 +159,7 @@ func GetRecentRequests(limit int) []RequestLogEntry { entries := make([]RequestLogEntry, 0, limit) for rows.Next() { var e RequestLogEntry - if err := rows.Scan(&e.Ts, &e.IP, &e.Method, &e.Path, &e.Query, &e.Status, &e.DurationMs, &e.ErrorMsg); err != nil { + if err := rows.Scan(&e.Ts, &e.IP, &e.Method, &e.Path, &e.Query, &e.Status, &e.DurationMs, &e.ErrorMsg, &e.UserAgent); err != nil { cacheLogger.Warning("读取请求日志失败: " + err.Error()) break } @@ -128,6 +168,66 @@ func GetRecentRequests(limit int) []RequestLogEntry { return entries } +// DailyStat 一天的请求量统计 +type DailyStat struct { + Day string `json:"day"` // 本地时区 MM-DD + Count int64 `json:"count"` + Pct int `json:"pct"` // 相对窗口峰值的百分比(进度条宽度) + MaxCount bool `json:"max_count"` // 是否为窗口内峰值(前端高亮用) +} + +// GetDailyRequestStats 按本地时区分天统计最近 days 天(含今天)的请求量,依赖 request_log。 +// ponytail: (ts+固定偏移)/86400 分组,DST 切换日边界误差 1 小时,趋势展示可接受 +func GetDailyRequestStats(days int) []DailyStat { + if cacheDB == nil || reqLogCh == nil || days <= 0 { + return nil + } + _, off := time.Now().Zone() + day := func(ts int64) int64 { return (ts + int64(off)) / 86400 } + cutoff := (day(time.Now().Unix()) - int64(days) + 1) * 86400 - int64(off) + + rows, err := cacheDB.Query(q( + "SELECT (ts + ?) / 86400, COUNT(*) FROM request_log WHERE ts >= ? GROUP BY 1 ORDER BY 1"), + int64(off), cutoff) + if err != nil { + cacheLogger.Warning("按天聚合请求量失败: " + err.Error()) + return nil + } + defer rows.Close() + + counts := make(map[int64]int64, days) + var maxCount int64 + for rows.Next() { + var d, c int64 + if err := rows.Scan(&d, &c); err != nil { + cacheLogger.Warning("读取按天统计失败: " + err.Error()) + break + } + counts[d] = c + if c > maxCount { + maxCount = c + } + } + + // 补齐无请求的日期,升序返回 + stats := make([]DailyStat, 0, days) + for i := int64(days) - 1; i >= 0; i-- { + d := day(time.Now().Unix()) - i + c := counts[d] + var pct int + if maxCount > 0 { + pct = int(c * 100 / maxCount) + } + stats = append(stats, DailyStat{ + Day: time.Unix(d*86400-int64(off), 0).Format("01-02"), + Count: c, + Pct: pct, + MaxCount: c > 0 && c == maxCount, + }) + } + return stats +} + // reqLogWorker 攒批落库 + 定期清理过期日志 func reqLogWorker() { defer close(reqLogDone) @@ -174,24 +274,21 @@ func reqLogWorker() { } } -// insertRequestLogs 事务批量写入 +// insertRequestLogs 多值 INSERT 一次往返写入整批(单条语句自带原子性) +// ponytail: 批量上限 50 行 × 9 列 = 450 参数,远低于 SQLite(999)/PG(65535) 占位符上限 func insertRequestLogs(batch []RequestLogEntry) error { - tx, err := cacheDB.Begin() - if err != nil { - return err - } - stmt, err := tx.Prepare(q("INSERT INTO request_log (ts, ip, method, path, query, status, duration_ms, error_msg) VALUES (?, ?, ?, ?, ?, ?, ?, ?)")) - if err != nil { - tx.Rollback() - return err - } - for _, e := range batch { - if _, err := stmt.Exec(e.Ts, e.IP, e.Method, e.Path, e.Query, e.Status, e.DurationMs, e.ErrorMsg); err != nil { - tx.Rollback() - return err + var b strings.Builder + b.WriteString("INSERT INTO request_log (ts, ip, method, path, query, status, duration_ms, error_msg, user_agent) VALUES ") + args := make([]interface{}, 0, len(batch)*9) + for i, e := range batch { + if i > 0 { + b.WriteByte(',') } + b.WriteString("(?, ?, ?, ?, ?, ?, ?, ?, ?)") + args = append(args, e.Ts, e.IP, e.Method, e.Path, e.Query, e.Status, e.DurationMs, e.ErrorMsg, e.UserAgent) } - return tx.Commit() + _, err := cacheDB.Exec(q(b.String()), args...) + return err } // cleanupRequestLog 删除超过保留期的日志 diff --git a/internal/handlers/common.go b/internal/handlers/common.go index 3d38992..6dc8da2 100644 --- a/internal/handlers/common.go +++ b/internal/handlers/common.go @@ -8,7 +8,6 @@ import ( "fmt" "net" "net/http" - "regexp" "strings" "sync" "sync/atomic" @@ -253,26 +252,38 @@ func LogRequestMiddleware(handler http.HandlerFunc) http.HandlerFunc { sw := &statusWriter{ResponseWriter: w, status: http.StatusOK} handler(sw, r) - query := sanitizeQuery(r.URL.RawQuery) + ua := r.UserAgent() + if len(ua) > 512 { // 防恶意超长 UA 撑爆日志 + ua = ua[:512] + } fqapi.LogRequest(fqapi.RequestLogEntry{ Ts: start.Unix(), IP: clientIP(r), Method: r.Method, Path: r.URL.Path, - Query: query, + Query: decodedQuery(r), Status: sw.status, DurationMs: time.Since(start).Milliseconds(), ErrorMsg: string(sw.errBuf), + UserAgent: ua, }) } } -// keyParamRe 匹配查询串中独立的 key 参数(accesskey 等不会被误伤) -var keyParamRe = regexp.MustCompile(`(?i)(^|&)key=[^&]*`) - -// sanitizeQuery 日志中脱敏:key 参数值替换为 ***(直接替换避免 Encode 把 *** 转义) -func sanitizeQuery(rawQuery string) string { - return keyParamRe.ReplaceAllString(rawQuery, "${1}key=***") +// decodedQuery 重组解码后的查询串(keyword=%E6%B5%8B → keyword=测),日志可读 +// ponytail: map 遍历乱序、%26 解码后与分隔符同名 —— 展示用途可接受 +func decodedQuery(r *http.Request) string { + vals := r.URL.Query() // 已解码 + if len(vals) == 0 { + return "" + } + parts := make([]string, 0, len(vals)) + for k, vs := range vals { + for _, v := range vs { + parts = append(parts, k+"="+v) + } + } + return strings.Join(parts, "&") } // HandleHealth 存活探针(供容器/反向代理使用) diff --git a/internal/handlers/status.go b/internal/handlers/status.go index b8d57b8..4134c93 100644 --- a/internal/handlers/status.go +++ b/internal/handlers/status.go @@ -68,9 +68,17 @@ func HandleStatus(w http.ResponseWriter, r *http.Request) { "Status": e.Status, "IsErr": e.Status >= 400, "Duration": fmt.Sprintf("%dms", e.DurationMs), + "UA": truncateStr(e.UserAgent, 60), }) } + // 近 7 天请求量(日志未启用时为空);最后一天即今日 + var todayCount int64 + daily := fqapi.GetDailyRequestStats(7) + if n := len(daily); n > 0 { + todayCount = daily[n-1].Count + } + statusTmpl.Execute(w, map[string]interface{}{ "NowTime": time.Now().Format("2006-01-02 15:04:05"), "Uptime": uptimeStr, @@ -94,6 +102,9 @@ func HandleStatus(w http.ResponseWriter, r *http.Request) { "StatAudio": stats["audio"], "StatVideo": stats["video"], "StatCategory": stats["category"], + "TodayCount": todayCount, + "DailyStats": daily, + "RateLimit": getRateLimitDesc(), "CacheCount": getDBCacheCount(), "CacheStatus": getCacheStatus(), "CleanupStatus": getCleanupStatus(), @@ -110,6 +121,15 @@ func truncateStr(s string, n int) string { return s[:n] + "…" } +// getRateLimitDesc 当前每 IP 限流描述(server.rate_limit_qps,0=不限制) +func getRateLimitDesc() string { + cfg := config.GetAppConfig() + if cfg == nil || cfg.Server.RateLimitQPS <= 0 { + return "未启用" + } + return fmt.Sprintf("%.0f QPS/IP", cfg.Server.RateLimitQPS) +} + // getCacheStatus 获取缓存状态 func getCacheStatus() string { cfg := config.GetConfig() diff --git a/internal/handlers/templates/status.html b/internal/handlers/templates/status.html index 3e8f99f..8fa99d2 100644 --- a/internal/handlers/templates/status.html +++ b/internal/handlers/templates/status.html @@ -186,6 +186,12 @@ .strack { height: 5px; border-radius: 999px; background: var(--panel-2); overflow: hidden; } .sfill { display: block; height: 100%; border-radius: inherit; background: var(--accent); width: 0; transition: width 0.7s cubic-bezier(0.22, 0.61, 0.36, 1); } .sval { font-size: 12px; font-weight: 600; color: var(--ink-2); text-align: right; } + /* 每日请求行:结构同 srow,宽度服务端算好,不参与 .srow 的 JS 归一化 */ + .drows { margin-top: 10px; padding-top: 10px; border-top: 1px dashed var(--border); display: flex; flex-direction: column; gap: 7px; } + .drow { display: grid; grid-template-columns: 44px 1fr 52px; gap: 8px; align-items: center; } + .dlabel { font-size: 11.5px; color: var(--ink-3); font-variant-numeric: tabular-nums; } + .dval { font-size: 12px; font-weight: 600; color: var(--ink-2); text-align: right; } + .drow.is-max .dlabel, .drow.is-max .dval { color: var(--accent); } /* ── 最近请求表 ─────────────────────── */ .req-card { margin-bottom: 14px; overflow: hidden; } @@ -472,6 +478,7 @@
签名算法{{.Signature}}
启动时间{{.StartTime}}
+
限流{{.RateLimit}}
@@ -513,7 +520,7 @@ 请求统计
{{.StatTotal}}
-
累计业务请求
+
累计业务请求 · 今日 {{.TodayCount}} 次
搜索{{.StatSearch}}
详情{{.StatDetail}}
@@ -524,6 +531,13 @@
视频{{.StatVideo}}
分类{{.StatCategory}}
+ {{if .DailyStats}} +
+ {{range .DailyStats}} +
{{.Day}}{{.Count}}
+ {{end}} +
+ {{end}}
@@ -553,6 +567,7 @@ 方法 路径 IP + UA 状态 耗时 @@ -564,6 +579,7 @@ {{.Method}} {{.Path}}{{if .Query}} ?{{.Query}}{{end}} {{.IP}} + {{.UA}} {{.Status}} {{.Duration}}