Documentation
¶
Overview ¶
Package middleware holds the HTTP middleware of the relay proxy, as plain http.Handler wrappers.
Index ¶
- Constants
- func Chain(h http.Handler, middleware ...Func) http.Handler
- func ETagHuma() func(huma.Context, func(huma.Context))
- func ETagMatches(ifNoneMatch, etag string) bool
- func ETagMatchesAny(lines []string, etag string) bool
- func RoutePattern(r *http.Request) string
- type BodyTagger
- type Func
- func BodyDump(skipper func(r *http.Request) bool, dump func(r *http.Request, body []byte)) Func
- func CORS() Func
- func DeprecatedAlias(replacement string) Func
- func ETag() Func
- func Prometheus(cfg PrometheusConfig) (Func, error)
- func StreamAuthorizer(keys service.APIKeys, flagsetManager service.FlagSetResolver, exposeError bool) Func
- func VersionHeader(version string, disabled bool) Func
- type KeyAuth
- type KeyAuthConfig
- type PrometheusConfig
Constants ¶
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
Chain returns h wrapped in the middleware, the first one being the outermost.
func ETagHuma ¶ added in v0.2.0
ETagHuma is ETag for a huma operation, register it in the middleware of the operation.
func ETagMatches ¶ added in v0.2.0
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
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
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
Func is a middleware: it wraps a handler in another one.
func BodyDump ¶ added in v0.2.0
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
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 ¶
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
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
HTTP is the entry point for a http.Handler.
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.