Documentation
¶
Overview ¶
Package middleware provides HTTP middleware utilities for wrapping http.HandlerFunc with additional functionality such as logging, metrics, and response inspection.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Middleware ¶
type Middleware func(http.HandlerFunc) http.HandlerFunc
Middleware is a function that takes an http.HandlerFunc and returns a new http.HandlerFunc which may execute code before and/or after calling the original handler. It is commonly used for cross-cutting concerns such as logging, authentication, and metrics.
func Logger ¶
func Logger(logger *slog.Logger) Middleware
Logger creates a middleware that logs information about HTTP requests. It logs the request method, path, status code, and response time.
func MetricCollector ¶
func MetricCollector() Middleware
MetricCollector creates a middleware that collects metrics about HTTP requests. This is a placeholder implementation that can be extended to integrate with your metrics collection system.
func PanicRecovery ¶
func PanicRecovery(logger *slog.Logger) Middleware
PanicRecovery creates a middleware that recovers from panics in HTTP handlers. It logs the error and returns a 500 Internal Server Error response.
func StateDebugger ¶
func StateDebugger(stateProvider func() string) Middleware
StateDebugger creates a middleware that adds the current server state to the response headers.
func WildcardRouter ¶
func WildcardRouter(prefix string) Middleware
WildcardRouter creates a middleware that handles requests with a prefix pattern, stripping the prefix before passing to the handler, useful for passing a group of routes to a single handler function.
type ResponseWriter ¶ added in v0.0.9
type ResponseWriter struct { http.ResponseWriter // contains filtered or unexported fields }
ResponseWriter is a wrapper for http.ResponseWriter that captures the status code and the number of bytes written. It is useful in middleware for logging, metrics, and conditional logic based on the response.
func (*ResponseWriter) BytesWritten ¶ added in v0.0.9
func (rw *ResponseWriter) BytesWritten() int
BytesWritten returns the total number of bytes written to the response body.
func (*ResponseWriter) Status ¶ added in v0.0.9
func (rw *ResponseWriter) Status() int
Status returns the HTTP status code that was written to the response. If no status code was explicitly set, it returns 0.
func (*ResponseWriter) Write ¶ added in v0.0.9
func (rw *ResponseWriter) Write(b []byte) (int, error)
Write captures that a response has been written, counts the bytes, and calls the underlying Write.
func (*ResponseWriter) WriteHeader ¶ added in v0.0.9
func (rw *ResponseWriter) WriteHeader(statusCode int)
WriteHeader captures the status code and calls the underlying WriteHeader.