Switch back to chi compress middleware and allow more content types

This commit is contained in:
Jan-Lukas Else 2022-02-16 19:37:56 +01:00
parent e7289c3120
commit 1f8c031e14
1 changed files with 28 additions and 6 deletions

34
http.go
View File

@ -4,6 +4,7 @@ import (
"database/sql" "database/sql"
"errors" "errors"
"fmt" "fmt"
"io"
"log" "log"
"net" "net"
"net/http" "net/http"
@ -14,8 +15,9 @@ import (
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware" "github.com/go-chi/chi/v5/middleware"
"github.com/justinas/alice" "github.com/justinas/alice"
"github.com/klauspost/compress/gzhttp" "github.com/klauspost/compress/flate"
"github.com/klauspost/compress/gzip" "github.com/klauspost/compress/gzip"
"go.goblog.app/app/pkgs/contenttype"
"go.goblog.app/app/pkgs/maprouter" "go.goblog.app/app/pkgs/maprouter"
"golang.org/x/net/context" "golang.org/x/net/context"
) )
@ -41,11 +43,31 @@ func (a *goBlog) startServer() (err error) {
if a.cfg.Server.Logging { if a.cfg.Server.Logging {
h = h.Append(a.logMiddleware) h = h.Append(a.logMiddleware)
} }
compressor, err := gzhttp.NewWrapper(gzhttp.CompressionLevel(gzip.BestCompression)) compressor := middleware.NewCompressor(flate.BestCompression, []string{
if err != nil { contenttype.AS,
return err contenttype.ATOM,
} contenttype.CSS,
h = h.Append(middleware.Recoverer, func(next http.Handler) http.Handler { return compressor(next) }, middleware.Heartbeat("/ping")) 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) { if a.httpsConfigured(false) {
h = h.Append(a.securityHeaders) h = h.Append(a.securityHeaders)
} }