middleware

package
v0.0.0-...-1b0f67f Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2025 License: AGPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	APIVersionKey contextKey = "api_version"
)

Context key for API version

View Source
const (
	RequestIDKey contextKey = "request_id"
)

Variables

This section is empty.

Functions

func CORS

func CORS() func(http.Handler) http.Handler

CORS returns a middleware that handles Cross-Origin Resource Sharing (CORS) headers

func Compression

func Compression() func(http.Handler) http.Handler

Compression returns a middleware that adds gzip compression for large responses

func CompressionWithConfig

func CompressionWithConfig(config CompressionConfig) func(http.Handler) http.Handler

CompressionWithConfig returns a compression middleware with custom configuration

func GetAPIVersionFromContext

func GetAPIVersionFromContext(r *http.Request) string

GetAPIVersionFromContext gets the API version from request context

func GetMetricsHandler

func GetMetricsHandler() http.Handler

GetMetricsHandler returns the Prometheus metrics handler

func GetRequestIDFromContext

func GetRequestIDFromContext(r *http.Request) string

GetRequestIDFromContext gets the request ID from request context

func GetRequestIDFromContextWithKey

func GetRequestIDFromContextWithKey(r *http.Request, key contextKey) string

GetRequestIDFromContextWithKey gets the request ID from request context using a custom key

func Logging

func Logging() func(http.Handler) http.Handler

Logging returns a middleware that logs HTTP requests with structured logging

func LoggingWithConfig

func LoggingWithConfig(config LoggingConfig) func(http.Handler) http.Handler

LoggingWithConfig returns a logging middleware with custom configuration

func Metrics

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

Metrics returns a middleware that collects HTTP metrics

func MetricsWithConfig

func MetricsWithConfig(config MetricsConfig) func(http.Handler) http.Handler

MetricsWithConfig returns a metrics middleware with custom configuration

func RecordCustomMetric

func RecordCustomMetric(name string, value float64, labels map[string]string)

RecordCustomMetric records a custom metric with labels

func RegisterCustomMetrics

func RegisterCustomMetrics(collectors ...prometheus.Collector) error

RegisterCustomMetrics allows registration of custom metrics

func RequestID

func RequestID() func(http.Handler) http.Handler

RequestID returns a middleware that adds request ID tracking for debugging and tracing

func RequestIDWithConfig

func RequestIDWithConfig(config RequestIDConfig) func(http.Handler) http.Handler

RequestIDWithConfig returns a request ID middleware with custom configuration

func Sentry

func Sentry() func(http.Handler) http.Handler

Sentry returns a middleware that adds Sentry error tracking to HTTP requests

func Versioning

func Versioning() func(http.Handler) http.Handler

Versioning returns a middleware that handles API version negotiation

func VersioningWithConfig

func VersioningWithConfig(config VersioningConfig) func(http.Handler) http.Handler

VersioningWithConfig returns a versioning middleware with custom configuration

Types

type CompressionConfig

type CompressionConfig struct {
	Level             int      `json:"level"`              // Compression level (1-9)
	MinLength         int      `json:"min_length"`         // Minimum response length to compress
	CompressiblePaths []string `json:"compressible_paths"` // Paths that should be compressed
	CompressibleTypes []string `json:"compressible_types"` // Content types that should be compressed
	ExcludedPaths     []string `json:"excluded_paths"`     // Paths that should not be compressed
	ExcludedTypes     []string `json:"excluded_types"`     // Content types that should not be compressed
}

CompressionConfig represents compression middleware configuration

func DefaultCompressionConfig

func DefaultCompressionConfig() CompressionConfig

DefaultCompressionConfig returns a default compression configuration

type LogEntry

type LogEntry struct {
	RequestID   string        `json:"request_id"`
	Method      string        `json:"method"`
	Path        string        `json:"path"`
	StatusCode  int           `json:"status_code"`
	Duration    time.Duration `json:"duration"`
	UserAgent   string        `json:"user_agent"`
	RemoteAddr  string        `json:"remote_addr"`
	ContentType string        `json:"content_type"`
	QueryParams string        `json:"query_params"`
	Timestamp   time.Time     `json:"timestamp"`
}

LogEntry represents a log entry for HTTP requests

type LoggingConfig

type LoggingConfig struct {
	StructuredLogging bool           `json:"structured_logging"`
	ColoredOutput     bool           `json:"colored_output"`
	LogErrors         bool           `json:"log_errors"`
	SkipPaths         []string       `json:"skip_paths"`
	CustomLogger      func(LogEntry) `json:"-"`
}

LoggingConfig represents logging middleware configuration

func DefaultLoggingConfig

func DefaultLoggingConfig() LoggingConfig

DefaultLoggingConfig returns a default logging configuration

type MetricsConfig

type MetricsConfig struct {
	SkipPaths             []string          `json:"skip_paths"`
	EndpointNormalization map[string]string `json:"endpoint_normalization"`
	CustomLabels          map[string]string `json:"custom_labels"`
}

MetricsConfig represents metrics middleware configuration

func DefaultMetricsConfig

func DefaultMetricsConfig() MetricsConfig

DefaultMetricsConfig returns a default metrics configuration

type RequestIDConfig

type RequestIDConfig struct {
	AllowClientRequestID bool              `json:"allow_client_request_id"`
	RequestIDHeader      string            `json:"request_id_header"`
	ResponseIDHeader     string            `json:"response_id_header"`
	ContextKey           contextKey        `json:"-"`
	Generator            func() string     `json:"-"`
	Validator            func(string) bool `json:"-"`
}

RequestIDConfig represents request ID middleware configuration

func DefaultRequestIDConfig

func DefaultRequestIDConfig() RequestIDConfig

DefaultRequestIDConfig returns a default request ID configuration

type VersionRouter

type VersionRouter struct {
	// contains filtered or unexported fields
}

VersionRouter routes requests to different handlers based on API version

func NewVersionRouter

func NewVersionRouter() *VersionRouter

NewVersionRouter creates a new version router

func (*VersionRouter) AddHandler

func (vr *VersionRouter) AddHandler(version string, handler http.HandlerFunc)

AddHandler adds a handler for a specific version

func (*VersionRouter) ServeHTTP

func (vr *VersionRouter) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler interface

func (*VersionRouter) SetFallback

func (vr *VersionRouter) SetFallback(handler http.HandlerFunc)

SetFallback sets the fallback handler for unsupported versions

type VersionedHandler

type VersionedHandler struct {
	Version string
	Handler http.HandlerFunc
}

VersionedHandler represents a handler for a specific API version

type VersioningConfig

type VersioningConfig struct {
	DefaultVersion      string   `json:"default_version"`
	SupportedVersions   []string `json:"supported_versions"`
	DeprecatedVersions  []string `json:"deprecated_versions"`
	HeaderName          string   `json:"header_name"`
	AcceptHeaderPattern string   `json:"accept_header_pattern"`
}

VersioningConfig represents versioning middleware configuration

func DefaultVersioningConfig

func DefaultVersioningConfig() VersioningConfig

DefaultVersioningConfig returns a default versioning configuration

Jump to

Keyboard shortcuts

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