Skip to content

Commit

Permalink
compress: allow config level
Browse files Browse the repository at this point in the history
  • Loading branch information
acoshift committed Jun 10, 2023
1 parent bbe5ebb commit 3cfdaa9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
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)
}
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

0 comments on commit 3cfdaa9

Please sign in to comment.