middleware

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2025 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrTimeoutWrite = errors.New("zerohttp: timeout middleware write failed")

ErrTimeoutWrite is returned when the timeout middleware fails to write response data.

Functions

func BasicAuth

func BasicAuth(opts ...config.BasicAuthOption) func(http.Handler) http.Handler

BasicAuth creates a basic authentication middleware with optional configuration

func CORS

func CORS(opts ...config.CORSOption) func(http.Handler) http.Handler

CORS middleware handles Cross-Origin Resource Sharing

func CircuitBreaker

func CircuitBreaker(opts ...config.CircuitBreakerOption) func(http.Handler) http.Handler

CircuitBreaker creates a circuit breaker middleware

func Compress

func Compress(opts ...config.CompressOption) func(http.Handler) http.Handler

Compress creates a compression middleware using the full config

func ContentCharset

func ContentCharset(opts ...config.ContentCharsetOption) func(http.Handler) http.Handler

ContentCharset generates a middleware that validates request charset and returns 415 Unsupported Media Type if the charset doesn't match the allowed list

func ContentEncoding

func ContentEncoding(opts ...config.ContentEncodingOption) func(http.Handler) http.Handler

ContentEncoding enforces a whitelist of request Content-Encoding

func ContentType

func ContentType(opts ...config.ContentTypeOption) func(http.Handler) http.Handler

ContentType enforces a whitelist of request Content-Types otherwise responds with a 415 Unsupported Media Type status.

func DefaultMiddlewares

func DefaultMiddlewares(cfg config.Config, logger log.Logger) []func(http.Handler) http.Handler

func GetContextValue

func GetContextValue[T any](r *http.Request, key any) (T, bool)

GetContextValue retrieves a typed value from the request context. Returns the value and true if found and correctly typed, zero value and false otherwise.

func GetRequestID

func GetRequestID(ctx context.Context, key ...config.RequestIDContextKey) string

GetRequestID retrieves the request ID from context using the specified key If no key is provided, uses the default key

func LogRequest

func LogRequest(logger log.Logger, cfg config.RequestLoggerConfig, r *http.Request, statusCode int, duration time.Duration)

LogRequest logs an HTTP request with consistent formatting.

func NoCache

func NoCache(opts ...config.NoCacheOption) func(http.Handler) http.Handler

NoCache middleware sets headers on every response to prevent caching and deletes ETag headers.

func RateLimit

func RateLimit(opts ...config.RateLimitOption) func(http.Handler) http.Handler

RateLimit creates a rate limiting middleware

func RealIP

func RealIP(opts ...config.RealIPOption) func(http.Handler) http.Handler

RealIP middleware sets r.RemoteAddr to the extracted real client IP.

func Recover

func Recover(logger log.Logger, opts ...config.RecoverOption) func(http.Handler) http.Handler

Recover is a middleware that recovers from panics, logs the panic (and a backtrace), and returns HTTP 500 if possible. It prints a request ID if one is provided.

func RequestBodySize

func RequestBodySize(opts ...config.RequestBodySizeOption) func(http.Handler) http.Handler

RequestBodySize creates a request size limiting middleware with optional configuration

func RequestID

func RequestID(opts ...config.RequestIDOption) func(http.Handler) http.Handler

RequestID creates a request ID middleware with optional configuration

func RequestLogger

func RequestLogger(logger log.Logger, opts ...config.RequestLoggerOption) func(http.Handler) http.Handler

RequestLogger creates a request logging middleware with optional configuration.

func SecurityHeaders

func SecurityHeaders(opts ...config.SecurityHeadersOption) func(http.Handler) http.Handler

SecurityHeaders creates a security headers middleware that adds various security-related HTTP headers

func SetHeader

func SetHeader(opts ...config.SetHeaderOption) func(http.Handler) http.Handler

SetHeader is a middleware that sets response headers

func Timeout

func Timeout(opts ...config.TimeoutOption) func(http.Handler) http.Handler

Timeout is a middleware that enforces request timeouts by canceling the context after a specified duration. When the timeout is exceeded, it returns an HTTP 504 Gateway Timeout response to the client.

Important: Your handler must monitor the ctx.Done() channel to detect when the context deadline has been reached. If you don't check this channel and return appropriately, the timeout mechanism will be ineffective and the request will continue processing beyond the intended timeout period.

func TrailingSlash

func TrailingSlash(opts ...config.TrailingSlashOption) func(http.Handler) http.Handler

TrailingSlash is a middleware that handles trailing slashes in URLs

func WithValue

func WithValue(key, val any) func(next http.Handler) http.Handler

WithValue sets a key/value pair in the request context for downstream handlers.

Types

type Bucket

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

Bucket represents a token bucket for rate limiting

type CircuitState

type CircuitState int

CircuitState represents the state of the circuit breaker

const (
	StateClosed CircuitState = iota
	StateOpen
	StateHalfOpen
)

type Compressor

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

Compressor represents a set of encoding configurations.

func NewCompressor

func NewCompressor(level int, types ...string) *Compressor

NewCompressor creates a new Compressor that will handle encoding responses.

func (*Compressor) Handler

func (c *Compressor) Handler(next http.Handler) http.Handler

Handler returns a new middleware that will compress the response.

func (*Compressor) SetEncoder

func (c *Compressor) SetEncoder(encoding string, fn EncoderFunc)

SetEncoder can be used to set the implementation of a compression algorithm.

type EncoderFunc

type EncoderFunc func(w io.Writer, level int) io.Writer

EncoderFunc is a function that wraps the provided io.Writer with compression.

type WindowCounter

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

WindowCounter represents a counter for window-based rate limiting

Jump to

Keyboard shortcuts

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