middleware

package
v0.0.19 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package middleware provides various middleware functions for HTTP handlers.

Package middleware provides various middleware functions for HTTP handlers.

Package middleware provides various middleware functions for HTTP handlers.

Index

Constants

View Source
const (
	RequestIDKey    contextKey = "request_id"
	XTestTimeoutKey contextKey = "X-Test-Timeout"
)

Variables

This section is empty.

Functions

func Authentication added in v0.0.6

func Authentication(next http.Handler) http.Handler

Authentication middleware validates API keys and manages authentication

func CORS

func CORS(next http.Handler) http.Handler

CORS handles Cross-Origin Resource Sharing It allows or denies requests from different origins based on the configuration.

func Logging added in v0.0.7

func Logging(logger *zap.Logger) func(http.Handler) http.Handler

Logging middleware logs request and response details

func PanicRecovery

func PanicRecovery(next http.Handler) http.Handler

PanicRecovery recovers from panics and returns a 500 error

func PrometheusMetrics added in v0.0.9

func PrometheusMetrics(m *metrics.Metrics) func(next http.Handler) http.Handler

PrometheusMetrics middleware records HTTP metrics using Prometheus. It wraps the HTTP handler to measure request duration and active requests. It takes a Metrics object as an argument to track metrics.

func RateLimit added in v0.0.6

func RateLimit(metrics *metrics.Metrics) func(http.Handler) http.Handler

RateLimit creates a new rate limit middleware that applies rate limiting to incoming requests and tracks metrics.

func Recovery added in v0.0.7

func Recovery(logger *zap.Logger) func(http.Handler) http.Handler

Recovery middleware recovers from panics and logs the error It takes a zap.Logger instance for logging errors.

func RequestID

func RequestID(next http.Handler) http.Handler

RequestID middleware adds a unique request ID to the context and sets it in the response header.

func RequestTimer

func RequestTimer(next http.Handler) http.Handler

RequestTimer measures request processing time It wraps the HTTP handler to calculate the duration of the request and sets the X-Response-Time header in the response.

func ResetRateLimiters added in v0.0.7

func ResetRateLimiters()

ResetRateLimiters resets all rate limiters. Only used for testing.

func Timeout added in v0.0.7

func Timeout(timeout time.Duration) func(http.Handler) http.Handler

Timeout middleware adds a timeout to the request context It allows you to specify a duration after which the request will be aborted if not completed.

The Timeout middleware works by creating a new context with a timeout, and using a custom timeoutWriter to track whether a response has been written. If the request times out and no response has been written, it sends a timeout error response.

Types

type ResponseWriter added in v0.0.7

type ResponseWriter struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

ResponseWriter wraps http.ResponseWriter to capture status code and size

func NewResponseWriter added in v0.0.7

func NewResponseWriter(w http.ResponseWriter) *ResponseWriter

NewResponseWriter creates a new ResponseWriter

func (*ResponseWriter) Size added in v0.0.7

func (w *ResponseWriter) Size() int64

Size returns the response size

func (*ResponseWriter) Status added in v0.0.7

func (w *ResponseWriter) Status() int

Status returns the status code

func (*ResponseWriter) Write added in v0.0.7

func (w *ResponseWriter) Write(b []byte) (int, error)

func (*ResponseWriter) WriteHeader added in v0.0.7

func (w *ResponseWriter) WriteHeader(status int)

Jump to

Keyboard shortcuts

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