bump go-pkgz/rest

This commit is contained in:
Umputun 2021-04-04 02:39:42 -05:00
parent 1fec95e9b8
commit 7a263fed42
7 changed files with 61 additions and 16 deletions

View File

@ -120,7 +120,7 @@ func (h *Http) toHttp(address string, httpPort int) string {
func (h *Http) gzipHandler() func(next http.Handler) http.Handler {
if h.GzEnabled {
return R.Gzip
return R.Gzip()
}
return func(next http.Handler) http.Handler {

2
go.mod
View File

@ -5,7 +5,7 @@ go 1.16
require (
github.com/fsouza/go-dockerclient v1.7.2
github.com/go-pkgz/lgr v0.10.4
github.com/go-pkgz/rest v1.8.0
github.com/go-pkgz/rest v1.8.1
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.7.0
github.com/umputun/go-flags v1.5.1

2
go.sum
View File

@ -44,6 +44,8 @@ github.com/go-pkgz/lgr v0.10.4 h1:l7qyFjqEZgwRgaQQSEp6tve4A3OU80VrfzpvtEX8ngw=
github.com/go-pkgz/lgr v0.10.4/go.mod h1:CD0s1z6EFpIUplV067gitF77tn25JItzwHNKAPqeCF0=
github.com/go-pkgz/rest v1.8.0 h1:0AC9NKRsytwZxmvAsO2i/uQzhvMTFVOyu5UNHExpPF0=
github.com/go-pkgz/rest v1.8.0/go.mod h1:FKpgK5FgSqREG323OIU/JpIc0xA7dqay9BmK7LZXTQE=
github.com/go-pkgz/rest v1.8.1 h1:M0sMbgcWxHpKjXw7Z8uF6uNcsLynaPoR0CHGczjYSw0=
github.com/go-pkgz/rest v1.8.1/go.mod h1:wZ/dGipZUaF9to0vIQl7PwDHgWQDB0jsrFg1xnAKLDw=
github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=

View File

@ -4,5 +4,5 @@ go 1.15
require (
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.6.1
github.com/stretchr/testify v1.7.0
)

View File

@ -8,6 +8,8 @@ github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=

View File

@ -9,6 +9,17 @@ import (
"sync"
)
var gzDefaultContentTypes = []string{
"text/css",
"text/javascript",
"text/xml",
"text/html",
"text/plain",
"application/javascript",
"application/x-javascript",
"application/json",
}
var gzPool = sync.Pool{
New: func() interface{} { return gzip.NewWriter(ioutil.Discard) },
}
@ -28,21 +39,51 @@ func (w *gzipResponseWriter) Write(b []byte) (int, error) {
}
// Gzip is a middleware compressing response
func Gzip(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
next.ServeHTTP(w, r)
return
func Gzip(contentTypes ...string) func(http.Handler) http.Handler {
gzCts := gzDefaultContentTypes
if len(contentTypes) > 0 {
gzCts = contentTypes
}
contentType := func(r *http.Request) string {
result := r.Header.Get("Content-type")
if result == "" {
return "application/octet-stream"
}
return result
}
w.Header().Set("Content-Encoding", "gzip")
f := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
next.ServeHTTP(w, r)
return
}
gz := gzPool.Get().(*gzip.Writer)
defer gzPool.Put(gz)
var gzOk bool
ctype := contentType(r)
for _, c := range gzCts {
if strings.EqualFold(ctype, c) {
gzOk = true
break
}
}
gz.Reset(w)
defer gz.Close()
if !gzOk {
next.ServeHTTP(w, r)
return
}
next.ServeHTTP(&gzipResponseWriter{ResponseWriter: w, Writer: gz}, r)
})
w.Header().Set("Content-Encoding", "gzip")
gz := gzPool.Get().(*gzip.Writer)
defer gzPool.Put(gz)
gz.Reset(w)
defer gz.Close()
next.ServeHTTP(&gzipResponseWriter{ResponseWriter: w, Writer: gz}, r)
})
}
return f
}

2
vendor/modules.txt vendored
View File

@ -68,7 +68,7 @@ github.com/fsouza/go-dockerclient
# github.com/go-pkgz/lgr v0.10.4
## explicit
github.com/go-pkgz/lgr
# github.com/go-pkgz/rest v1.8.0
# github.com/go-pkgz/rest v1.8.1
## explicit
github.com/go-pkgz/rest
github.com/go-pkgz/rest/logger