middleware

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2022 License: MIT Imports: 16 Imported by: 3

README

a collection of net/http middleware

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetRequestID added in v0.3.1

func GetRequestID(r *http.Request) (string, bool)

GetRequestID fetches the unique request ID from a request.

Types

type Logger

type Logger interface {
	// LogRequest will log a handled HTTP request along with the handler's response code and latency.
	LogRequest(r *http.Request, code int, latency time.Duration)

	// LogHTTP will log the error response of a handled HTTP request including status, code and the causal error.
	LogHTTPError(r *http.Request, status string, code int, err error)

	// LogPanic will log a panic caught when handling an HTTP request including recovered panic info and stack frames.
	LogPanic(r *http.Request, i interface{}, stack *runtime.Frames)
}

Logger is a log interface used by middleware in its various handlers. This allows your log implementation of choice to work with our middleware handlers.

type Middleware added in v0.2.1

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

func New added in v1.0.0

func New(logger Logger) *Middleware

New returns a new instance of Middleware, using log.Default() is Logger is nil.

func (*Middleware) HTTPError added in v0.2.1

func (m *Middleware) HTTPError(rw http.ResponseWriter, r *http.Request, msg string, code int, err error)

HTTPError handles responding to a client with an HTTP error code and logs the provided error.

func (*Middleware) Logger added in v0.2.1

func (m *Middleware) Logger(handler http.Handler) http.Handler

Logger adds request logging to the provided HTTP handler.

func (*Middleware) Recovery added in v0.2.1

func (m *Middleware) Recovery(handler http.Handler) http.Handler

Recovery adds panic recovery (and panic logging) to the provided HTTP handler.

func (*Middleware) RequestID added in v0.3.1

func (m *Middleware) RequestID(handler http.Handler) http.Handler

RequestID provides setting unique IDs per request and adding a hook to the logger to append these to log entries using the request's context. This should be placed at the TOP of handler stack.

func (*Middleware) Timeout added in v0.3.0

func (m *Middleware) Timeout(handler http.Handler, fallback time.Duration) http.Handler

Timeout adds user defined request timeouts to provided handler, either by "Request-Timeout" header or the given fallback value. Please note that this handler also provides logging so do not wrap within Middleware.Logger().

type StdLogger added in v1.0.0

type StdLogger log.Logger

StdLogger is a typedef to allow the standard library "log" Logger to implement the middleware.Logger interface.

func (*StdLogger) LogHTTPError added in v1.0.0

func (l *StdLogger) LogHTTPError(r *http.Request, status string, code int, err error)

func (*StdLogger) LogPanic added in v1.0.0

func (l *StdLogger) LogPanic(r *http.Request, i interface{}, stack *runtime.Frames)

func (*StdLogger) LogRequest added in v1.0.0

func (l *StdLogger) LogRequest(r *http.Request, code int, latency time.Duration)

type TimeoutResponseWriter added in v0.3.0

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

TimeoutResponseWriter wraps an http.ResponseWriter to add request timeout capabilities.

func (*TimeoutResponseWriter) Cancel added in v0.3.0

func (rw *TimeoutResponseWriter) Cancel() bool

Cancel will attempt to cancel the timeout counter.

func (*TimeoutResponseWriter) Header added in v0.3.0

func (rw *TimeoutResponseWriter) Header() http.Header

Header implements http.ResponseWriter's .Header().

func (*TimeoutResponseWriter) ResponseWriter added in v0.3.0

func (rw *TimeoutResponseWriter) ResponseWriter() http.ResponseWriter

ResponseWriter returns access to the underlying http.ResponseWriter.

func (*TimeoutResponseWriter) Status added in v0.3.0

func (rw *TimeoutResponseWriter) Status() int

Status returns the set response status code.

func (*TimeoutResponseWriter) TimedOut added in v0.3.0

func (rw *TimeoutResponseWriter) TimedOut() bool

TimedOut returns whether this TimeoutResponseWriter has timed out.

func (*TimeoutResponseWriter) Write added in v0.3.0

func (rw *TimeoutResponseWriter) Write(b []byte) (int, error)

Write implements http.ResponseWriter's .Write().

func (*TimeoutResponseWriter) WriteHeader added in v0.3.0

func (rw *TimeoutResponseWriter) WriteHeader(status int)

WriteHeader implements http.ResponseWriter's .WriteHeader().

Jump to

Keyboard shortcuts

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