stdlib

package
v11.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2026 License: AGPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package stdlib provides a routing.Backend built on the standard library's net/http.ServeMux. It adds no third-party router dependency: Go's mux already supports method-scoped patterns ("GET /users/{id}") and per-request path values, which is exactly the shape routing.Backend needs. The shared observability, recovery, CORS, and OpenTelemetry middleware stack is applied around the mux, matching the chi backend's behavior.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RequestIDFunc

func RequestIDFunc(req *http.Request) string

RequestIDFunc returns the request ID assigned to a request by the backend's request-ID middleware, or "" if none is present. It can be handed to logging.Logger.SetRequestIDFunc so log lines carry the request ID.

Types

type Backend

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

Backend is a net/http.ServeMux implementation of routing.Backend. Global middleware is composed around the mux lazily in Handler; because the mux is wrapped by reference, routes registered after the first Handler call are still served.

It is exported, and returned by NewBackend, so a caller who has chosen the standard library mux can depend on that choice rather than on the seam every router backend shares.

func NewBackend

func NewBackend(cfg *Config, opts ...Option) *Backend

NewBackend constructs a net/http-backed routing.Backend with the standard middleware and OpenTelemetry stack installed. Pass it to routing.New.

func (*Backend) Handle

func (b *Backend) Handle(method, pattern string, handler http.Handler)

Handle registers handler for method at pattern, using net/http's native "METHOD /path/{name}" pattern syntax.

func (*Backend) Handler

func (b *Backend) Handler() http.Handler

Handler returns the composed http.Handler: the standard middleware stack and any user middleware wrapped around the mux.

func (*Backend) PathValue

func (b *Backend) PathValue(req *http.Request, name string) string

PathValue returns the named path parameter, resolved by the ServeMux from the matched pattern.

func (*Backend) Use

func (b *Backend) Use(middleware ...routing.Middleware)

Use installs global middleware, applied to every route. It may be called at any time before Handler; unlike chi, this backend imposes no ordering constraint relative to Handle. Use appends middleware to the chain.

It must be called before Handler(). The chain is composed once, on the first Handler() call, so middleware added afterwards was silently dropped — the server ran without the authentication or rate limiting the caller believed it had registered. That is now a panic: a middleware that does not run is not a condition a process should serve traffic in.

type Config

type Config struct {
	ServiceName            string   `env:"SERVICE_NAME"              json:"serviceName,omitempty"            yaml:"serviceName,omitempty"`
	ValidDomains           []string `env:"VALID_DOMAINS"             json:"validDomains,omitempty"           yaml:"validDomains,omitempty"`
	EnableCORSForLocalhost bool     `env:"ENABLE_CORS_FOR_LOCALHOST" json:"enableCORSForLocalhost,omitempty" yaml:"enableCORSForLocalhost,omitempty"`
	SilenceRouteLogging    bool     `env:"SILENCE_ROUTE_LOGGING"     json:"silenceRouteLogging,omitempty"    yaml:"silenceRouteLogging,omitempty"`
	// contains filtered or unexported fields
}

Config configures the stdlib (net/http.ServeMux) router backend. Its fields mirror the other backends so a service can switch backends without changing its configuration shape.

func (*Config) ValidateWithContext

func (cfg *Config) ValidateWithContext(ctx context.Context) error

ValidateWithContext validates a router config struct.

type Option

type Option func(*options)

Option configures the backend this package constructs. The zero configuration works: an absent logger logs nowhere, an absent tracer provider traces nowhere, and an absent metrics provider records nothing.

func WithLogger

func WithLogger(logger logging.Logger) Option

WithLogger attaches a logger.

func WithMetricsProvider

func WithMetricsProvider(metricsProvider metrics.Provider) Option

WithMetricsProvider attaches a metrics provider, enabling request metrics.

func WithTracerProvider

func WithTracerProvider(tracerProvider tracing.Provider) Option

WithTracerProvider attaches a tracer provider, enabling spans on every request.

Jump to

Keyboard shortcuts

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