mw: Index | Examples | Files | Directories

package mw

import ""

Package mw is a collection of various Go middlewares. For details about any non-trivial middlewares, refer to the documentation of the corresponding sub-package.

Feel free to open issues and/or features requests on project's GitHub page at


// Sample application that combines all included
// middlewares together into a single stack
app := http.NotFoundHandler()

hs := map[string]string{
    "Content-Type": "application/json; charset=utf-8",

a := alice.New(

http.ListenAndServe(":1234", a)



Package Files

chaotic.go gzip.go headers.go logger.go mw.go recover.go util.go

func Chaotic Uses

func Chaotic(url string) func(h http.Handler) http.Handler

Chaotic middleware allows to inject configurable delays and failures into the requests processed by its underlying HTTP stack. It provides a configuration page, mounted under the specified URL.


	// wrap application handler with chaotic.H,
	// installing its pages under /chaotic URL

Read chaotic package documentation for more details: on github (, or on godoc (

func Gzip Uses

func Gzip(h http.Handler) http.Handler

Gzip middleware turns response writer w into a gzip response writer if the appropriate Accept-Encoding headers are present

func Headers Uses

func Headers(hs map[string]string) func(h http.Handler) http.Handler

Headers to be written into all responses from the application. Example:

hs := map[string]string{
    "Content-Type":   "application/json; charset=utf-8",
http.ListenAndServe(":1234", mw.Headers(hs)(app))

func Logger Uses

func Logger(h http.Handler) http.Handler

Logger middleware writes all incoming requests into stdout using the format:

<time>\t<ip>\t"<method> <URI>"\t<runtime>


2015/02/01 19:19:31     "GET /Ping"     31.376µs

IP address prefers X-Real-IP or X-Forwarded-For headers, if present, or falls back to http.Request's RemoteAddr otherwise.

func Recover Uses

func Recover(h http.Handler) http.Handler

Recover from panic when it happened in the application. Details of the panic are written into Stderr, together with some information about the request- client's real IP address, HTTP method and URL path.


chaoticPackage chaotic provides stdlib-compatible middleware to inject configurable delays and failures into the requests processed by its underlying HTTP stack.

Package mw imports 8 packages (graph) and is imported by 2 packages. Updated 2016-07-23. Refresh now. Tools for package owners.