Help us shape the future of Go! Take the 2026 Go Developer Survey

compress

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: MIT Imports: 20 Imported by: 0

README

Go Reference tests codecov GitHub release (latest SemVer) GitHub go.mod Go version

Compress

Compress is a middleware for Fox that compresses responses with zstd, brotli or gzip, depending on the client Accept-Encoding.

Disclaimer

The current API is not yet stabilized. Breaking changes may occur before v1.0.0 and will be noted on the release note.

Getting started

Installation
go get -u github.com/fox-toolkit/compress
Feature
  • zstd, brotli and gzip, or your own encoder.
  • Compress only bodies above a minimum size.
  • Allowlist or denylist of content types.
  • Choose the encoder order when the client has no preference.
  • Works with streaming responses, like server-sent events or a reverse proxy.
Usage
package main

import (
	"errors"
	"log"
	"net/http"
	"os"

	"github.com/fox-toolkit/compress"
	"github.com/fox-toolkit/fox"
)

func main() {
	mw, err := compress.Middleware(compress.WithMinSize(512))
	if err != nil {
		log.Fatalln(err)
	}

	f := fox.MustRouter(fox.WithMiddleware(mw))

	f.MustAdd(fox.MethodGet, "/*{filepath}", fox.WrapH(http.FileServerFS(os.DirFS("public"))))

	if err := http.ListenAndServe(":8080", f); err != nil && !errors.Is(err, http.ErrServerClosed) {
		log.Fatalln(err)
	}
}

Documentation

Index

Constants

View Source
const DefaultMinSize = 1024

DefaultMinSize is the default minimum body size to compress.

Variables

View Source
var ErrInvalidConfig = errors.New("invalid config")

Functions

func Middleware

func Middleware(opts ...Option) (fox.MiddlewareFunc, error)

Middleware returns a fox.MiddlewareFunc that compresses responses with the encoding preferred by the client, among the configured encoders.

Types

type Encoder

type Encoder interface {
	// Encoding returns the encoding name, such as "gzip".
	Encoding() string
	// NewWriter returns a new [Writer] that writes to w.
	// Internally, writers are pooled.
	NewWriter(w io.Writer) Writer
}

Encoder creates the Writer for a content encoding.

func Brotli

func Brotli(level int) Encoder

Brotli returns an Encoder for the br encoding. The level ranges from 0 to 11.

func Gzip

func Gzip(level int) Encoder

Gzip returns an Encoder for the gzip encoding. The level ranges from -3 (stateless) to 9.

func Zstd

func Zstd(level int) Encoder

Zstd returns an Encoder for the zstd encoding. The level follows the zstd scale, from 1 to 22.

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option configures the middleware.

func WithEncoders

func WithEncoders(enc ...Encoder) Option

WithEncoders sets the encoders in order of preference. A preference expressed by the client takes precedence over this order. The default is zstd, br then gzip.

func WithExcludedContentTypes

func WithExcludedContentTypes(types ...string) Option

WithExcludedContentTypes never compresses the given media types, in addition to the default ones (already compressed formats and gRPC). A "type/*" entry matches any subtype. It cannot be combined with WithIncludedContentTypes.

func WithIncludedContentTypes

func WithIncludedContentTypes(types ...string) Option

WithIncludedContentTypes compresses only the given media types. A "type/*" entry matches any subtype. It cannot be combined with WithExcludedContentTypes.

func WithMinSize

func WithMinSize(n int) Option

WithMinSize sets the minimum body size to compress. Smaller bodies are sent as is. The default is DefaultMinSize.

type Writer

type Writer interface {
	io.WriteCloser
	// Flush writes any pending compressed data to the underlying writer.
	Flush() error
	// Reset discards the writer state and makes it write to w. The middleware calls Reset(nil) before
	// returning a writer to its pool.
	Reset(w io.Writer)
}

Writer is a resettable compressor.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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