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 ¶
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 ¶
NewBackend constructs a net/http-backed routing.Backend with the standard middleware and OpenTelemetry stack installed. Pass it to routing.New.
func (*Backend) Handle ¶
Handle registers handler for method at pattern, using net/http's native "METHOD /path/{name}" pattern syntax.
func (*Backend) Handler ¶
Handler returns the composed http.Handler: the standard middleware stack and any user middleware wrapped around the mux.
func (*Backend) PathValue ¶
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.
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 WithMetricsProvider ¶
WithMetricsProvider attaches a metrics provider, enabling request metrics.
func WithTracerProvider ¶
WithTracerProvider attaches a tracer provider, enabling spans on every request.