middleware

package
v0.3.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: 17 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 BodyTag added in v0.3.0

func BodyTag(body []byte) string

BodyTag is the ETag of a response body: strong, the length of the body and its SHA-1 in hexadecimal, in double quotes. It does not allocate but for the tag it returns.

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 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 Revalidate added in v0.3.0

func Revalidate(ctx context.Context, tag string) bool

Revalidate names tag as the ETag of the answer that the handler sends if it succeeds, and reports whether the request holds it in If-None-Match: the handler then answers 304 (the status the middleware sends the tag with) and builds nothing. The handler calls it after every check that can refuse the request, and only with the tag of the answer it would send now. Without Validator in the operation (or with an empty tag) it names nothing and reports false.

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 "/").

func Validator added in v0.3.0

func Validator() func(huma.Context, func(huma.Context))

Validator is the middleware of an operation whose handler knows the tag of its answer before it builds it (see Revalidate): the answer is not buffered nor hashed, and the tag is sent with a 200 or a 304 only.

Types

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 part of the body of every request that is not skipped to dump, once the request is handled. The body is not read ahead: the dump holds what the handler read (at most maxDumpedRequestBytes), so a request that the handler refuses without reading its body (a refused API key) costs the dump nothing.

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 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