Skip to content
Package httpmw is a collection of bite-sized middleware with chaining support.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
augmentedrw
basicauth
bodylimit
cleanpath
cors
headers
logrequest
misc
ratelimit
recover
remoteip
requestid
stripprefix
timeout
.gitignore
LICENSE
README.md
TODO
httpmw.go
httpmw_test.go
logger.go
logger_test.go

README.md

httpmw GoDoc Build Status

Package httpmw is a collection of bite-sized middleware with chaining support. Uses the standard library's http.Handler interface. It's 🐢 🐢 🐢 all the way down. See the godoc for full documentation.

Installation

$ go get github.com/PuerkitoBio/httpmw/...

Use -u to update, -t to install test dependencies.

Example

func myHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "hello, middleware...")
}

// add a random request ID to all requests, with default configuration
var rid requestid.RequestID

// extract the real client remote address, with default configuration
var ra remoteip.RemoteIP

// limit the request body size to 1024 bytes
bl := bodylimit.BodyLimit{N: 1024}

// add CSRF support - this uses the github.com/gorilla/csrf external
// package, but it is very easy to adapt any http.Handler-compliant
// middleware out there. csrf.Protect returns a middleware-friendly
// `func(http.Handler) http.Handler`, which can be adapted to an
// httpmw.Wrapper using httpmw.WrapperFunc (much like http.Handler/
// http.HandlerFunc).
protect := httpmw.WrapperFunc(csrf.Protect([]byte(/* the secret */)))

h := httpmw.Wrap(http.HandlerFunc(myHandler), &rid, &ra, &bl, protect)

// serve using the middleware-augmented handler
log.Fatal(http.ListenAndServe(":9000", h))

License

The BSD 3-clause license, see LICENSE file.

You can’t perform that action at this time.