package middleware

import (
	"github.com/google/uuid"
	"github.com/labstack/echo/v4"
	"go.uber.org/zap"
)

const (
	// HeaderXRequestID is the canonical header name for request ID
	HeaderXRequestID = "X-Request-ID"
)

// RequestIDConfig defines the config for RequestID middleware.
type RequestIDConfig struct {
	// Skipper defines a function to skip middleware.
	Skipper Skipper

	// Generator defines a function to generate an ID.
	// Optional. Default value uuid.NewString().
	Generator func() string

	// RequestIDHandler defines a function which is executed when the request ID is attached to the context.
	// Optional.
	RequestIDHandler func(echo.Context, string)

	// TargetHeader defines what header to look for to use as request ID.
	// Optional. Default value "X-Request-ID".
	TargetHeader string

	// Logger to use for any middleware-specific logging (e.g., if ID generation fails)
	Logger *zap.Logger
}

// DefaultRequestIDConfig is the default RequestID middleware config.
var DefaultRequestIDConfig = RequestIDConfig{
	Skipper:      DefaultSkipper,
	Generator:    func() string { return uuid.NewString() },
	TargetHeader: HeaderXRequestID,
}

// RequestID returns a X-Request-ID middleware.
func RequestID() echo.MiddlewareFunc {
	return RequestIDWithConfig(DefaultRequestIDConfig)
}

// RequestIDWithConfig returns a X-Request-ID middleware with config.
// This middleware is inspired by Echo's built-in request_id middleware but adapted
// for potentially integrating a request-scoped logger.
func RequestIDWithConfig(config RequestIDConfig) echo.MiddlewareFunc {
	if config.Skipper == nil {
		config.Skipper = DefaultRequestIDConfig.Skipper
	}
	if config.Generator == nil {
		config.Generator = DefaultRequestIDConfig.Generator
	}
	if config.TargetHeader == "" {
		config.TargetHeader = DefaultRequestIDConfig.TargetHeader
	}

	return func(next echo.HandlerFunc) echo.HandlerFunc {
		return func(c echo.Context) error {
			if config.Skipper(c) {
				return next(c)
			}

			req := c.Request()
			res := c.Response()
			rid := req.Header.Get(config.TargetHeader)
			if rid == "" {
				rid = config.Generator()
			}
			res.Header().Set(config.TargetHeader, rid)
			c.Set(config.TargetHeader, rid) // Make it available in context for handlers

			// If a request ID handler is provided, call it
			if config.RequestIDHandler != nil {
				config.RequestIDHandler(c, rid)
			}

			// For true request-scoped logging with zap, where the logger itself carries the rid:
			// This would typically involve cloning the base logger and adding the rid as a field.
			// Example:
			// if config.Logger != nil {
			// requestLogger := config.Logger.With(zap.String("request_id", rid))
			// c.Set("logger", requestLogger) // Store request-scoped logger in context
			// }
			// Handlers would then retrieve this logger from context.
			// For now, we ensure the RID is available via c.Get(config.TargetHeader)
			// and the main request logger (echoMiddleware.RequestLoggerWithConfig) should be
			// configured to pick it up if possible.

			return next(c)
		}
	}
}




package middleware

import (
	"github.com/google/uuid"
	"github.com/labstack/echo/v4"
	"go.uber.org/zap"
)

const (
	// HeaderXRequestID is the canonical header name for request ID
	HeaderXRequestID = "X-Request-ID"
)

// RequestIDConfig defines the config for RequestID middleware.
type RequestIDConfig struct {
	// Skipper defines a function to skip middleware.
	Skipper Skipper

	// Generator defines a function to generate an ID.
	// Optional. Default value uuid.NewString().
	Generator func() string

	// RequestIDHandler defines a function which is executed when the request ID is attached to the context.
	// Optional.
	RequestIDHandler func(echo.Context, string)

	// TargetHeader defines what header to look for to use as request ID.
	// Optional. Default value "X-Request-ID".
	TargetHeader string

	// Logger to use for any middleware-specific logging (e.g., if ID generation fails)
	Logger *zap.Logger
}

// DefaultRequestIDConfig is the default RequestID middleware config.
var DefaultRequestIDConfig = RequestIDConfig{
	Skipper:      DefaultSkipper,
	Generator:    func() string { return uuid.NewString() },
	TargetHeader: HeaderXRequestID,
}

// RequestID returns a X-Request-ID middleware.
func RequestID() echo.MiddlewareFunc {
	return RequestIDWithConfig(DefaultRequestIDConfig)
}

// RequestIDWithConfig returns a X-Request-ID middleware with config.
// This middleware is inspired by Echo's built-in request_id middleware but adapted
// for potentially integrating a request-scoped logger.
func RequestIDWithConfig(config RequestIDConfig) echo.MiddlewareFunc {
	if config.Skipper == nil {
		config.Skipper = DefaultRequestIDConfig.Skipper
	}
	if config.Generator == nil {
		config.Generator = DefaultRequestIDConfig.Generator
	}
	if config.TargetHeader == "" {
		config.TargetHeader = DefaultRequestIDConfig.TargetHeader
	}

	return func(next echo.HandlerFunc) echo.HandlerFunc {
		return func(c echo.Context) error {
			if config.Skipper(c) {
				return next(c)
			}

			req := c.Request()
			res := c.Response()
			rid := req.Header.Get(config.TargetHeader)
			if rid == "" {
				rid = config.Generator()
			}
			res.Header().Set(config.TargetHeader, rid)
			c.Set(config.TargetHeader, rid) // Make it available in context for handlers

			// If a request ID handler is provided, call it
			if config.RequestIDHandler != nil {
				config.RequestIDHandler(c, rid)
			}

			// For true request-scoped logging with zap, where the logger itself carries the rid:
			// This would typically involve cloning the base logger and adding the rid as a field.
			// Example:
			// if config.Logger != nil {
			// requestLogger := config.Logger.With(zap.String("request_id", rid))
			// c.Set("logger", requestLogger) // Store request-scoped logger in context
			// }
			// Handlers would then retrieve this logger from context.
			// For now, we ensure the RID is available via c.Get(config.TargetHeader)
			// and the main request logger (echoMiddleware.RequestLoggerWithConfig) should be
			// configured to pick it up if possible.

			return next(c)
		}
	}
}


修复此文件中的错误

package middleware

import (
	"github.com/google/uuid"
	"github.com/labstack/echo/v4"
	"go.uber.org/zap"
)

const (
	// HeaderXRequestID is the canonical header name for request ID
	HeaderXRequestID = "X-Request-ID"
)

// RequestIDConfig defines the config for RequestID middleware.
type RequestIDConfig struct {
	// Skipper defines a function to skip middleware.
	Skipper Skipper

	// Generator defines a function to generate an ID.
	// Optional. Default value uuid.NewString().
	Generator func() string

	// RequestIDHandler defines a function which is executed when the request ID is attached to the context.
	// Optional.
	RequestIDHandler func(echo.Context, string)

	// TargetHeader defines what header to look for to use as request ID.
	// Optional. Default value "X-Request-ID".
	TargetHeader string

	// Logger to use for any middleware-specific logging (e.g., if ID generation fails)
	Logger *zap.Logger
}

// DefaultRequestIDConfig is the default RequestID middleware config.
var DefaultRequestIDConfig = RequestIDConfig{
	Skipper:      DefaultSkipper,
	Generator:    func() string { return uuid.NewString() },
	TargetHeader: HeaderXRequestID,
}

// RequestID returns a X-Request-ID middleware.
func RequestID() echo.MiddlewareFunc {
	return RequestIDWithConfig(DefaultRequestIDConfig)
}

// RequestIDWithConfig returns a X-Request-ID middleware with config.
// This middleware is inspired by Echo's built-in request_id middleware but adapted
// for potentially integrating a request-scoped logger.
func RequestIDWithConfig(config RequestIDConfig) echo.MiddlewareFunc {
	if config.Skipper == nil {
		config.Skipper = DefaultRequestIDConfig.Skipper
	}
	if config.Generator == nil {
		config.Generator = DefaultRequestIDConfig.Generator
	}
	if config.TargetHeader == "" {
		config.TargetHeader = DefaultRequestIDConfig.TargetHeader
	}

	return func(next echo.HandlerFunc) echo.HandlerFunc {
		return func(c echo.Context) error {
			if config.Skipper(c) {
				return next(c)
			}

			req := c.Request()
			res := c.Response()
			rid := req.Header.Get(config.TargetHeader)
			if rid == "" {
				rid = config.Generator()
			}
			res.Header().Set(config.TargetHeader, rid)
			c.Set(config.TargetHeader, rid) // Make it available in context for handlers

			// If a request ID handler is provided, call it
			if config.RequestIDHandler != nil {
				config.RequestIDHandler(c, rid)
			}

			// For true request-scoped logging with zap, where the logger itself carries the rid:
			// This would typically involve cloning the base logger and adding the rid as a field.
			// Example:
			// if config.Logger != nil {
			// requestLogger := config.Logger.With(zap.String("request_id", rid))
			// c.Set("logger", requestLogger) // Store request-scoped logger in context
			// }
			// Handlers would then retrieve this logger from context.
			// For now, we ensure the RID is available via c.Get(config.TargetHeader)
			// and the main request logger (echoMiddleware.RequestLoggerWithConfig) should be
			// configured to pick it up if possible.

			return next(c)
		}
	}
}