mirror of
https://github.com/labstack/echo.git
synced 2024-11-28 08:38:39 +02:00
1ee3bc23e3
Signed-off-by: Vishal Rana <vr@labstack.com>
44 lines
866 B
Go
44 lines
866 B
Go
package middleware
|
|
|
|
import (
|
|
"compress/gzip"
|
|
"io"
|
|
"strings"
|
|
|
|
"github.com/labstack/echo"
|
|
)
|
|
|
|
type (
|
|
gzipResponseWriter struct {
|
|
io.Writer
|
|
*echo.Response
|
|
}
|
|
)
|
|
|
|
func (g gzipResponseWriter) Write(b []byte) (int, error) {
|
|
return g.Writer.Write(b)
|
|
}
|
|
|
|
// Gzip 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) *echo.HTTPError {
|
|
if !strings.Contains(c.Request.Header.Get(echo.AcceptEncoding), scheme) {
|
|
return nil
|
|
}
|
|
|
|
w := gzip.NewWriter(c.Response.Writer)
|
|
defer w.Close()
|
|
gw := gzipResponseWriter{Writer: w, Response: c.Response}
|
|
c.Response.Header().Set(echo.ContentEncoding, scheme)
|
|
c.Response = &echo.Response{Writer: gw}
|
|
if he := h(c); he != nil {
|
|
c.Error(he)
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
}
|