Skip to content

Commit

Permalink
Merge pull request #2489 from traPtitech/revert/fix/gzip_middleware
Browse files Browse the repository at this point in the history
Revert "Merge pull request #2473 from traPtitech/fix/gzip_middleware"
  • Loading branch information
kaitoyama authored Sep 30, 2024
2 parents 39abec6 + bc4fe67 commit ceebf98
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
cloud.google.com/go/profiler v0.4.1
firebase.google.com/go/v4 v4.14.1
github.com/MicahParks/jwkset v0.5.20
github.com/NYTimes/gziphandler v1.1.1
github.com/aws/aws-sdk-go-v2 v1.27.0
github.com/aws/aws-sdk-go-v2/config v1.27.16
github.com/aws/aws-sdk-go-v2/credentials v1.17.16
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ github.com/MicahParks/keyfunc v1.9.0 h1:lhKd5xrFHLNOWrDc4Tyb/Q1AJ4LCzQ48GVJyVIID
github.com/MicahParks/keyfunc v1.9.0/go.mod h1:IdnCilugA0O/99dW+/MkvlyrsX8+L8+x95xuVNtM5jw=
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
github.com/NYTimes/gziphandler v1.1.1 h1:ZUDjpQae29j0ryrS0u/B8HZfJBtBQHjqw2rQ2cqUQ3I=
github.com/NYTimes/gziphandler v1.1.1/go.mod h1:n/CVRwUEOgIxrgPvAQhUUr9oeUtvrhMomdKFjzJNB0c=
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 h1:TngWCqHvy9oXAN6lEVMRuU21PR1EtLVZJmdB18Gu3Rw=
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5/go.mod h1:lmUJ/7eu/Q8D7ML55dXQrVaamCz2vxCfdQBasLZfHKk=
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 h1:ZBbLwSJqkHBuFDA6DUhhse0IGJ7T5bemHyNILUjvOq4=
Expand Down
37 changes: 37 additions & 0 deletions router/middlewares/gzip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package middlewares

import (
"compress/gzip"
"net/http"

"github.com/NYTimes/gziphandler"
"github.com/labstack/echo/v4"
)

// Gzip Gzipミドルウェア
func Gzip() echo.MiddlewareFunc {
gzh, _ := gziphandler.GzipHandlerWithOpts(
gziphandler.ContentTypes([]string{
"application/javascript",
"application/json",
"image/svg+xml",
"text/css",
"text/html",
"text/plain",
"text/xml",
}),
gziphandler.CompressionLevel(gzip.BestSpeed),
)
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) (err error) {
gzh(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c.SetRequest(r)
c.Response().Writer = w
if err := next(c); err != nil {
c.Error(err)
}
})).ServeHTTP(c.Response().Writer, c.Request())
return
}
}
}
5 changes: 1 addition & 4 deletions router/router.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package router

import (
"compress/gzip"
"net/http"

"github.com/labstack/echo-contrib/echoprometheus"
Expand Down Expand Up @@ -101,9 +100,7 @@ func newEcho(logger *zap.Logger, config *Config, repo repository.Repository, cm
}
e.Use(middlewares.Recovery(logger))
if config.Gzipped {
e.Use(middleware.GzipWithConfig(middleware.GzipConfig{
Level: gzip.BestSpeed,
}))
e.Use(middlewares.Gzip())
}
e.Use(extension.Wrap(repo, cm))
e.Use(middlewares.RequestCounter())
Expand Down

0 comments on commit ceebf98

Please sign in to comment.