Implement customizable minimum body size for compress middleware
This commit is contained in:
parent
8f0832d340
commit
07a3c37a23
13 changed files with 134 additions and 9 deletions
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue