From 1f8c031e14050bd4d8abd90423e49c80e4ee347e Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Wed, 16 Feb 2022 19:37:56 +0100 Subject: [PATCH] Switch back to chi compress middleware and allow more content types --- http.go | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/http.go b/http.go index 0a8d05b..0bff2ed 100644 --- a/http.go +++ b/http.go @@ -4,6 +4,7 @@ import ( "database/sql" "errors" "fmt" + "io" "log" "net" "net/http" @@ -14,8 +15,9 @@ import ( "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" "github.com/justinas/alice" - "github.com/klauspost/compress/gzhttp" + "github.com/klauspost/compress/flate" "github.com/klauspost/compress/gzip" + "go.goblog.app/app/pkgs/contenttype" "go.goblog.app/app/pkgs/maprouter" "golang.org/x/net/context" ) @@ -41,11 +43,31 @@ func (a *goBlog) startServer() (err error) { if a.cfg.Server.Logging { h = h.Append(a.logMiddleware) } - compressor, err := gzhttp.NewWrapper(gzhttp.CompressionLevel(gzip.BestCompression)) - if err != nil { - return err - } - h = h.Append(middleware.Recoverer, func(next http.Handler) http.Handler { return compressor(next) }, middleware.Heartbeat("/ping")) + compressor := middleware.NewCompressor(flate.BestCompression, []string{ + contenttype.AS, + contenttype.ATOM, + contenttype.CSS, + contenttype.HTML, + contenttype.JS, + contenttype.JSON, + contenttype.JSONFeed, + contenttype.LDJSON, + contenttype.RSS, + contenttype.Text, + contenttype.XML, + "application/opensearchdescription+xml", + "application/jrd+json", + "application/xrd+xml", + }...) + compressor.SetEncoder("deflate", func(w io.Writer, level int) io.Writer { + cw, _ := flate.NewWriter(w, level) + return cw + }) + compressor.SetEncoder("gzip", func(w io.Writer, level int) io.Writer { + cw, _ := gzip.NewWriterLevel(w, level) + return cw + }) + h = h.Append(middleware.Recoverer, compressor.Handler, middleware.Heartbeat("/ping")) if a.httpsConfigured(false) { h = h.Append(a.securityHeaders) }