compress

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2026 License: MIT Imports: 7 Imported by: 0

README

Compress

Gzip and deflate compression middleware for HTTP responses.

Install

go get github.com/go-kruda/kruda/contrib/compress

Usage

import "github.com/go-kruda/kruda/contrib/compress"

// Auto-compress responses
app.Use(compress.New())

// Manual compression
compress.CompressText(c, "large text data")
compress.Compress(c, data, "application/json")

Config

Field Type Default Description
Level int 6 Compression level (1-9)
MinLength int 1024 Minimum response size
Types []string text/html, etc. MIME types to compress
Skipper func nil Skip compression function

Documentation

Overview

Package compress provides gzip and deflate response compression for Kruda.

Usage

import "github.com/go-kruda/kruda/contrib/compress"

app := kruda.New()
app.Use(compress.New())

app.Get("/api/data", func(c *kruda.Ctx) error {
    return compress.CompressText(c, "large response data...")
})

What it does

The middleware parses the request's Accept-Encoding header and stores the negotiated encoding (gzip or deflate) in the request context. Handlers then call Compress, CompressText, or CompressHTML to actually compress and send the body. Pooled gzip/flate writers (keyed by compression level) keep allocations low on the hot path.

Responses are skipped when:

  • The client did not send Accept-Encoding
  • A Content-Encoding response header is already set
  • The body is smaller than MinSize
  • The Content-Type matches an entry in ExcludedTypes

Configuration

  • MinSize: minimum body size before compression kicks in (default 1024)
  • Level: gzip/flate compression level (default -1 = stdlib default)
  • ExcludedTypes: Content-Type prefixes that skip compression (default "image/", "video/", "audio/")

See also

  • net/http "Accept-Encoding" semantics

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compress

func Compress(c *kruda.Ctx, data []byte, contentType string) error

Compress compresses a response if compression is requested and criteria are met. This is the main function - use CompressText/CompressHTML for convenience.

func CompressHTML

func CompressHTML(c *kruda.Ctx, html string) error

CompressHTML compresses an HTML response.

func CompressText

func CompressText(c *kruda.Ctx, text string) error

CompressText compresses a text response.

func New

func New(config ...Config) kruda.HandlerFunc

New creates a compression middleware with optional config. The middleware parses Accept-Encoding headers and stores compression preferences in the request context for use by Compress* functions.

Types

type Config

type Config struct {
	MinSize       int      // Minimum response size to compress (default: 1024)
	Level         int      // Compression level (default: -1 for default)
	ExcludedTypes []string // Content types to skip compression (default: image/, video/, audio/)
}

Config holds compression middleware configuration.

Jump to

Keyboard shortcuts

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