Help us shape the future of Go! Take the 2026 Go Developer Survey

httpinfo

package module
v0.0.0-...-e2ee621 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2016 License: MIT Imports: 2 Imported by: 0

README

Go HTTPInfo Middleware

Simple http.Handler middleware that records HTTP status code, response size, and duration and makes the data available after ServeHTTP is finished. Requires no 3rd party dependencies.

For example:

func FooMiddleware(h http.Handler) http.Handler {
  return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

    // wrap the request and track the response status code, size and duration
    info := httpinfo.New(h)
    info.ServeHTTP(w, r)

    // log response
    fmt.Printf("Request: %s %s %s %d %d %d (%d) ", r.Method, r.RequestURI, r.Proto, info.ReqSize(), info.Status(), info.Size(), info.Elapsed())
  })
}

Install

go get github.com/mbrevoort/go-httpinfo

Run Tests

git clone https://github.com/mbrevoort/go-httpinfo && cd go-httpinfo
go test .

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HTTPInfo

type HTTPInfo interface {
	Header() http.Header
	Status() int
	Size() int
	ReqSize() int
	Elapsed() time.Duration
	ServeHTTP(w http.ResponseWriter, req *http.Request)
}

HTTPInfo interface

func New

func New(h http.Handler) HTTPInfo

New returns a new HTTPInfo

Simple http.Handler middleware that records HTTP status code, response size, and duration and makes the data available after `ServeHTTP` is finished. Requires no 3rd party dependencies.

For example:

func FooMiddleware(h http.Handler) http.Handler {
  return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

    // wrap the request and track the response status code, size and duration
    info := httpinfo.New(h)
    info.ServeHTTP(w, r)

    // log response
    fmt.Printf("Request: %s %s %s %d %d (%d) ", r.Method, r.RequestURI, r.Proto, info.Status(), info.Size(), info.Elapsed())
  })
}

Jump to

Keyboard shortcuts

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