middleware

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2025 License: Apache-2.0 Imports: 18 Imported by: 3

Documentation

Index

Constants

View Source
const (
	TraceIDKey ctxKeyTraceID = 0
	UserIDKey  ctxKeyUserID  = "user_id"
)

TraceIDKey and UserIDKey are the keys that hold the unique trace ID and user ID in a request context.

Variables

View Source
var (
	APILoggerKey       = &contextKey{"logger"}
	RunLoggerKey       = &contextKey{"runLogger"}
	RunLoggerBufferKey = &contextKey{"runLoggerBuffer"}
)

APILoggerKey is a context key used for associating a logger with a request.

View Source
var (
	TraceIDHeader = "x-kusion-trace"
	UserIDHeader  = "x-kusion-user"
)

TraceIDHeader and UserIDHeader is the name of the HTTP Header which contains the trace id and user id.

View Source
var StartTimeKey = &contextKey{"startTime"}

StartTimeKey is a context key used for storing the start time of a request.

Functions

func APILoggerMiddleware added in v0.13.0

func APILoggerMiddleware(logFile string) func(http.Handler) http.Handler

APILoggerMiddleware injects a logger, configured with a request ID, into the request context for use throughout the request's lifecycle.

func DefaultLoggerMiddleware added in v0.13.0

func DefaultLoggerMiddleware(logFile string) func(http.Handler) http.Handler

DefaultLogger is a middleware that provides basic request logging using chi's built-in Logger middleware.

func GetMiddlewareLogger added in v0.13.0

func GetMiddlewareLogger(ctx context.Context, logFile string) *httplog.Logger

func GetStartTime

func GetStartTime(ctx context.Context) time.Time

GetStartTime returns the start time from the given context if one is present. If the start time is not present or the context is nil, returns the zero time.

func GetTraceID added in v0.13.0

func GetTraceID(ctx context.Context) string

GetTraceID returns a trace ID from the given context if one is present. Returns the empty string if a trace ID cannot be found.

func GetUserID added in v0.13.0

func GetUserID(ctx context.Context) string

GetUserID returns a user ID from the given context if one is present. Returns the empty string if a user ID cannot be found.

func InitLogger added in v0.13.0

func InitLogger(logFilePath string, name string) *httplog.Logger

func InitLoggerBuffer added in v0.14.0

func InitLoggerBuffer(name string) (*httplog.Logger, *bytes.Buffer)

func NextTraceID added in v0.13.0

func NextTraceID() uint64

NextTraceID generates the next trace ID in the sequence.

func ReadOnlyMode

func ReadOnlyMode(next http.Handler) http.Handler

ReadOnlyMode disallows non-GET requests in read-only mode.

func SubjectWhitelisted added in v0.13.0

func SubjectWhitelisted(subject, orgType, orgName string, whitelist []string) bool

func Timing

func Timing(next http.Handler) http.Handler

Timing is a middleware that captures the current time at the start of a request and stores it in the request context. This start time can be used to measure request processing duration.

func TokenAuthMiddleware added in v0.13.0

func TokenAuthMiddleware(keyMap map[string]any, whitelist []string, logFilePath string) func(http.Handler) http.Handler

This is the middleware function that verifies the JWT against a JWKS KeyMap

func TraceID added in v0.13.0

func TraceID(next http.Handler) http.Handler

TraceID is a middleware that injects a trace ID into the context of each request. A trace ID is a string of the form "host.example.com/random-0001", where "random" is a base62 random string that uniquely identifies this go process, and where the last number is an atomically incremented request counter.

func UserID added in v0.13.0

func UserID(next http.Handler) http.Handler

UserID is a middleware that injects the operator of the request into the context of each request.

Types

This section is empty.

Jump to

Keyboard shortcuts

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