compress

package
v2.0.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package compress provides response-compression middleware for httpsrv (New(config) -> Handler).

Register it with Use; it compresses responses with gzip or brotli, chosen from the request's Accept-Encoding (brotli preferred, then gzip):

import "github.com/hooto/httpsrv/v2"
"github.com/hooto/httpsrv/v2/middleware/compress"

app.Use(compress.New())
// or: app.Use(compress.New(compress.Config{Level: compress.LevelBestSpeed}))

It wraps the response writer, sets Content-Encoding (and Vary), and removes any Content-Length the handler may have set. All response bodies are compressed; there is no content-type or minimum-size filter.

Index

Constants

View Source
const (
	LevelDefault         = iota // 0: gzip DefaultCompression, brotli quality 5
	LevelBestSpeed              // 1: gzip BestSpeed, brotli BestSpeed
	LevelBestCompression        // 2: gzip BestCompression, brotli BestCompression
)

Compression levels. They are an enum (not raw gzip/brotli levels); New maps each to the appropriate encoder level.

Variables

View Source
var ConfigDefault = Config{
	Level: LevelDefault,
}

ConfigDefault is the default configuration (LevelDefault, no Next).

Functions

func New

func New(config ...Config) httpsrv.Handler

New returns compression middleware. It compresses responses with brotli (preferred) or gzip based on Accept-Encoding.

Types

type Config

type Config struct {
	// Next optionally skips this middleware when it returns true.
	Next func(c httpsrv.Ctx) bool

	// Level is the compression level: LevelDefault (default), LevelBestSpeed,
	// or LevelBestCompression. Defaults to LevelDefault.
	Level int
}

Config configures the compression middleware.

Jump to

Keyboard shortcuts

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