| 1 |
# gorilla/handlers |
| 2 |
|
| 3 |
 |
| 4 |
[](https://codecov.io/github/gorilla/handlers) |
| 5 |
[](https://godoc.org/github.com/gorilla/handlers) |
| 6 |
[](https://sourcegraph.com/github.com/gorilla/handlers?badge) |
| 7 |
|
| 8 |
Package handlers is a collection of handlers (aka "HTTP middleware") for use |
| 9 |
with Go's `net/http` package (or any framework supporting `http.Handler`), including: |
| 10 |
|
| 11 |
* [**LoggingHandler**](https://godoc.org/github.com/gorilla/handlers#LoggingHandler) for logging HTTP requests in the Apache [Common Log |
| 12 |
Format](http://httpd.apache.org/docs/2.2/logs.html#common). |
| 13 |
* [**CombinedLoggingHandler**](https://godoc.org/github.com/gorilla/handlers#CombinedLoggingHandler) for logging HTTP requests in the Apache [Combined Log |
| 14 |
Format](http://httpd.apache.org/docs/2.2/logs.html#combined) commonly used by |
| 15 |
both Apache and nginx. |
| 16 |
* [**CompressHandler**](https://godoc.org/github.com/gorilla/handlers#CompressHandler) for gzipping responses. |
| 17 |
* [**ContentTypeHandler**](https://godoc.org/github.com/gorilla/handlers#ContentTypeHandler) for validating requests against a list of accepted |
| 18 |
content types. |
| 19 |
* [**MethodHandler**](https://godoc.org/github.com/gorilla/handlers#MethodHandler) for matching HTTP methods against handlers in a |
| 20 |
`map[string]http.Handler` |
| 21 |
* [**ProxyHeaders**](https://godoc.org/github.com/gorilla/handlers#ProxyHeaders) for populating `r.RemoteAddr` and `r.URL.Scheme` based on the |
| 22 |
`X-Forwarded-For`, `X-Real-IP`, `X-Forwarded-Proto` and RFC7239 `Forwarded` |
| 23 |
headers when running a Go server behind a HTTP reverse proxy. |
| 24 |
* [**CanonicalHost**](https://godoc.org/github.com/gorilla/handlers#CanonicalHost) for re-directing to the preferred host when handling multiple |
| 25 |
domains (i.e. multiple CNAME aliases). |
| 26 |
* [**RecoveryHandler**](https://godoc.org/github.com/gorilla/handlers#RecoveryHandler) for recovering from unexpected panics. |
| 27 |
|
| 28 |
Other handlers are documented [on the Gorilla |
| 29 |
website](https://www.gorillatoolkit.org/pkg/handlers). |
| 30 |
|
| 31 |
## Example |
| 32 |
|
| 33 |
A simple example using `handlers.LoggingHandler` and `handlers.CompressHandler`: |
| 34 |
|
| 35 |
```go |
| 36 |
import ( |
| 37 |
"net/http" |
| 38 |
"github.com/gorilla/handlers" |
| 39 |
) |
| 40 |
|
| 41 |
func main() { |
| 42 |
r := http.NewServeMux() |
| 43 |
|
| 44 |
// Only log requests to our admin dashboard to stdout |
| 45 |
r.Handle("/admin", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(ShowAdminDashboard))) |
| 46 |
r.HandleFunc("/", ShowIndex) |
| 47 |
|
| 48 |
// Wrap our server with our gzip handler to gzip compress all responses. |
| 49 |
http.ListenAndServe(":8000", handlers.CompressHandler(r)) |
| 50 |
} |
| 51 |
``` |
| 52 |
|
| 53 |
## License |
| 54 |
|
| 55 |
BSD licensed. See the included LICENSE file for details. |
| 56 |
|