Documentation
¶
Overview ¶
Package middleware provides common HTTP middleware functions for the velocity router.
Available Middlewares:
- CORS: Handle Cross-Origin Resource Sharing
- Logger: HTTP request logging with color support
- RequestID: Request ID tracking
- ClientIP: Client IP detection
- ErrRecover: Panic recovery
Usage:
router := app.Router("/api", middleware.Logger(), middleware.CORS(), middleware.RequestID(), middleware.ClientIP(), middleware.ErrRecover(), )
Index ¶
- Constants
- func CORS(cfg ...CorsConfig) func(next http.HandlerFunc) http.HandlerFunc
- func ClientIP(cfg ...ClientIPConfig) func(next http.HandlerFunc) http.HandlerFunc
- func ErrRecover(cfg ...ErrRecoverConfig) func(next http.HandlerFunc) http.HandlerFunc
- func GetClientIP(r *http.Request) string
- func GetOrigin(r *http.Request) string
- func GetRequestID(r *http.Request) string
- func Logger(cfg ...LoggerConfig) func(next http.HandlerFunc) http.HandlerFunc
- func RequestID(cfg ...RequestIDConfig) func(next http.HandlerFunc) http.HandlerFunc
- type ClientIPConfig
- type CorsConfig
- type ErrRecoverConfig
- type LoggerConfig
- type RequestIDConfig
Constants ¶
const ( Reset = "\033[0m" Bold = "\033[1m" Red = "\033[31m" Green = "\033[32m" Yellow = "\033[33m" Blue = "\033[34m" Purple = "\033[35m" Cyan = "\033[36m" Gray = "\033[37m" )
Variables ¶
This section is empty.
Functions ¶
func CORS ¶
func CORS(cfg ...CorsConfig) func(next http.HandlerFunc) http.HandlerFunc
CORS returns a middleware that handles CORS.
Example:
router := app.Router("/api", middleware.CORS()) // or with config router := app.Router("/api", middleware.CORS(middleware.CorsConfig{ AllowedOrigins: &[]string{"https://example.com"}, }))
func ClientIP ¶
func ClientIP(cfg ...ClientIPConfig) func(next http.HandlerFunc) http.HandlerFunc
ClientIP returns a middleware that sets the client's IP address.
Example:
router := app.Router("/api", middleware.ClientIP()) // or with config router := app.Router("/api", middleware.ClientIP(middleware.ClientIPConfig{ Header: stringPtr("X-Real-IP"), TrustProxy: boolPtr(true), }))
func ErrRecover ¶
func ErrRecover(cfg ...ErrRecoverConfig) func(next http.HandlerFunc) http.HandlerFunc
ErrRecover returns a middleware that recovers from panics.
Example:
router := app.Router("/api", middleware.ErrRecover()) // or with custom callback router := app.Router("/api", middleware.ErrRecover(middleware.ErrRecoverConfig{ Cb: func(v any) { log.Printf("Panic: %v", v) }, }))
func GetClientIP ¶
GetClientIP retrieves the client IP from the request context.
func GetRequestID ¶
GetRequestID retrieves the request ID from the request context.
func Logger ¶
func Logger(cfg ...LoggerConfig) func(next http.HandlerFunc) http.HandlerFunc
Logger returns a middleware that logs HTTP requests.
Example:
router := app.Router("/api", middleware.Logger()) // or with config router := app.Router("/api", middleware.Logger(middleware.LoggerConfig{ Colors: boolPtr(true), Skip: &[]string{"/health"}, }))
func RequestID ¶
func RequestID(cfg ...RequestIDConfig) func(next http.HandlerFunc) http.HandlerFunc
RequestID returns a middleware that adds request ID tracking.
Example:
router := app.Router("/api", middleware.RequestID()) // or with config router := app.Router("/api", middleware.RequestID(middleware.RequestIDConfig{ Header: stringPtr("X-Correlation-ID"), }))
Types ¶
type ClientIPConfig ¶
type ClientIPConfig struct { // Header specifies the header to check for client IP Header *string // TrustProxy enables proxy headers when true TrustProxy *bool }
ClientIPConfig configures the ClientIP middleware.
type CorsConfig ¶
type CorsConfig struct { // AllowedMethods defines allowed HTTP methods AllowedMethods *[]string // AllowedHeaders defines allowed HTTP headers AllowedHeaders *[]string // ExposedHeaders defines headers exposed to the client ExposedHeaders *[]string // AllowedOrigins defines allowed origins AllowedOrigins *[]string }
CorsConfig configures the CORS middleware.
type ErrRecoverConfig ¶
type ErrRecoverConfig struct { // Cb is the callback function called on panic Cb func(v any) }
ErrRecoverConfig configures the ErrRecover middleware.
type LoggerConfig ¶
type LoggerConfig struct { // Format defines the log format string Format *string // Skip defines paths to skip logging Skip *[]string // Logger is a custom logger instance Logger *log.Logger // Colors enables colored output Colors *bool }
LoggerConfig configures the Logger middleware.
type RequestIDConfig ¶
type RequestIDConfig struct { // Header specifies the header for request ID Header *string // Generator is a function that generates request IDs Generator func() string }
RequestIDConfig configures the RequestID middleware.