Documentation
¶
Overview ¶
Package middleware provides HTTP middleware utilities for the httprpc framework.
Index ¶
- func CORS(cfg CORSConfig) httprpc.Middleware
- func Logging(logger *slog.Logger) httprpc.Middleware
- func Recover(logger *slog.Logger) httprpc.Middleware
- func RequestID(headerName string) httprpc.Middleware
- func RequestIDFromContext(ctx context.Context) (string, bool)
- func RequestSizeLimit(maxBytes int64) httprpc.Middleware
- func ReverseProxy(cfg ReverseProxyConfig) httprpc.Middleware
- func ReverseProxyHandler(cfg ReverseProxyConfig) http.Handler
- func Timeout(d time.Duration) httprpc.Middleware
- type CORSConfig
- type ReverseProxyConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CORS ¶
func CORS(cfg CORSConfig) httprpc.Middleware
CORS returns middleware applying the provided CORSConfig.
func Logging ¶
func Logging(logger *slog.Logger) httprpc.Middleware
Logging logs request/response metadata using slog. If logger is nil, slog.Default is used.
func Recover ¶
func Recover(logger *slog.Logger) httprpc.Middleware
Recover returns middleware that recovers from panics and writes a 500 response. If logger is nil, slog.Default is used.
func RequestID ¶
func RequestID(headerName string) httprpc.Middleware
RequestID injects a request ID into the context and response headers. If headerName is empty, "X-Request-ID" is used. If the incoming request already has the header, it is propagated.
func RequestIDFromContext ¶
RequestIDFromContext returns the request ID set by RequestID middleware, if present.
func RequestSizeLimit ¶
func RequestSizeLimit(maxBytes int64) httprpc.Middleware
RequestSizeLimit limits the readable request body to maxBytes using http.MaxBytesReader.
func ReverseProxy ¶
func ReverseProxy(cfg ReverseProxyConfig) httprpc.Middleware
ReverseProxy returns a middleware that proxies matching requests to the target. If MatchPrefix is set and the path doesn't match, the request is passed to the next handler.
func ReverseProxyHandler ¶
func ReverseProxyHandler(cfg ReverseProxyConfig) http.Handler
ReverseProxyHandler returns an http.Handler that proxies requests to the target.
Types ¶
type CORSConfig ¶
type CORSConfig struct {
AllowedOrigins []string
AllowedMethods []string
AllowedHeaders []string
ExposeHeaders []string
AllowCredentials bool
MaxAgeSeconds int
}
CORSConfig configures CORS behavior. For production, adjust the allowed origins and methods to your needs.
type ReverseProxyConfig ¶
type ReverseProxyConfig struct {
Target *url.URL
// MatchPrefix, if set, limits proxying to paths with this prefix.
// When used with the middleware form, requests that don't match fall through to next.
MatchPrefix string
// StripPrefix removes the given prefix from the request path before forwarding.
StripPrefix string
// PreserveHost keeps the incoming Host header instead of rewriting to the target's host.
PreserveHost bool
// ErrorHandler handles proxy errors. If nil, a 502 Bad Gateway is returned.
ErrorHandler func(http.ResponseWriter, *http.Request, error)
}
ReverseProxyConfig configures the reverse proxy middleware/handler. Target is required.