Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compress: allow config level #145

Merged
merged 2 commits into from
Jun 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions pkg/compress/br.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@ import (

// Br creates new brotli compress middleware
func Br() *Compress {
return BrWithOption(cbrotli.WriterOptions{Quality: 4})
}

func BrWithQuality(quality int) *Compress {
return BrWithOption(cbrotli.WriterOptions{Quality: quality})
}

func BrWithOption(opt cbrotli.WriterOptions) *Compress {
return &Compress{
New: func() Compressor {
return &brWriter{quality: 4}
return &brWriter{opt: &opt}
},
Encoding: "br",
Vary: defaultCompressVary,
Expand All @@ -22,10 +30,11 @@ func Br() *Compress {
}

type brWriter struct {
quality int
*cbrotli.Writer

opt *cbrotli.WriterOptions
}

func (w *brWriter) Reset(p io.Writer) {
w.Writer = cbrotli.NewWriter(p, cbrotli.WriterOptions{Quality: w.quality})
w.Writer = cbrotli.NewWriter(p, *w.opt)
}
2 changes: 1 addition & 1 deletion pkg/compress/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (m Compress) ServeHandler(h http.Handler) http.Handler {
}

pool := &sync.Pool{
New: func() interface{} {
New: func() any {
return m.New()
},
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/compress/gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import (

// Gzip creates new gzip compress middleware
func Gzip() *Compress {
return GzipWithLevel(gzip.DefaultCompression)
}

func GzipWithLevel(level int) *Compress {
return &Compress{
New: func() Compressor {
g, err := gzip.NewWriterLevel(io.Discard, gzip.DefaultCompression)
g, err := gzip.NewWriterLevel(io.Discard, level)
if err != nil {
panic(err)
}
Expand Down