2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 11:27:17 +08:00

feat: 优化压缩

This commit is contained in:
2025-12-31 01:40:01 +08:00
parent 5e7093c8ec
commit 0ecbe3da0e
3 changed files with 26 additions and 2 deletions

View File

@@ -1,14 +1,18 @@
package middleware
import (
"io"
"log/slog"
"net/http"
"path/filepath"
"github.com/andybalholm/brotli"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/httplog/v3"
"github.com/google/wire"
"github.com/klauspost/compress/gzip"
"github.com/klauspost/compress/zstd"
"github.com/knadh/koanf/v2"
"github.com/leonelquinteros/gotext"
"github.com/libtnb/sessions"
@@ -48,9 +52,21 @@ func NewMiddlewares(conf *koanf.Koanf, session *sessions.Manager, appRepo biz.Ap
// Globals is a collection of global middleware that will be applied to every request.
func (r *Middlewares) Globals(t *gotext.Locale, mux *chi.Mux) []func(http.Handler) http.Handler {
compressor := middleware.NewCompressor(5)
compressor.SetEncoder("gzip", func(w io.Writer, level int) io.Writer {
writer, _ := gzip.NewWriterLevel(w, level)
return writer
})
compressor.SetEncoder("br", func(w io.Writer, level int) io.Writer {
return brotli.NewWriterV2(w, 6)
})
compressor.SetEncoder("zstd", func(w io.Writer, level int) io.Writer {
writer, _ := zstd.NewWriter(w, zstd.WithEncoderLevel(zstd.SpeedBetterCompression))
return writer
})
return []func(http.Handler) http.Handler{
middleware.Recoverer,
//middleware.SupressNotFound(mux),// bug https://github.com/go-chi/chi/pull/940
httplog.RequestLogger(r.log, &httplog.Options{
Level: slog.LevelInfo,
LogRequestHeaders: []string{"User-Agent"},
@@ -64,7 +80,7 @@ func (r *Middlewares) Globals(t *gotext.Locale, mux *chi.Mux) []func(http.Handle
return req.Header.Get("X-Debug-Response") == "1"
},
}),
middleware.Compress(5),
compressor.Handler,
sessionmiddleware.StartSession(r.session),
Status(t),
Entrance(t, r.conf, r.session),