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 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.
}
Output:
func GzipWithLevel ¶ added in v0.12.5
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
}
Output:
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)
}
Output:
type Compressor ¶
Compressor type