调整工厂模式的方法
This commit is contained in:
@@ -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 != "" {
|
||||
|
||||
@@ -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)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user