Documentation ¶
Overview ¶
Package middleware contains helpers for adding standard behavior like authentication and metrics to REST endpoints.
Index ¶
- func CORS(next http.Handler) http.Handler
- func Chain(middlewares ...mux.MiddlewareFunc) mux.MiddlewareFunc
- func ContextFromBase64(base64Context string) (ldcontext.Context, error)
- func CountBrowserConns(handler http.Handler) http.Handler
- func CountMobileConns(handler http.Handler) http.Handler
- func CountServerConns(handler http.Handler) http.Handler
- func RequestCount(measure metrics.Measure) mux.MiddlewareFunc
- func SelectEnvironmentByAuthorizationKey(sdkKind basictypes.SDKKind, envs RelayEnvironments) mux.MiddlewareFunc
- func Streaming(next http.Handler) http.Handler
- func WithEnvContextInfo(ctx context.Context, info EnvContextInfo) context.Context
- type EnvContextInfo
- type RelayEnvironments
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CORS ¶
CORS is a middleware function that sets the appropriate CORS headers on a browser response (not counting Access-Control-Allow-Methods, which is set by gorilla/mux's CORS middleware based on the route handlers we've defined).
Also, if the HTTP method is OPTIONS, it will short-circuit the rest of the middleware chain so that the underlying handler is *not* called-- since an OPTIONS request should not do anything except set the CORS response headers. Therefore, we should always put the gorilla/mux CORS middleware before this one.
func Chain ¶
func Chain(middlewares ...mux.MiddlewareFunc) mux.MiddlewareFunc
Chain combines a series of middleware functions that will be applied in the same order.
func ContextFromBase64 ¶
ContextFromBase64 decodes a base64-encoded go-server-sdk evaluation context. If any decoding/unmarshaling errors occur, or the decoded context is invalid by the rules of the Go SDK, an error is returned.
func CountBrowserConns ¶
CountBrowserConns is a middleware function that increments the total number of browser connections, and also increments the number of active browser connections until the handler ends.
func CountMobileConns ¶
CountMobileConns is a middleware function that increments the total number of mobile connections, and also increments the number of active mobile connections until the handler ends.
func CountServerConns ¶
CountServerConns is a middleware function that increments the total number of server-side connections, and also increments the number of active server-side connections until the handler ends.
func RequestCount ¶
func RequestCount(measure metrics.Measure) mux.MiddlewareFunc
RequestCount is a middleware function that increments the specified metric for each request.
func SelectEnvironmentByAuthorizationKey ¶
func SelectEnvironmentByAuthorizationKey(sdkKind basictypes.SDKKind, envs RelayEnvironments) mux.MiddlewareFunc
SelectEnvironmentByAuthorizationKey creates a middleware function that attempts to authenticate the request using the appropriate kind of credential for the basictypes.SDKKind. If successful, it updates the request context so GetEnvContextInfo will return environment information. If not successful, it returns an error response.
func Streaming ¶
Streaming is a middleware function that sets the appropriate headers on a streaming response.
func WithEnvContextInfo ¶
func WithEnvContextInfo(ctx context.Context, info EnvContextInfo) context.Context
WithEnvContextInfo returns a new Context with the EnvContextInfo added.
Types ¶
type EnvContextInfo ¶
type EnvContextInfo struct { // Env is an existing EnvContext object for the environment. Env relayenv.EnvContext // Credential is the SDK key, mobile key, or environment ID that was used in the request. Credential config.SDKCredential }
EnvContextInfo is data that we attach to the current HTTP request to indicate which environment it is related to.
func GetEnvContextInfo ¶
func GetEnvContextInfo(ctx context.Context) EnvContextInfo
GetEnvContextInfo returns the EnvContextInfo that is attached to the specified Context (normally obtained from a request as request.Context()). It panics if there is none, since this context data is supposed to be injected by our middleware and our handlers cannot work without it.
type RelayEnvironments ¶
type RelayEnvironments interface { GetEnvironment(config.SDKCredential) (env relayenv.EnvContext, fullyConfigured bool) GetAllEnvironments() []relayenv.EnvContext }
RelayEnvironments defines the methods for looking up environments. This is represented as an interface so that test code can mock that capability.