middleware

package
v0.0.0-...-5c3b92c Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2023 License: MIT Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var LogLevel *zap.AtomicLevel

LogLevel sets the current Zap root logger's level when using the logging middleware. This can be changed dynamically at runtime.

View Source
var LogTracePrefix = "dd."

LogTracePrefix is used to prefix OpenTracing trace and span ID key names in emitted log message tag names. Use this to integrate with DataDog and other tracing service providers.

View Source
var MaxLogBodyBytes int64 = 10 * 1024

MaxLogBodyBytes logs at most this many bytes of any request body during a panic when using the recovery middleware. Defaults to 10KiB. Changing this value changes the amount of potential memory used for *each* incoming request, so change it carefully and complement the change with load testing because larger values can have a detrimental effect on the server.

View Source
var NewLogger func() (*zap.Logger, error) = NewDefaultLogger

NewLogger is a function that returns a new logger instance to use with the logger middleware.

View Source
var RemovedHeaders = []string{"Authorization"}

RemovedHeaders defines a list of HTTP headers that will be redacted from the request in the Recovery handler--if any logging or other output occurs, these headings will have value '<redacted>'. By default, a huma service removes the 'Authorization' header to avoid leaking sensitive information, but clients can override this with an empty slice.

Functions

func AddLoggerOptions

func AddLoggerOptions(app Flagger)

AddLoggerOptions adds command line options for enabling debug logging.

func ContentEncoding

func ContentEncoding(next http.Handler) http.Handler

ContentEncoding uses content negotiation with the client to pick an appropriate encoding (compression) method and transparently encodes the response. Supports GZip and Brotli.

func DefaultChain

func DefaultChain(next http.Handler) http.Handler

DefaultChain sets up the default middlewares conveniently chained together into a single easy-to-add handler.

func Defaults

func Defaults(app Middlewarer)

Defaults sets up the default middleware. This convenience function adds the `DefaultChain` to the router and adds the `--debug` option for logging to the CLI if app is a CLI.

func GetBufferedBody

func GetBufferedBody(ctx context.Context) []byte

GetBufferedBody returns the buffered body from a request when using the recovery middleware, up to MaxLogBodyBytes.

func GetLogger

func GetLogger(ctx context.Context) *zap.SugaredLogger

GetLogger returns the contextual logger for the current request. If no logger is present, it returns a no-op logger so no nil check is required.

func Logger

func Logger(next http.Handler) http.Handler

Logger creates a new middleware to set a tagged `*zap.SugarLogger` in the request context. It debug logs request info. If the current terminal is a TTY, it will try to use colored output automatically.

func NewDefaultLogger

func NewDefaultLogger() (*zap.Logger, error)

NewDefaultLogger returns a new low-level `*zap.Logger` instance. If the current terminal is a TTY, it will try ot use colored output automatically.

func OpenTracing

func OpenTracing(next http.Handler) http.Handler

OpenTracing provides a middleware for cross-service tracing support.

func PreferMinimal

func PreferMinimal(next http.Handler) http.Handler

PreferMinimal will remove the response body and return 204 No Content for any 2xx response where the request had the Prefer: return=minimal set on the request.

func Recovery

func Recovery(onPanic PanicFunc) func(http.Handler) http.Handler

Recovery prints stack traces on panic when used with the logging middleware.

func SetLogger

func SetLogger(r *http.Request, logger *zap.SugaredLogger) *http.Request

SetLogger sets the contextual logger for the current request.

func SetLoggerInContext

func SetLoggerInContext(ctx huma.Context, logger *zap.SugaredLogger)

SetLoggerInContext allows you to override the logger in the current request context. This is useful for modifying the logger in input resolvers.

Types

type Flagger

type Flagger interface {
	Flag(name string, short string, description string, defaultValue interface{})
	PreStart(f func())
}

Flagger lets you create command line flags and functions that use them.

type Middlewarer

type Middlewarer interface {
	Middleware(middlewares ...func(next http.Handler) http.Handler)
}

Middlewarer lets you add middlewares

type PanicFunc

type PanicFunc func(ctx context.Context, err error, request string)

PanicFunc defines a function to run after a panic, which allows you to set up custom logging, metrics, etc.

Jump to

Keyboard shortcuts

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