gzip

package
v1.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 29, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package gzip provides an HTTP middleware for compressing response bodies using the gzip algorithm. It automatically adds the "Content-Encoding: gzip" header and compresses the payload for clients that support it (indicated by the "Accept-Encoding" request header).

Usage

The middleware is designed to be efficient. It pools gzip writers to reduce memory allocations and gracefully skips compression for responses tha already have a "Content-Encoding" header set.

Example:

// Create the final handler.
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/plain")
	w.Write([]byte("This is a long string that will be compressed."))
})

// Create a gzip middleware pipe with the highest level if compression.
pipe := gzip.New(
	gzip.WithCompressionLevel(gzip.BestCompression),
	gzip.WithExcludeMimeTypes("text/*", "application/font-woff"),
)

// Apply the CORS middleware as one of the first layers.
chainedHandler := middleware.Chain(handler, pipe)

http.ListenAndServe(":8080", chainedHandler)

Index

Constants

View Source
const (
	BestCompression    = gzip.BestCompression
	BestSpeed          = gzip.BestSpeed
	DefaultCompression = gzip.DefaultCompression
	NoCompression      = gzip.NoCompression
)

Mirror constants from the compress/gzip package for easy access without requiring an extra import.

Variables

This section is empty.

Functions

func New

func New(opts ...Option) middleware.Pipe

New creates a middleware Pipe that compresses HTTP responses using gzip with the specified options.

The middleware is a no-op if the client does not send an Accept-Encoding header including "gzip" or if the response already has a non-empty Content-Encoding header. It adds the "Vary: Accept-Encoding" header to responses to prevent cache poisoning.

Types

type Option

type Option func(*config)

Option is a function that configures the middleware.

func WithCompressionLevel

func WithCompressionLevel(level int) Option

WithCompressionLevel sets the compression level. It accepts values ranging from BestSpeed (1) to BestCompression (9). For other values, it will fall back to DefaultCompression, a good balance between speed and compression ratio.

func WithExcludeMimeTypes

func WithExcludeMimeTypes(types ...string) Option

WithExcludeMimeTypes adds MIME types to the list of content types that should not be compressed. This option is additive and can be called multiple times; it appends to the default exclusion list rather than replacing it.

The matching logic supports two formats:

  • Exact: Provide the full MIME type (e.g., "application/pdf").
  • Prefix: End the MIME type with a wildcard "*" (e.g., "image/*") to exclude all subtypes for that primary type.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL