go-microservices-tuto/images/handlers/middleware.go
2020-07-21 10:59:27 +02:00

24 lines
568 B
Go

package handlers
import (
"compress/gzip"
"net/http"
)
// GZipResponseMiddleware detects if the client can handle zipped content and if
// so returns the response in GZipped format
func GZipResponseMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r)
})
}
type WrappedResponseWriter struct {
w http.ResponseWriter
g *gzip.Writer
}
func NewWrappedResponseWriter(w http.ResponseWriter) *WrappedResponseWriter {
g := gzip.NewWriter(w)
return &WrappedResponseWriter{w, g}
}