middleware

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: MIT Imports: 20 Imported by: ? 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 middleware holds the HTTP middleware of the relay proxy, as plain http.Handler wrappers.

Index

Constants

View Source
const (
	// VersionHeaderName is the response header carrying the version of the relay proxy.
	VersionHeaderName = "X-Semaphore-Version"
	// LegacyVersionHeaderName is the name GO Feature Flag used for the same header, still sent
	// next to VersionHeaderName for the clients that read it.
	LegacyVersionHeaderName = "X-Gofeatureflag-Version"
)

Variables

This section is empty.

Functions

func Chain added in v0.2.0

func Chain(h http.Handler, middleware ...Func) http.Handler

Chain returns h wrapped in the middleware, the first one being the outermost.

func ETagHuma added in v0.2.0

func ETagHuma() func(huma.Context, func(huma.Context))

ETagHuma is ETag for a huma operation, register it in the middleware of the operation.

func ETagMatches added in v0.2.0

func ETagMatches(ifNoneMatch, etag string) bool

ETagMatches reports whether an If-None-Match header value matches the entity tag, as RFC 9110 section 13.1.2 defines it: the value is `*`, or a comma separated list of entity tags that is compared to the tag with the weak comparison (the `W/` prefix is ignored on both sides).

func ETagMatchesAny added in v0.2.0

func ETagMatchesAny(lines []string, etag string) bool

ETagMatchesAny is ETagMatches for the lines of the If-None-Match header of a request: several lines are one comma separated list (RFC 9110 section 5.3).

func RoutePattern added in v0.2.0

func RoutePattern(r *http.Request) string

RoutePattern returns the route that served the request, in the form of the labels of the metrics: the path template with its parameters written ":name" ("/v1/feature/:flagKey/eval"), or the path of the request when no route matched (the mux answers it with the pattern "/").

Types

type BodyTagger added in v0.2.0

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

BodyTagger computes the tag that ETag gives to the body that is written to it, for a handler that has to know the tag of its own answer.

func NewBodyTagger added in v0.2.0

func NewBodyTagger() *BodyTagger

NewBodyTagger returns a tagger of an empty body.

func (*BodyTagger) Add added in v0.2.0

func (b *BodyTagger) Add(data []byte)

Add adds bytes to the body.

func (*BodyTagger) Tag added in v0.2.0

func (b *BodyTagger) Tag() string

Tag is the tag of the body written so far.

type Func added in v0.2.0

type Func = func(http.Handler) http.Handler

Func is a middleware: it wraps a handler in another one.

func BodyDump added in v0.2.0

func BodyDump(skipper func(r *http.Request) bool, dump func(r *http.Request, body []byte)) Func

BodyDump gives the body of every request that is not skipped to dump, once the request is handled. The body is read before the handler, which reads it from a copy.

func CORS added in v0.2.0

func CORS() Func

CORS allows any origin and answers every preflight with a fixed list of methods and the headers the browser asked for. A request without an Origin header goes through untouched (but for Vary).

func DeprecatedAlias added in v0.2.0

func DeprecatedAlias(replacement string) Func

DeprecatedAlias adds the RFC 8594 Deprecation and Link headers pointing clients to the replacement of the endpoint. Sunset is not set until the removal date is decided.

func ETag added in v0.2.0

func ETag() Func

ETag adds a strong ETag to the 2xx responses with a body, and answers 304 when the request carries it in If-None-Match. The body is buffered to be hashed: only the routes that serve a small document use it.

func Prometheus added in v0.2.0

func Prometheus(cfg PrometheusConfig) (Func, error)

Prometheus measures the requests: how many, how long, how big the requests and the responses are, by status code, method, host and route (the route, not the path, so the number of series stays bounded).

func StreamAuthorizer

func StreamAuthorizer(keys service.APIKeys, flagsetManager service.FlagSetResolver, exposeError bool) Func

StreamAuthorizer is a middleware that checks the apiKey query param to authorize streaming connections (websocket / SSE). In flag set mode (flagsetManager is not nil) a key is accepted if it routes to a flag set, and the name of the flag set resolved here is the one the handler uses. exposeError adds the error to the body of a refusal.

func VersionHeader

func VersionHeader(version string, disabled bool) Func

VersionHeader is a middleware that adds the version of the relay proxy in the headers VersionHeaderName and LegacyVersionHeaderName, unless disabled is true.

type KeyAuth added in v0.2.0

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

KeyAuth is the middleware that authenticates a request with the API key of its X-API-Key header, or else with the bearer token of its Authorization header. A request that is refused gets a 401 and a {"message": ...} body. It has two entry points that share the same rules: KeyAuth.HTTP for a http.Handler, KeyAuth.Huma for a huma operation.

func NewKeyAuth added in v0.2.0

func NewKeyAuth(cfg KeyAuthConfig) KeyAuth

NewKeyAuth returns the authentication middleware of the configuration.

func (KeyAuth) AllowsAnonymous added in v0.2.0

func (k KeyAuth) AllowsAnonymous() bool

AllowsAnonymous reports whether a request without a key reaches the validator: the operations behind this middleware are documented with an optional key.

func (KeyAuth) HTTP added in v0.2.0

func (k KeyAuth) HTTP() Func

HTTP is the entry point for a http.Handler.

func (KeyAuth) Huma added in v0.2.0

func (k KeyAuth) Huma() func(huma.Context, func(huma.Context))

Huma is the entry point for a huma operation, it only uses the huma.Context interface.

type KeyAuthConfig added in v0.2.0

type KeyAuthConfig struct {
	// Validator reports whether the key is accepted. It returns the context to give to the handler, which may
	// carry values resolved while validating the key.
	Validator func(ctx context.Context, key string) (context.Context, bool)
	// Skipper reports that the requests need no authentication.
	Skipper func() bool
	// AllowAnonymous makes the Validator decide a request that carries no key at all: it is called with an empty
	// key. A request with a key, right or wrong, is never given to it that way.
	AllowAnonymous bool
}

KeyAuthConfig defines the configuration of the API key authentication.

type PrometheusConfig added in v0.2.0

type PrometheusConfig struct {
	// Registerer receives the metrics.
	Registerer prometheus.Registerer
	// HistogramOptsFunc changes the options of every histogram.
	HistogramOptsFunc func(opts prometheus.HistogramOpts) prometheus.HistogramOpts
	// Subsystem is the subsystem of the names of the metrics.
	Subsystem string
}

PrometheusConfig is the configuration of the HTTP metrics.

Jump to

Keyboard shortcuts

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