Skip to content

Commit e9a58e1

Browse files
authored
Merge pull request #231 from BennyFranciscus/fix/echo-compression-middleware
fix(echo): use built-in compression middleware instead of manual gzip/deflate
2 parents 4d7100d + dbe13ef commit e9a58e1

3 files changed

Lines changed: 9 additions & 25 deletions

File tree

frameworks/echo/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ require (
2727
golang.org/x/sync v0.19.0 // indirect
2828
golang.org/x/sys v0.39.0 // indirect
2929
golang.org/x/text v0.32.0 // indirect
30+
golang.org/x/time v0.14.0 // indirect
3031
modernc.org/libc v1.67.6 // indirect
3132
modernc.org/mathutil v1.7.1 // indirect
3233
modernc.org/memory v1.11.0 // indirect

frameworks/echo/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
5555
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
5656
golang.org/x/text v0.32.0 h1:ZD01bjUt1FQ9WJ0ClOL5vxgxOI/sVCNgX1YtKwcY0mU=
5757
golang.org/x/text v0.32.0/go.mod h1:o/rUWzghvpD5TXrTIBuJU77MTaN0ljMWE47kxGJQ7jY=
58+
golang.org/x/time v0.14.0 h1:MRx4UaLrDotUKUdCIqzPC48t1Y9hANFKIRpNx+Te8PI=
59+
golang.org/x/time v0.14.0/go.mod h1:eL/Oa2bBBK0TkX57Fyni+NgnyQQN4LitPmob2Hjnqw4=
5860
golang.org/x/tools v0.39.0 h1:ik4ho21kwuQln40uelmciQPp9SipgNDdrafrYA4TmQQ=
5961
golang.org/x/tools v0.39.0/go.mod h1:JnefbkDPyD8UU2kI5fuf8ZX4/yUeh9W877ZeBONxUqQ=
6062
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

frameworks/echo/main.go

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package main
22

33
import (
4-
"compress/flate"
5-
"compress/gzip"
64
"context"
75
"database/sql"
86
"encoding/json"
@@ -18,6 +16,7 @@ import (
1816

1917
"github.com/jackc/pgx/v5/pgxpool"
2018
"github.com/labstack/echo/v4"
19+
"github.com/labstack/echo/v4/middleware"
2120
_ "modernc.org/sqlite"
2221
)
2322

@@ -172,6 +171,10 @@ func main() {
172171
e.HideBanner = true
173172
e.HidePort = true
174173

174+
gzipMiddleware := middleware.GzipWithConfig(middleware.GzipConfig{
175+
Level: 1,
176+
})
177+
175178
e.GET("/pipeline", func(c echo.Context) error {
176179
c.Response().Header().Set("Server", "echo")
177180
return c.String(http.StatusOK, "ok")
@@ -214,30 +217,8 @@ func main() {
214217

215218
e.GET("/compression", func(c echo.Context) error {
216219
c.Response().Header().Set("Server", "echo")
217-
ae := c.Request().Header.Get("Accept-Encoding")
218-
if strings.Contains(ae, "deflate") {
219-
c.Response().Header().Set("Content-Type", "application/json")
220-
c.Response().Header().Set("Content-Encoding", "deflate")
221-
c.Response().WriteHeader(http.StatusOK)
222-
w, err := flate.NewWriter(c.Response(), flate.BestSpeed)
223-
if err == nil {
224-
w.Write(jsonLargeResponse)
225-
w.Close()
226-
}
227-
return nil
228-
} else if strings.Contains(ae, "gzip") {
229-
c.Response().Header().Set("Content-Type", "application/json")
230-
c.Response().Header().Set("Content-Encoding", "gzip")
231-
c.Response().WriteHeader(http.StatusOK)
232-
w, err := gzip.NewWriterLevel(c.Response(), gzip.BestSpeed)
233-
if err == nil {
234-
w.Write(jsonLargeResponse)
235-
w.Close()
236-
}
237-
return nil
238-
}
239220
return c.Blob(http.StatusOK, "application/json", jsonLargeResponse)
240-
})
221+
}, gzipMiddleware)
241222

242223
e.POST("/upload", func(c echo.Context) error {
243224
size, _ := io.Copy(io.Discard, c.Request().Body)

0 commit comments

Comments
 (0)