middleware

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 21, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package middleware provides common HTTP middleware for Go web applications.

Index

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 Logging

func Logging(logger zerolog.Logger) func(http.Handler) http.Handler

Logging returns a middleware that logs HTTP requests using zerolog.

func LoggingFunc

func LoggingFunc(logger zerolog.Logger) func(http.Handler) http.Handler

LoggingFunc returns a middleware function for use with gorilla/mux.

func Metrics

func Metrics() func(http.Handler) http.Handler

Metrics returns a middleware that collects Prometheus metrics.

func MetricsSimple

func MetricsSimple() func(http.Handler) http.Handler

MetricsSimple returns a simpler metrics middleware that only counts requests. This is useful when you don't need detailed path-level metrics.

func Recovery

func Recovery() func(http.Handler) http.Handler

Recovery returns a middleware that recovers from panics and logs the error.

func RecoveryWithLogger

func RecoveryWithLogger(logger zerolog.Logger) func(http.Handler) http.Handler

RecoveryWithLogger returns a middleware that recovers from panics using a custom logger.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL