Implement customizable minimum body size for compress middleware

This commit is contained in:
Lukas Schulte Pelkum 2021-09-20 18:00:08 +02:00 committed by GitHub
parent 8f0832d340
commit 07a3c37a23
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 134 additions and 9 deletions

View file

@ -23,6 +23,7 @@ type compress struct {
next http.Handler
name string
excludes []string
minSize int
}
// New creates a new compress middleware.
@ -39,7 +40,12 @@ func New(ctx context.Context, next http.Handler, conf dynamic.Compress, name str
excludes = append(excludes, mediaType)
}
return &compress{next: next, name: name, excludes: excludes}, nil
minSize := gzhttp.DefaultMinSize
if conf.MinResponseBodyBytes > 0 {
minSize = conf.MinResponseBodyBytes
}
return &compress{next: next, name: name, excludes: excludes, minSize: minSize}, nil
}
func (c *compress) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
@ -64,7 +70,7 @@ func (c *compress) gzipHandler(ctx context.Context) http.Handler {
wrapper, err := gzhttp.NewWrapper(
gzhttp.ExceptContentTypes(c.excludes),
gzhttp.CompressionLevel(gzip.DefaultCompression),
gzhttp.MinSize(gzhttp.DefaultMinSize))
gzhttp.MinSize(c.minSize))
if err != nil {
log.FromContext(ctx).Error(err)
}