Documentation
¶
Index ¶
- Variables
- func BasicAuth(opts ...config.BasicAuthOption) func(http.Handler) http.Handler
- func CORS(opts ...config.CORSOption) func(http.Handler) http.Handler
- func CircuitBreaker(opts ...config.CircuitBreakerOption) func(http.Handler) http.Handler
- func Compress(opts ...config.CompressOption) func(http.Handler) http.Handler
- func ContentCharset(opts ...config.ContentCharsetOption) func(http.Handler) http.Handler
- func ContentEncoding(opts ...config.ContentEncodingOption) func(http.Handler) http.Handler
- func ContentType(opts ...config.ContentTypeOption) func(http.Handler) http.Handler
- func DefaultMiddlewares(cfg config.Config, logger log.Logger) []func(http.Handler) http.Handler
- func GetContextValue[T any](r *http.Request, key any) (T, bool)
- func GetRequestID(ctx context.Context, key ...config.RequestIDContextKey) string
- func LogRequest(logger log.Logger, cfg config.RequestLoggerConfig, r *http.Request, ...)
- func NoCache(opts ...config.NoCacheOption) func(http.Handler) http.Handler
- func RateLimit(opts ...config.RateLimitOption) func(http.Handler) http.Handler
- func RealIP(opts ...config.RealIPOption) func(http.Handler) http.Handler
- func Recover(logger log.Logger, opts ...config.RecoverOption) func(http.Handler) http.Handler
- func RequestBodySize(opts ...config.RequestBodySizeOption) func(http.Handler) http.Handler
- func RequestID(opts ...config.RequestIDOption) func(http.Handler) http.Handler
- func RequestLogger(logger log.Logger, opts ...config.RequestLoggerOption) func(http.Handler) http.Handler
- func SecurityHeaders(opts ...config.SecurityHeadersOption) func(http.Handler) http.Handler
- func SetHeader(opts ...config.SetHeaderOption) func(http.Handler) http.Handler
- func Timeout(opts ...config.TimeoutOption) func(http.Handler) http.Handler
- func TrailingSlash(opts ...config.TrailingSlashOption) func(http.Handler) http.Handler
- func WithValue(key, val any) func(next http.Handler) http.Handler
- type Bucket
- type CircuitState
- type Compressor
- type EncoderFunc
- type WindowCounter
Constants ¶
This section is empty.
Variables ¶
var ErrTimeoutWrite = errors.New("zerohttp: timeout middleware write failed")
ErrTimeoutWrite is returned when the timeout middleware fails to write response data.
Functions ¶
func CircuitBreaker ¶
CircuitBreaker creates a circuit breaker middleware
func ContentCharset ¶
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 ¶
ContentEncoding enforces a whitelist of request Content-Encoding
func ContentType ¶
ContentType enforces a whitelist of request Content-Types otherwise responds with a 415 Unsupported Media Type status.
func DefaultMiddlewares ¶
func GetContextValue ¶
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 ¶
NoCache middleware sets headers on every response to prevent caching and deletes ETag headers.
func Recover ¶
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 ¶
RequestBodySize creates a request size limiting 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 ¶
SecurityHeaders creates a security headers middleware that adds various security-related HTTP headers
func Timeout ¶
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 ¶
TrailingSlash is a middleware that handles trailing slashes in URLs
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 ¶
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