1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00
echo/middleware/compress.go
Vishal Rana bf85c56b08 Encapsulated fields and exposed public functions.
Signed-off-by: Vishal Rana <vr@labstack.com>
2015-05-22 04:40:01 -07:00

41 lines
829 B
Go

package middleware
import (
"compress/gzip"
"io"
"strings"
"github.com/labstack/echo"
"net/http"
)
type (
gzipWriter struct {
io.Writer
http.ResponseWriter
}
)
func (w gzipWriter) Write(b []byte) (int, error) {
return w.Writer.Write(b)
}
// Gzip returns a middleware which compresses HTTP response using gzip compression
// scheme.
func Gzip() echo.MiddlewareFunc {
scheme := "gzip"
return func(h echo.HandlerFunc) echo.HandlerFunc {
return func(c *echo.Context) error {
if strings.Contains(c.Request().Header.Get(echo.AcceptEncoding), scheme) {
w := gzip.NewWriter(c.Response().Writer())
defer w.Close()
gw := gzipWriter{Writer: w, ResponseWriter: c.Response().Writer()}
c.Response().Header().Set(echo.ContentEncoding, scheme)
c.Response().SetWriter(gw)
}
return h(c)
}
}
}