Documentation ¶
Overview ¶
Package middleware provides http.Handler functions for common server middleware tasks.
Basic Usage
// Create a logger middleware using the default request logger and recovery stack := middleware.Stack( middleware.Logger(middleware.DefaultRequestLogger), middleware.Recovery(log.Default().Writer()), ) // Wrap the fooHandler in the logger middleware. http.Handle("/foo", stack(fooHandler))
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type MiddlewareFunc ¶
MiddlewareFunc defines a standard middleware pattern. It takes a http.Handler as an argument and returns a new http.Handler. The returned handler will be responsible for processing the request and calling the supplied handler to continue the chain.
func Logger ¶
func Logger(logger RequestLogger) MiddlewareFunc
Logger performs the next handler then calls RequestLogger.Log with the response status code, runtime of the sub-handler, and a clone of the original http.Request.
func Recovery ¶
func Recovery(w io.Writer) MiddlewareFunc
Recovery provides a panic handler and logs panic details to the passed io.Writer. Log message will be JSON printed to a single line with keys "panic" and "stackTrace". The stackTrace key is a Base64 encoded stack trace.
func RecoveryWithLogger ¶ added in v1.6.0
func RecoveryWithLogger(logger SLogger) MiddlewareFunc
func ResponseRuntime ¶ added in v1.3.1
func ResponseRuntime(header string) MiddlewareFunc
ResponseRuntime will include a request duration header response value.
func Stack ¶
func Stack(in ...MiddlewareFunc) MiddlewareFunc
Stack returns the middleware functions setup as a single MiddlewareFunc. The middlewares will be called in the order they were added.
type RequestLogger ¶
type RequestLogger interface { // Log received the HTTP status code, duration of the sub-handler, and a copy of the http.Request Log(int, time.Duration, *http.Request) }
RequestLogger is used to generate a log message for a request.
var DefaultRequestLogger RequestLogger = new(dRequestLogger)
DefaultRequestLogger provides a simple log message written to log.Default()
func NewStructuredRequestLogger ¶ added in v1.4.0
func NewStructuredRequestLogger(l SLogger) RequestLogger
type ResponseWriter ¶ added in v1.1.0
type ResponseWriter struct { StatusCode int Headers http.Header BodyBuffer *bytes.Buffer // contains filtered or unexported fields }
func NewResponseWriter ¶ added in v1.1.0
func NewResponseWriter() *ResponseWriter
func (*ResponseWriter) Apply ¶ added in v1.1.0
func (r *ResponseWriter) Apply(w http.ResponseWriter)
func (*ResponseWriter) Header ¶ added in v1.1.0
func (r *ResponseWriter) Header() http.Header
func (*ResponseWriter) Write ¶ added in v1.1.0
func (r *ResponseWriter) Write(b []byte) (int, error)
func (*ResponseWriter) WriteHeader ¶ added in v1.1.0
func (r *ResponseWriter) WriteHeader(code int)