realip

package
v0.0.0-...-c7901e9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2026 License: MIT Imports: 8 Imported by: 0

README

realip

import "github.com/altessa-s/go-atlas/transport/http/server/middlewares/realip"

Package realip provides middleware that extracts the real client IP address from request headers and stores it in the request context. 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.

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

func FromContext(ctx context.Context) netip.Addr

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

func Middleware(extractor *clientip.Extractor, logger *slog.Logger) func(http.Handler) http.Handler

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 New

func New(extractor *clientip.Extractor, logger *slog.Logger) *middleware

New creates a new real IP extraction middleware.

func NewContext

func NewContext(ctx context.Context, ip netip.Addr) context.Context

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL