A collection of useful handlers for Go's net/http package.
Clone or download
Permalink
Failed to load latest commit information.
.travis.yml Travis go18 (#106) Jun 2, 2017
LICENSE Add LICENSE. Jan 30, 2015
README.md README.md: Update site URL Oct 12, 2018
canonical.go Keep query string when redirecting to CanoncialHost Feb 1, 2016
canonical_test.go Keep query string when redirecting to CanoncialHost Feb 1, 2016
compress.go [docs] Add note about the CompressHandler and TLS traffic (#90) Oct 28, 2016
compress_test.go Validate CompressHandler preserves http.{Hijacker,Flusher,CloseNotifi… Feb 22, 2016
cors.go [cors] add OPTIONS status code + fix function typo (#132) Sep 14, 2018
cors_test.go [cors] add OPTIONS status code + fix function typo (#132) Sep 14, 2018
doc.go Moved the package godoc into a separate doc.go file. Expanded on it. Jul 4, 2015
handlers.go added ability to register custom log formatter (#131) Jul 27, 2018
handlers_go18.go (Combined)LoggingHandler: Add Go 1.8 http.Pusher support Feb 9, 2017
handlers_go18_test.go (Combined)LoggingHandler: Add tests for http.Push support Feb 9, 2017
handlers_pre18.go (Combined)LoggingHandler: Add Go 1.8 http.Pusher support Feb 9, 2017
handlers_test.go added ability to register custom log formatter (#131) Jul 27, 2018
logging.go added ability to register custom log formatter (#131) Jul 27, 2018
logging_test.go added ability to register custom log formatter (#131) Jul 27, 2018
proxy_headers.go Add ProxyHeaders support for X-Forwarded-Proto. Jul 21, 2016
proxy_headers_test.go Add ProxyHeaders support for X-Forwarded-Proto. Jul 21, 2016
recovery.go Use logger interface in recovery handler (#92) Dec 6, 2016
recovery_test.go Moved configuration to functional options Oct 28, 2015

README.md

gorilla/handlers

GoDoc Build Status Sourcegraph

Package handlers is a collection of handlers (aka "HTTP middleware") for use with Go's net/http package (or any framework supporting http.Handler), including:

Other handlers are documented on the Gorilla website.

Example

A simple example using handlers.LoggingHandler and handlers.CompressHandler:

import (
    "net/http"
    "github.com/gorilla/handlers"
)

func main() {
    r := http.NewServeMux()

    // Only log requests to our admin dashboard to stdout
    r.Handle("/admin", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(ShowAdminDashboard)))
    r.HandleFunc("/", ShowIndex)

    // Wrap our server with our gzip handler to gzip compress all responses.
    http.ListenAndServe(":8000", handlers.CompressHandler(r))
}

License

BSD licensed. See the included LICENSE file for details.