middleware

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2024 License: LGPL-3.0 Imports: 12 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsStatusError

func IsStatusError(statusCode int) bool

func JsonErrMiddleware

func JsonErrMiddleware(next http.Handler, genericMessage bool) http.Handler

JsonErrMiddleware is a middleware mostly intended to handle http errors on a JSON api it will check if a handler wrote a non 2xx response code, intercept the response and transform it to a generic JSON error response.

the motivation is that we don't always control the error format of our responses and maybe also don't want to force json error responses always

if genericMessage is set to true, the error message will not printed; only the generic message related to the response code; this is intended for production environments to prevent leaking details about errors. TODO: this assumes that only 200 response codes contain usable body, e.g. not usable for rendering html

func PromLogMiddleware

func PromLogMiddleware(next http.Handler, histogram Histogram, l *zerolog.Logger) http.Handler

func PromMiddleware

func PromMiddleware(next http.Handler, histogram Histogram) http.Handler

func ZerologMiddleware

func ZerologMiddleware(next http.Handler, l *zerolog.Logger) http.Handler

ZerologMiddleware logs every request using the provided logger

Types

type Histogram

type Histogram struct {
	// contains filtered or unexported fields
}

func NewHistogram

func NewHistogram(prefix string, buckets []float64, registry prometheus.Registerer) Histogram

type ReqThrottle

type ReqThrottle struct {
	MinDelay time.Duration
	MaxDelay time.Duration
	On       bool
}

func (ReqThrottle) Throttle

func (t ReqThrottle) Throttle(next http.Handler) http.Handler

type StatWriter

type StatWriter struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

StatWriter is a wrapper to a httpResponse writer that allows to intercept and extract the status code that the upstream code has defined

func NewWriter

func NewWriter(w http.ResponseWriter, teeBodyOnErr bool) *StatWriter

NewWriter will return a pointer to a response writer if teeBodyOnErr is set to true, and in case of a non 200 status code, the writer will not write the Response but instead into it's own buffer. this allows to modify the response before writing it to the output

func (*StatWriter) StatusCode

func (r *StatWriter) StatusCode() int

func (*StatWriter) StatusCodeStr

func (r *StatWriter) StatusCodeStr() string

func (*StatWriter) Write

func (r *StatWriter) Write(b []byte) (int, error)

Write returns underlying Write result, while counting data size

func (*StatWriter) WriteHeader

func (r *StatWriter) WriteHeader(code int)

WriteHeader writes the response status code and stores it internally

Jump to

Keyboard shortcuts

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