middleware

package
v0.0.0-...-552cc5b Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2023 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// StripSlashes is a middleware that will match request paths with a trailing
	// slash, strip it from the path and continue routing through the mux, if a route
	// matches, then it will serve the handler.
	StripSlashes = middleware.StripSlashes

	// RequestID is a middleware that injects a request ID into the context of each
	// request. A request 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.
	RequestID = middleware.RequestID

	// RealIP is a middleware that sets a http.Request's RemoteAddr to the results
	// of parsing either the X-Forwarded-For header or the X-Real-IP header (in that
	// order).
	RealIP = middleware.RealIP

	// NoCache is a simple piece of middleware that sets a number of HTTP headers to prevent
	// a router (or subrouter) from being cached by an upstream proxy and/or client.
	NoCache = middleware.NoCache

	// SetContentType is a middleware that forces response Content-Type.
	SetContentType = render.SetContentType

	// GetReqID returns a request ID from the given context if one is present.
	// Returns the empty string if a request ID cannot be found.
	GetReqID = middleware.GetReqID

	// Status sets a HTTP response status code hint into request context at any point
	// during the request life-cycle. Before the Responder sends its response header
	// it will check the StatusCtxKey
	Status = render.Status

	// Heartbeat endpoint middleware useful to setting up a path like
	// `/ping` that load balancers or uptime testing external services
	// can make a request before hitting any routes. It's also convenient
	// to place this above ACL middlewares as well.
	Heartbeat = middleware.Heartbeat
)

Functions

func GetLogger

func GetLogger(r *http.Request) log.Logger

GetLogger returns the associated request logger

func InstrumentHandlerCounter

func InstrumentHandlerCounter(counter *prometheus.CounterVec, next http.Handler) http.HandlerFunc

InstrumentHandlerCounter is a middleware that wraps the provided http.Handler to observe the request result with the provided CounterVec. The CounterVec must have zero, one, or two non-const non-curried labels. For those, the only allowed label names are "code" and "method". The function panics otherwise. Partitioning of the CounterVec happens by HTTP status code and/or HTTP method if the respective instance label names are present in the CounterVec. For unpartitioned counting, use a CounterVec with zero labels.

If the wrapped Handler does not set a status code, a status code of 200 is assumed.

If the wrapped Handler panics, the Counter is not incremented.

See the example for InstrumentHandlerDuration for example usage.

func InstrumentHandlerDuration

func InstrumentHandlerDuration(obs prometheus.ObserverVec, next http.Handler) http.HandlerFunc

InstrumentHandlerDuration is a middleware that wraps the provided http.Handler to observe the request duration with the provided ObserverVec. The ObserverVec must have zero, one, or two non-const non-curried labels. For those, the only allowed label names are "code" and "method". The function panics otherwise. The Observe method of the Observer in the ObserverVec is called with the request duration in seconds. Partitioning happens by HTTP status code and/or HTTP method if the respective instance label names are present in the ObserverVec. For unpartitioned observations, use an ObserverVec with zero labels. Note that partitioning of Histograms is expensive and should be used judiciously.

If the wrapped Handler does not set a status code, a status code of 200 is assumed.

If the wrapped Handler panics, no values are reported.

Note that this method is only guaranteed to never observe negative durations if used with Go1.9+.

func InstrumentLabels

func InstrumentLabels(r *http.Request, keys ...string) prometheus.Labels

InstrumentLabels returns the instrument labels

func LiveReloader

func LiveReloader(next http.Handler) http.Handler

LiveReloader reloads a webpage

func Logger

func Logger(next http.Handler) http.Handler

Logger is a middleware that logs the start and end of each request, along with some useful data about what was requested, what the response status was, and how long it took to return.

func LoggerFields

func LoggerFields(r *http.Request) log.Map

LoggerFields returns the logger's fields

func LoggerWithOption

func LoggerWithOption(options ...LoggerOption) func(http.Handler) http.Handler

LoggerWithOption returns a logger middleware

func Metrics

func Metrics(next http.Handler) http.Handler

Metrics enables metrics for each request

func Recoverer

func Recoverer(next http.Handler) http.Handler

Recoverer is a middleware that recovers from panics, logs the panic (and a backtrace), and returns a HTTP 500 (Internal Server Error) status if possible. Recoverer prints a request ID if one is provided.

Alternatively, look at https://github.com/pressly/lg middleware pkgs.

Types

type ContextKey

type ContextKey struct {
	Name string
}

ContextKey is a value for use with context.WithValue. It's used as a pointer so it fits in an interface{} without allocation. This technique for defining context keys was copied from Go 1.7's new use of context in net/http.

func (*ContextKey) String

func (k *ContextKey) String() string

String returns the string key

type LoggerOption

type LoggerOption interface {
	Apply(logger log.Logger) log.Logger
}

LoggerOption represent a logger option

func LoggerOptionWithFields

func LoggerOptionWithFields(kv log.Map) LoggerOption

LoggerOptionWithFields creates a new logger option with fields

type LoggerOptionFunc

type LoggerOptionFunc func(logger log.Logger) log.Logger

LoggerOptionFunc represents a function

func (LoggerOptionFunc) Apply

func (fn LoggerOptionFunc) Apply(logger log.Logger) log.Logger

Apply applies the option

type Reloader

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

Reloader reloads the page

func NewReloader

func NewReloader() *Reloader

NewReloader creates a new reloader

func (*Reloader) ServeHTTP

func (l *Reloader) ServeHTTP(next http.Handler) http.Handler

ServeHTTP serves the reloader

Jump to

Keyboard shortcuts

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