Documentation
¶
Overview ¶
Package middleware provides common HTTP middleware for Go web applications.
Index ¶
- Variables
- func CORS(cfg *CORSConfig) func(http.Handler) http.Handler
- func Logging(logger zerolog.Logger) func(http.Handler) http.Handler
- func LoggingFunc(logger zerolog.Logger) func(http.Handler) http.Handler
- func Metrics() func(http.Handler) http.Handler
- func MetricsSimple() func(http.Handler) http.Handler
- func Recovery() func(http.Handler) http.Handler
- func RecoveryWithLogger(logger zerolog.Logger) func(http.Handler) http.Handler
- type CORSConfig
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // HTTPRequestsTotal counts total HTTP requests. HTTPRequestsTotal = promauto.NewCounterVec( prometheus.CounterOpts{ Name: "http_requests_total", Help: "Total number of HTTP requests", }, []string{"method", "path", "status"}, ) // HTTPRequestDuration tracks HTTP request duration. HTTPRequestDuration = promauto.NewHistogramVec( prometheus.HistogramOpts{ Name: "http_request_duration_seconds", Help: "HTTP request duration in seconds", Buckets: prometheus.DefBuckets, }, []string{"method", "path"}, ) // HTTPRequestsInFlight tracks the number of in-flight requests. HTTPRequestsInFlight = promauto.NewGauge( prometheus.GaugeOpts{ Name: "http_requests_in_flight", Help: "Number of HTTP requests currently being processed", }, ) )
Functions ¶
func CORS ¶
func CORS(cfg *CORSConfig) func(http.Handler) http.Handler
CORS returns a middleware that handles Cross-Origin Resource Sharing.
func LoggingFunc ¶
LoggingFunc returns a middleware function for use with gorilla/mux.
func MetricsSimple ¶
MetricsSimple returns a simpler metrics middleware that only counts requests. This is useful when you don't need detailed path-level metrics.
Types ¶
type CORSConfig ¶
type CORSConfig struct {
// AllowedOrigins is a list of allowed origins. Use "*" for all origins.
AllowedOrigins []string
// AllowedMethods is a list of allowed HTTP methods.
AllowedMethods []string
// AllowedHeaders is a list of allowed headers.
AllowedHeaders []string
// AllowCredentials indicates whether credentials are allowed.
AllowCredentials bool
// MaxAge is the max age for preflight cache in seconds.
MaxAge int
}
CORSConfig holds CORS middleware configuration.
func DefaultCORSConfig ¶
func DefaultCORSConfig() *CORSConfig
DefaultCORSConfig returns a permissive CORS configuration suitable for development.
Click to show internal directories.
Click to hide internal directories.