middleware

package
v6.9.1 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2023 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package middleware contains helpers for adding standard behavior like authentication and metrics to REST endpoints.

This package exports its symbols from core, rather than being in core/internal, because the Relay and Relay Enterprise projects will need to use the standard middleware whenever they create their own endpoints.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CORS

func CORS(next http.Handler) http.Handler

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 CountBrowserConns

func CountBrowserConns(handler http.Handler) http.Handler

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

func CountMobileConns(handler http.Handler) http.Handler

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

func CountServerConns(handler http.Handler) http.Handler

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

func Streaming(next http.Handler) http.Handler

Streaming is a middleware function that sets the appropriate headers on a streaming response.

func UserFromBase64

func UserFromBase64(base64User string) (lduser.User, error)

UserFromBase64 decodes a base64-encoded go-server-sdk user. If any decoding/unmarshaling errors occur or the user is missing the "key" attribute an error is returned.

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.

Jump to

Keyboard shortcuts

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