Files
lijiaoqiao/supply-api/internal/middleware/middleware.go
Your Name 8ac23bf7d4 test: improve coverage and fix sanitizer bug
- Fix MaskMap to properly handle []string sensitive fields
- Add missing slice handling in sanitizer
- Add comprehensive tests for GetMetrics and CreateEventsBatch
- Improve audit/handler coverage from 49.8% to 68.8%
- Fix test expectations to match actual sanitizer behavior
- All tests pass
2026-04-08 07:44:58 +08:00

65 lines
1.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package middleware
import (
"log"
"net/http"
"runtime/debug"
"lijiaoqiao/supply-api/internal/pkg/logging"
)
// Recovery 中间件 - 恢复 panic
func Recovery(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
log.Printf("panic recovered: %v\n%s", err, debug.Stack())
http.Error(w, "internal server error", http.StatusInternalServerError)
}
}()
next.ServeHTTP(w, r)
})
}
// Logging 中间件 - 请求日志使用结构化JSON日志
// P1-010修复: 使用结构化日志替代标准log
func Logging(next http.Handler, logger logging.Logger) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// 从context获取追踪信息
fields := make(map[string]interface{})
fields["method"] = r.Method
fields["path"] = r.URL.Path
fields["query"] = r.URL.RawQuery
// 尝试获取request_id
if requestID := r.Header.Get("X-Request-Id"); requestID != "" {
fields["request_id"] = requestID
} else if requestID := r.Header.Get("X-Request-ID"); requestID != "" {
fields["request_id"] = requestID
}
// 尝试获取trace_id
if tc, ok := GetTraceContext(r.Context()); ok {
fields["trace_id"] = tc.TraceID
fields["span_id"] = tc.SpanID
}
logger.Info("HTTP request", fields)
next.ServeHTTP(w, r)
})
}
// RequestID 中间件 - 请求追踪
func RequestID(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestID := r.Header.Get("X-Request-Id")
if requestID == "" {
requestID = r.Header.Get("X-Request-ID")
}
if requestID != "" {
w.Header().Set("X-Request-Id", requestID)
}
next.ServeHTTP(w, r)
})
}