https://github.com/gowww/compress
🔬 A clever gzip compressing handler
https://github.com/gowww/compress
compress go golang gzip handler http middleware response
Last synced: 28 days ago
JSON representation
🔬 A clever gzip compressing handler
- Host: GitHub
- URL: https://github.com/gowww/compress
- Owner: gowww
- License: mit
- Created: 2016-03-26T11:23:37.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2020-09-11T17:34:41.000Z (over 5 years ago)
- Last Synced: 2025-08-14T06:02:10.450Z (8 months ago)
- Topics: compress, go, golang, gzip, handler, http, middleware, response
- Language: Go
- Homepage:
- Size: 23.4 KB
- Stars: 11
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# [](https://github.com/gowww) compress [](https://godoc.org/github.com/gowww/compress) [](https://travis-ci.org/gowww/compress) [](https://coveralls.io/github/gowww/compress?branch=master) [](https://goreportcard.com/report/github.com/gowww/compress) 
Package [compress](https://godoc.org/github.com/gowww/compress) provides a clever gzip compressing handler.
It takes care to not handle small contents, or contents that are already compressed (like JPEG, MPEG or PDF).
Trying to gzip them not only wastes CPU but can potentially increase the response size.
## Installing
1. Get package:
```Shell
go get -u github.com/gowww/compress
```
2. Import it in your code:
```Go
import "github.com/gowww/compress"
```
## Usage
To wrap an [http.Handler](https://golang.org/pkg/net/http/#Handler), use [Handle](https://godoc.org/github.com/gowww/compress#Handle):
```Go
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello")
})
http.ListenAndServe(":8080", compress.Handle(handler))
```
To wrap an [http.HandlerFunc](https://golang.org/pkg/net/http/#HandlerFunc), use [HandleFunc](https://godoc.org/github.com/gowww/compress#HandleFunc):
```Go
http.Handle("/", compress.HandleFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello")
}))
http.ListenAndServe(":8080", nil)
```
All in all, make sure to include this handler above any other handler that may write the response.