Categorygithub.com/NYTimes/gziphandler
modulepackage
1.1.1
Repository: https://github.com/nytimes/gziphandler.git
Documentation: pkg.go.dev

# README

Gzip Handler

This is a tiny Go package which wraps HTTP handlers to transparently gzip the response body, for clients which support it. Although it's usually simpler to leave that to a reverse proxy (like nginx or Varnish), this package is useful when that's undesirable.

Install

go get -u github.com/NYTimes/gziphandler

Usage

Call GzipHandler with any handler (an object which implements the http.Handler interface), and it'll return a new handler which gzips the response. For example:

package main

import (
	"io"
	"net/http"
	"github.com/NYTimes/gziphandler"
)

func main() {
	withoutGz := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "text/plain")
		io.WriteString(w, "Hello, World")
	})

	withGz := gziphandler.GzipHandler(withoutGz)

	http.Handle("/", withGz)
	http.ListenAndServe("0.0.0.0:8000", nil)
}

Documentation

The docs can be found at godoc.org, as usual.

License

Apache 2.0.

# Functions

ContentTypes specifies a list of content types to compare the Content-Type header to before compressing.
GzipHandler wraps an HTTP handler, to transparently gzip the response body if the client supports it (via the Accept-Encoding header).
MustNewGzipLevelHandler behaves just like NewGzipLevelHandler except that in an error case it panics rather than returning an error.
NewGzipLevelAndMinSize behave as NewGzipLevelHandler except it let the caller specify the minimum size before compression.
NewGzipLevelHandler returns a wrapper function (often known as middleware) which can be used to wrap an HTTP handler to transparently gzip the response body if the client supports it (via the Accept-Encoding header).

# Constants

DefaultMinSize is the default minimum size until we enable gzip compression.
DefaultQValue is the default qvalue to assign to an encoding if no explicit qvalue is set.

# Structs

GzipResponseWriter provides an http.ResponseWriter interface, which gzips bytes before writing them to the underlying response.