Documentation
¶
Overview ¶
Package realip provides middleware that extracts the real client IP address from request headers and stores it in the request context.
The extraction is delegated to a clientip.Extractor which handles trusted-proxy validation and header parsing (X-Forwarded-For, X-Real-IP, etc.). Downstream middleware and handlers retrieve the IP via FromContext.
This middleware has no dependencies and should be placed early in the chain so that other middleware (logger, limiter, tracing) can read the real IP from context.
Example ¶
extractor, _ := clientip.NewExtractor(
clientip.WithHeaders("X-Real-IP", "X-Forwarded-For"),
)
handler := realip.Middleware(extractor, logger)(yourHandler)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FromContext ¶
FromContext returns the real IP address stored by this middleware from the context. Returns an invalid netip.Addr (where IsValid returns false) if no real IP is present. The value is set by [middleware.Handler] or manually via NewContext.
func Middleware ¶
Middleware returns an HTTP middleware that extracts the real client IP address from incoming requests and stores it in the request context. This is a convenience function; prefer New() for access to the full Middleware interface.
func NewContext ¶
NewContext returns a new context with the given real IP address. This is useful for injecting a real IP into the context manually, for example in tests. Retrieve it later with FromContext.
Types ¶
This section is empty.