httpmw

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package httpmw holds HTTP middleware that runs ahead of the API's own handling.

Index

Constants

View Source
const RequestIDHeader = "X-Request-Id"

RequestIDHeader is the header a caller can supply its own correlation ID on.

Variables

This section is empty.

Functions

func IsKnownSource

func IsKnownSource(source IPExtractionSource) bool

IsKnownSource reports whether the given source is one this middleware implements.

func LoggerFromContext

func LoggerFromContext(ctx context.Context, fallback bpcore.Logger) bpcore.Logger

LoggerFromContext returns the request-scoped logger, falling back to the given logger when the middleware has not run.

A fallback rather than a nil logger, so that a caller never has to decide whether it is worth logging at all.

func RequestIDFromContext

func RequestIDFromContext(ctx context.Context) (string, bool)

RequestIDFromContext returns the correlation ID resolved for the request, and whether one was resolved at all.

func SourceIPFromContext

func SourceIPFromContext(ctx context.Context) (string, bool)

SourceIPFromContext returns the source IP the middleware resolved for the request, and whether one was resolved at all.

An absent value means no address could be trusted rather than that the request had none, so a caller records nothing rather than guessing.

func WithLoggerFields

func WithLoggerFields(ctx context.Context, fields ...bpcore.LogField) context.Context

WithLoggerFields returns a context whose request-scoped logger also carries the given fields.

This is how later middleware adds what it alone knows, without this package having to know about it. Returns the context unchanged when the middleware has not run, so that a caller does not have to check.

Types

type ExtractIPMiddleware

type ExtractIPMiddleware struct {
	// contains filtered or unexported fields
}

ExtractIPMiddleware determines the source IP of a request and puts it on the request context, so that everything downstream agrees on one answer rather than each deriving its own.

func NewExtractIPMiddleware

func NewExtractIPMiddleware(opts ...ExtractIPMiddlewareOption) *ExtractIPMiddleware

NewExtractIPMiddleware creates the middleware that resolves a request's source IP.

func (*ExtractIPMiddleware) Middleware

func (m *ExtractIPMiddleware) Middleware(next http.Handler) http.Handler

type ExtractIPMiddlewareOption

type ExtractIPMiddlewareOption func(*ExtractIPMiddleware)

ExtractIPMiddlewareOption configures an ExtractIPMiddleware.

func WithExtractIPMiddlewareSource

func WithExtractIPMiddlewareSource(source IPExtractionSource) ExtractIPMiddlewareOption

WithExtractIPMiddlewareSource sets what sits in front of the engine, in place of DirectExtractionSource.

type IPExtractionSource

type IPExtractionSource string

IPExtractionSource names what sits in front of the engine, which is what decides how a request's source IP is determined.

Which header to read, and whether to trust one at all, is a property of the deployment rather than of the engine. A header is the caller's own claim unless something in the request path is known to rewrite or append to it.

const (
	// DirectExtractionSource takes the address the connection came from and ignores
	// every header.
	//
	// The default, because an engine reached directly is the case where a forwarded
	// header is purely caller-supplied and trusting one would let any caller choose the
	// address recorded against its actions.
	DirectExtractionSource IPExtractionSource = "direct"

	// AWSALBExtractionSource reads the address an AWS application load balancer
	// appended to X-Forwarded-For.
	AWSALBExtractionSource IPExtractionSource = "aws-alb"

	// GCPLoadBalancerExtractionSource reads the address a Google Cloud external
	// HTTP(S) load balancer appended to X-Forwarded-For.
	//
	// That balancer appends two entries, the client address followed by its own, so the
	// client is the second from the right rather than the last.
	GCPLoadBalancerExtractionSource IPExtractionSource = "gcp-lb"

	// AzureAppGatewayExtractionSource reads the address an Azure Application Gateway
	// appended to X-Forwarded-For, which carries a port.
	AzureAppGatewayExtractionSource IPExtractionSource = "azure-app-gateway"

	// AzureFrontDoorExtractionSource reads the address Azure Front Door set on
	// X-Azure-ClientIP.
	AzureFrontDoorExtractionSource IPExtractionSource = "azure-front-door"

	// CloudflareExtractionSource reads the address Cloudflare set on CF-Connecting-IP.
	CloudflareExtractionSource IPExtractionSource = "cloudflare"
)

func KnownSources

func KnownSources() []IPExtractionSource

KnownSources returns every source that can be configured, for validating what an operator asked for before the engine starts serving.

type LogCorrelationMiddleware

type LogCorrelationMiddleware struct {
	// contains filtered or unexported fields
}

LogCorrelationMiddleware puts the identity of a request's span onto everything logged for that request.

func NewLogCorrelationMiddleware

func NewLogCorrelationMiddleware(xray bool) *LogCorrelationMiddleware

NewLogCorrelationMiddleware creates the middleware, including the X-Ray form of the trace ID where the engine's traces reach X-Ray.

func (*LogCorrelationMiddleware) Middleware

func (m *LogCorrelationMiddleware) Middleware(next http.Handler) http.Handler

type RequestContextMiddleware

type RequestContextMiddleware struct {
	// contains filtered or unexported fields
}

RequestContextMiddleware resolves a request's correlation ID and builds the request-scoped logger that everything handling the request logs through.

Both are resolved once, here, because both are answers that have to be the same wherever they are read. A correlation ID generated separately by two consumers correlates nothing, and a log line that disagrees with its own audit record about where a request came from is worse than one that says nothing.

This does not log requests. It prepares what a log line is enriched with; emitting one is the job of whatever is handling the request.

Runs after ExtractIPMiddleware, whose answer it reads. Registered the other way round, every line is logged without a source IP, which is the field that matters most on the lines nothing else accounts for.

func NewRequestContextMiddleware

func NewRequestContextMiddleware(logger bpcore.Logger) *RequestContextMiddleware

NewRequestContextMiddleware creates the middleware that resolves the request ID and the request-scoped logger, deriving the latter from the given base logger.

func (*RequestContextMiddleware) Middleware

func (m *RequestContextMiddleware) Middleware(next http.Handler) http.Handler

Jump to

Keyboard shortcuts

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