httpmw

package module
v0.0.0-...-363c1d9 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2016 License: BSD-3-Clause Imports: 3 Imported by: 2

README

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.

Documentation

Overview

Package httpmw supports creating middleware chains for HTTP handlers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Wrap

func Wrap(h http.Handler, ws ...Wrapper) http.Handler

Wrap wraps the HTTP handler h with the provided middleware ws. It returns a handler that will call ws[0] -> ws[1] -> ... -> ws[n-1] -> h. Each Wrapper may stop the chain of calls by not calling the next handler in the chain.

Types

type Logger

type Logger interface {
	Log(...interface{}) error
}

Logger defines the Log method that is used to log structured data, in tuples of alternating keys/values. The go-kit logger satisfies this interface (github.com/go-kit/kit/log).

type PrintfLogger

type PrintfLogger func(string, ...interface{})

PrintfLogger is an adapter to use Printf-style functions as a Logger in the middlewares that accept one. For example, the stdlib's log.Printf function can be used via this adapter.

func (PrintfLogger) Log

func (fn PrintfLogger) Log(args ...interface{}) error

Log implements Logger for the PrintfLogger function adapter.

type StatusHandler

type StatusHandler int

StatusHandler is an integer that handles HTTP requests by writing itself as status code. No body is sent.

func (StatusHandler) ServeHTTP

func (s StatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler for the StatusHandler.

type Wrapper

type Wrapper interface {
	Wrap(http.Handler) http.Handler
}

Wrapper defines the Wrap method required to build a middleware-style chain of calls.

type WrapperFunc

type WrapperFunc func(http.Handler) http.Handler

WrapperFunc is a function type that implements the Wrapper interface, useful to adapt middleware from other packages.

func (WrapperFunc) Wrap

func (fn WrapperFunc) Wrap(h http.Handler) http.Handler

Wrap implements the Wrapper interface for a WrapperFunc by calling the function with h as argument.

Directories

Path Synopsis
Package augmentedrw implements a middleware that replaces the standard http.ResponseWriter with one that records the Size and Status of the response.
Package augmentedrw implements a middleware that replaces the standard http.ResponseWriter with one that records the Size and Status of the response.
Package basicauth implements a basic authentication middleware.
Package basicauth implements a basic authentication middleware.
Package bodylimit implements an HTTP middleware that limits the number of bytes that can be read from the request body.
Package bodylimit implements an HTTP middleware that limits the number of bytes that can be read from the request body.
Package cleanpath implements a middleware that cleans the requested path to a canonical form.
Package cleanpath implements a middleware that cleans the requested path to a canonical form.
Package cors implements a CORS middleware and a handler for OPTIONS requests.
Package cors implements a CORS middleware and a handler for OPTIONS requests.
Package headers defines a middleware that adds static headers to the requests.
Package headers defines a middleware that adds static headers to the requests.
Package logrequest implements a middleware that logs requests.
Package logrequest implements a middleware that logs requests.
Package ratelimit implements a rate limiter middleware handler.
Package ratelimit implements a rate limiter middleware handler.
Package recover implements a middleware that recovers from panics.
Package recover implements a middleware that recovers from panics.
Package remoteip implements a middleware that extracts the effective remote client IP address and sets it on the request's RemoteAddr field.
Package remoteip implements a middleware that extracts the effective remote client IP address and sets it on the request's RemoteAddr field.
Package requestid implements a middleware that generates a random request ID.
Package requestid implements a middleware that generates a random request ID.
Package stripprefix implements a middleware handler that strips a prefix from the request URL's Path.
Package stripprefix implements a middleware handler that strips a prefix from the request URL's Path.
Package timeout implements a middleware that returns a 503 error if the request takes too long to execute.
Package timeout implements a middleware that returns a 503 error if the request takes too long to execute.

Jump to

Keyboard shortcuts

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