From a0e7f7784307a9fcba4fe262f46c6a882e4b04da Mon Sep 17 00:00:00 2001 From: Haelnorr Date: Thu, 13 Feb 2025 23:59:08 +1100 Subject: [PATCH] Added option to toggle gzip as it causes air browser refresh to break --- config/config.go | 2 ++ middleware/gzip.go | 5 +++-- server/server.go | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/config/config.go b/config/config.go index c42ab69..da62d0d 100644 --- a/config/config.go +++ b/config/config.go @@ -17,6 +17,7 @@ type Config struct { Port string // Port to listen on TrustedHost string // Domain/Hostname to accept as trusted SSL bool // Flag for SSL Mode + GZIP bool // Flag for GZIP compression on requests ReadHeaderTimeout time.Duration // Timeout for reading request headers in seconds WriteTimeout time.Duration // Timeout for writing requests in seconds IdleTimeout time.Duration // Timeout for idle connections in seconds @@ -84,6 +85,7 @@ func GetConfig(args map[string]string) (*Config, error) { Port: port, TrustedHost: GetEnvDefault("TRUSTED_HOST", "127.0.0.1"), SSL: GetEnvBool("SSL_MODE", false), + GZIP: GetEnvBool("GZIP", false), ReadHeaderTimeout: GetEnvDur("READ_HEADER_TIMEOUT", 2), WriteTimeout: GetEnvDur("WRITE_TIMEOUT", 10), IdleTimeout: GetEnvDur("IDLE_TIMEOUT", 120), diff --git a/middleware/gzip.go b/middleware/gzip.go index cc9ae92..7d3a632 100644 --- a/middleware/gzip.go +++ b/middleware/gzip.go @@ -7,9 +7,10 @@ import ( "strings" ) -func Gzip(next http.Handler) http.Handler { +func Gzip(next http.Handler, useGzip bool) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") { + if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") || + !useGzip { next.ServeHTTP(w, r) return } diff --git a/server/server.go b/server/server.go index c86490b..e460546 100644 --- a/server/server.go +++ b/server/server.go @@ -34,7 +34,7 @@ func NewServer( handler = middleware.Favicon(handler) // Gzip - handler = middleware.Gzip(handler) + handler = middleware.Gzip(handler, config.GZIP) // Start the timer for the request chain so logger can have accurate info handler = middleware.StartTimer(handler)