compress

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2026 License: MIT Imports: 12 Imported by: 2

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Compress

type Compress struct {
	New       func() Compressor
	Encoding  string // http Accept-Encoding, Content-Encoding value
	Vary      bool   // add Vary: Accept-Encoding
	Types     string // only compress for given types, * for all types
	MinLength int    // skip if Content-Length less than given value
}

Compress is the compress middleware

func Deflate

func Deflate() *Compress

Deflate creates new deflate compress middleware

func Gzip

func Gzip() *Compress

Gzip creates new gzip compress middleware

Example

Compress responses with gzip. The middleware negotiates with the client via Accept-Encoding and only kicks in for compressible content types above its MinLength threshold, so it's safe to mount unconditionally.

package main

import (
	"github.com/moonrhythm/parapet"
	"github.com/moonrhythm/parapet/pkg/compress"
)

func main() {
	s := parapet.New()
	s.Use(compress.Gzip())
	// s.Use(upstream.SingleHost(...)) — the handler whose responses get compressed.
}

func GzipWithLevel added in v0.12.5

func GzipWithLevel(level int) *Compress

func Zstd added in v0.16.0

func Zstd() *Compress

Zstd creates new zstd compress middleware

Example

Offer multiple encodings and let the client's Accept-Encoding pick. Each Compress self-skips when the client doesn't list its encoding or when the response is already encoded, so the INNERMOST middleware (the last Use, closest to the handler) compresses the response first and wins — the outer ones then see Content-Encoding already set and pass through. Order them least- to most-preferred: a client that accepts zstd gets zstd, else gzip, else deflate.

package main

import (
	"github.com/moonrhythm/parapet"
	"github.com/moonrhythm/parapet/pkg/compress"
)

func main() {
	s := parapet.New()
	s.Use(compress.Deflate()) // outermost: last-resort fallback
	s.Use(compress.Gzip())
	s.Use(compress.Zstd()) // innermost: runs first on the response, so zstd wins
}

func ZstdWithLevel added in v0.16.0

func ZstdWithLevel(level zstd.EncoderLevel) *Compress
Example

Tune the compressor: pick a stronger zstd level and tighten which responses get compressed. Types is a space-separated MIME allow-list ("*" for all), and MinLength skips bodies whose Content-Length is below the threshold.

package main

import (
	"github.com/klauspost/compress/zstd"

	"github.com/moonrhythm/parapet"
	"github.com/moonrhythm/parapet/pkg/compress"
)

func main() {
	m := compress.ZstdWithLevel(zstd.SpeedBetterCompression)
	m.Types = "text/html text/css application/json"
	m.MinLength = 1024 // don't bother compressing tiny payloads
	m.Vary = true      // add Vary: Accept-Encoding so caches key on it

	s := parapet.New()
	s.Use(m)
}

func (Compress) ServeHandler

func (m Compress) ServeHandler(h http.Handler) http.Handler

ServeHandler implements middleware interface

type Compressor

type Compressor interface {
	io.Writer
	io.Closer
	Reset(io.Writer)
	Flush() error
}

Compressor type

type Noop

type Noop struct{}

Noop middleware

func Br

func Br() *Noop

Br creates noop middleware

func BrWithQuality added in v0.12.5

func BrWithQuality(quality int) *Noop

func (*Noop) ServeHandler

func (m *Noop) ServeHandler(h http.Handler) http.Handler

ServeHandler implements middleware interface

Jump to

Keyboard shortcuts

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