Skip to content

Latest commit

 

History

History
46 lines (30 loc) · 1.85 KB

README.md

File metadata and controls

46 lines (30 loc) · 1.85 KB

gowww compress GoDoc Build Coverage Go Report Status Stable

Package 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:

    go get -u github.com/gowww/compress
  2. Import it in your code:

    import "github.com/gowww/compress"

Usage

To wrap an http.Handler, use Handle:

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, use HandleFunc:

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.