调整工厂模式的方法

This commit is contained in:
2025-12-05 00:07:15 +08:00
parent 0650feb0d2
commit 6146178111
15 changed files with 635 additions and 1039 deletions

View File

@@ -123,7 +123,7 @@ func shouldSkipPath(path string, skipPaths []string) bool {
// logHTTPRequest 记录HTTP请求日志
func logHTTPRequest(log *logger.Logger, r *http.Request, rw *responseWriter, duration time.Duration) {
// 获取客户端IP
clientIP := getClientIP(r)
clientIP := GetClientIP(r)
// 构建日志字段
fields := map[string]interface{}{
@@ -188,9 +188,9 @@ func formatValue(v interface{}) string {
}
}
// getClientIP 获取客户端真实IP
// GetClientIP 获取客户端真实IP
// 优先级X-Forwarded-For > X-Real-IP > RemoteAddr
func getClientIP(r *http.Request) string {
func GetClientIP(r *http.Request) string {
// 尝试从 X-Forwarded-For 获取
xff := r.Header.Get("X-Forwarded-For")
if xff != "" {

View File

@@ -169,7 +169,7 @@ func RateLimit(config *RateLimitConfig) func(http.Handler) http.Handler {
// 如果没有提供KeyFunc使用默认的客户端IP
if config.KeyFunc == nil {
config.KeyFunc = func(r *http.Request) string {
return getClientIP(r)
return GetClientIP(r)
}
}
@@ -223,7 +223,7 @@ func RateLimitByIP(rate int, windowSize time.Duration) func(http.Handler) http.H
return RateLimit(&RateLimitConfig{
Limiter: NewTokenBucketLimiter(rate, windowSize),
KeyFunc: func(r *http.Request) string {
return getClientIP(r)
return GetClientIP(r)
},
})
}

View File

@@ -109,7 +109,7 @@ func logPanic(log *logger.Logger, r *http.Request, err interface{}, enableStackT
"method": r.Method,
"path": r.URL.Path,
"query": r.URL.RawQuery,
"ip": getClientIP(r),
"ip": GetClientIP(r),
"error": fmt.Sprintf("%v", err),
}