# README
gorilla/handlers
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:
- LoggingHandler for logging HTTP requests in the Apache Common Log Format.
- CombinedLoggingHandler for logging HTTP requests in the Apache Combined Log Format commonly used by both Apache and nginx.
- CompressHandler for gzipping responses.
- ContentTypeHandler for validating requests against a list of accepted content types.
- MethodHandler for matching HTTP methods against handlers in a
map[string]http.Handler
- ProxyHeaders for populating
r.RemoteAddr
andr.URL.Scheme
based on theX-Forwarded-For
,X-Real-IP
,X-Forwarded-Proto
and RFC7239Forwarded
headers when running a Go server behind a HTTP reverse proxy. - CanonicalHost for re-directing to the preferred host when handling multiple domains (i.e. multiple CNAME aliases).
- RecoveryHandler for recovering from unexpected panics.
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.
# Functions
AllowCredentials can be used to specify that the user agent may pass authentication details along with the request.
AllowedHeaders adds the provided headers to the list of allowed headers in a CORS request.
AllowedMethods can be used to explicitly allow methods in the Access-Control-Allow-Methods header.
AllowedOrigins sets the allowed origins for CORS requests, as used in the 'Allow-Access-Control-Origin' HTTP header.
AllowedOriginValidator sets a function for evaluating allowed origins in CORS requests, represented by the 'Allow-Access-Control-Origin' HTTP header.
CanonicalHost is HTTP middleware that re-directs requests to the canonical domain.
CombinedLoggingHandler return a http.Handler that wraps h and logs requests to out in Apache Combined Log Format.
CompressHandler gzip compresses HTTP responses for clients that support it via the 'Accept-Encoding' header.
CompressHandlerLevel gzip compresses HTTP responses with specified compression level for clients that support it via the 'Accept-Encoding' header.
ContentTypeHandler wraps and returns a http.Handler, validating the request content type is compatible with the contentTypes list.
CORS provides Cross-Origin Resource Sharing middleware.
CustomLoggingHandler provides a way to supply a custom log formatter while taking advantage of the mechanisms in this package.
ExposedHeaders can be used to specify headers that are available and will not be stripped out by the user-agent.
HTTPMethodOverrideHandler wraps and returns a http.Handler which checks for the X-HTTP-Method-Override header or the _method form key, and overrides (if valid) request.Method with its value.
IgnoreOptions causes the CORS middleware to ignore OPTIONS requests, instead passing them through to the next handler.
LoggingHandler return a http.Handler that wraps h and logs requests to out in Apache Common Log Format (CLF).
MaxAge determines the maximum age (in seconds) between preflight requests.
OptionStatusCode sets a custom status code on the OPTIONS requests.
PrintRecoveryStack is a functional option to enable or disable printing stack traces on panic.
ProxyHeaders inspects common reverse proxy headers and sets the corresponding fields in the HTTP request struct.
RecoveryHandler is HTTP middleware that recovers from a panic, logs the panic, writes http.StatusInternalServerError, and continues to the next handler.
RecoveryLogger is a functional option to override the default logger.
# Constants
HTTPMethodOverrideFormKey is a commonly used HTML form key to override a request method.
HTTPMethodOverrideHeader is a commonly used http header to override a request method.
# Structs
LogFormatterParams is the structure any formatter will be handed when time to log comes.
# Interfaces
RecoveryHandlerLogger is an interface used by the recovering handler to print logs.
# Type aliases
CORSOption represents a functional option for configuring the CORS middleware.
LogFormatter gives the signature of the formatter function passed to CustomLoggingHandler.
MethodHandler is an http.Handler that dispatches to a handler whose key in the MethodHandler's map matches the name of the HTTP request's method, eg: GET
If the request's method is OPTIONS and OPTIONS is not a key in the map then the handler responds with a status of 200 and sets the Allow header to a comma-separated list of available methods.
OriginValidator takes an origin string and returns whether or not that origin is allowed.
RecoveryOption provides a functional approach to define configuration for a handler; such as setting the logging whether or not to print stack traces on panic.