Documentation
¶
Overview ¶
Package middleware holds the HTTP middleware of the relay proxy, as plain http.Handler wrappers.
Index ¶
- Constants
- func BodyTag(body []byte) string
- func Chain(h http.Handler, middleware ...Func) http.Handler
- func ETagMatches(ifNoneMatch, etag string) bool
- func ETagMatchesAny(lines []string, etag string) bool
- func Revalidate(ctx context.Context, tag string) bool
- func RoutePattern(r *http.Request) string
- func Validator() func(huma.Context, func(huma.Context))
- 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 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 BodyTag ¶ added in v0.3.0
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
Chain returns h wrapped in the middleware, the first one being the outermost.
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 Revalidate ¶ added in v0.3.0
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
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 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 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
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 ¶
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.