traefik/middlewares/compress.go

28 lines
652 B
Go
Raw Normal View History

2016-09-28 22:07:06 +01:00
package middlewares
import (
2017-07-13 17:56:01 +02:00
"compress/gzip"
2016-09-28 22:07:06 +01:00
"net/http"
"github.com/NYTimes/gziphandler"
2017-07-13 17:56:01 +02:00
"github.com/containous/traefik/log"
2017-06-07 01:02:02 +02:00
)
// Compress is a middleware that allows redirection
type Compress struct{}
2016-09-28 22:07:06 +01:00
2017-06-07 01:02:02 +02:00
// ServerHTTP is a function used by Negroni
2016-09-28 22:29:27 +01:00
func (c *Compress) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
2017-07-13 17:56:01 +02:00
gzipHandler(next).ServeHTTP(rw, r)
2017-06-07 01:02:02 +02:00
}
2017-07-13 17:56:01 +02:00
func gzipHandler(h http.Handler) http.Handler {
2017-08-21 11:10:03 +02:00
wrapper, err := gziphandler.GzipHandlerWithOpts(
gziphandler.CompressionLevel(gzip.DefaultCompression),
gziphandler.MinSize(gziphandler.DefaultMinSize))
2017-07-13 17:56:01 +02:00
if err != nil {
log.Error(err)
}
return wrapper(h)
2016-09-28 22:07:06 +01:00
}