1
mirror of https://github.com/jlelse/GoBlog synced 2024-06-15 11:17:09 +00:00

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

34
http.go
View File

@ -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)
}