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
- func Authentication(next http.Handler) http.Handler
- func CORS(next http.Handler) http.Handler
- func Logging(logger *zap.Logger) func(http.Handler) http.Handler
- func PanicRecovery(next http.Handler) http.Handler
- func PrometheusMetrics(m *metrics.Metrics) func(next http.Handler) http.Handler
- func RateLimit(metrics *metrics.Metrics) func(http.Handler) http.Handler
- func Recovery(logger *zap.Logger) func(http.Handler) http.Handler
- func RequestID(next http.Handler) http.Handler
- func RequestTimer(next http.Handler) http.Handler
- func ResetRateLimiters()
- func Timeout(timeout time.Duration) func(http.Handler) http.Handler
- type QueueConfig
- type QueueMiddleware
- func (qm *QueueMiddleware) GetMaxSize() int64
- func (qm *QueueMiddleware) GetProcessing() int32
- func (qm *QueueMiddleware) GetQueueSize() int
- func (qm *QueueMiddleware) Handler(next http.Handler) http.Handler
- func (qm *QueueMiddleware) SetMaxSize(size int64)
- func (qm *QueueMiddleware) Shutdown(ctx context.Context) error
- type QueueState
- type ResponseWriter
Constants ¶
const ( RequestIDKey contextKey = "request_id" XTestTimeoutKey contextKey = "X-Test-Timeout" )
Variables ¶
This section is empty.
Functions ¶
func Authentication ¶ added in v0.0.6
Authentication middleware validates API keys and manages authentication
func CORS ¶
CORS handles Cross-Origin Resource Sharing It allows or denies requests from different origins based on the configuration.
func PanicRecovery ¶
PanicRecovery recovers from panics and returns a 500 error
func PrometheusMetrics ¶ added in v0.0.9
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
RateLimit creates a new rate limit middleware that applies rate limiting to incoming requests and tracks metrics.
func Recovery ¶ added in v0.0.7
Recovery middleware recovers from panics and logs the error It takes a zap.Logger instance for logging errors.
func RequestID ¶
RequestID middleware adds a unique request ID to the context and sets it in the response header.
func RequestTimer ¶
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
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 QueueConfig ¶ added in v0.0.22
type QueueMiddleware ¶ added in v0.0.22
type QueueMiddleware struct {
// contains filtered or unexported fields
}
func NewQueueMiddleware ¶ added in v0.0.22
func NewQueueMiddleware(cfg QueueConfig) *QueueMiddleware
func (*QueueMiddleware) GetMaxSize ¶ added in v0.0.22
func (qm *QueueMiddleware) GetMaxSize() int64
func (*QueueMiddleware) GetProcessing ¶ added in v0.0.22
func (qm *QueueMiddleware) GetProcessing() int32
func (*QueueMiddleware) GetQueueSize ¶ added in v0.0.22
func (qm *QueueMiddleware) GetQueueSize() int
func (*QueueMiddleware) Handler ¶ added in v0.0.22
func (qm *QueueMiddleware) Handler(next http.Handler) http.Handler
func (*QueueMiddleware) SetMaxSize ¶ added in v0.0.22
func (qm *QueueMiddleware) SetMaxSize(size int64)
type QueueState ¶ added in v0.0.22
type QueueState struct {
MaxSize int64 `json:"max_size"`
QueueLength int `json:"queue_length"`
LastSaved time.Time `json:"last_saved"`
}
QueueState represents the persistent state of the queue
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)