From 616a654b27567bccba81877b6978943aff25bfaf Mon Sep 17 00:00:00 2001 From: TwiN Date: Fri, 30 Dec 2022 21:37:52 -0500 Subject: [PATCH] fix: Compress everything with Gzip --- controller/handler/endpoint_status.go | 25 ++----------------------- controller/handler/handler.go | 7 ++++--- 2 files changed, 6 insertions(+), 26 deletions(-) diff --git a/controller/handler/endpoint_status.go b/controller/handler/endpoint_status.go index 7eb9317a..b75fedc9 100644 --- a/controller/handler/endpoint_status.go +++ b/controller/handler/endpoint_status.go @@ -1,14 +1,11 @@ package handler import ( - "bytes" - "compress/gzip" "encoding/json" "fmt" "io" "log" "net/http" - "strings" "time" "github.com/TwiN/gatus/v5/client" @@ -31,25 +28,14 @@ var ( ) // EndpointStatuses handles requests to retrieve all EndpointStatus -// Due to the size of the response, this function leverages a cache. -// Must not be wrapped by GzipHandler +// Due to how intensive this operation can be on the storage, this function leverages a cache. func EndpointStatuses(cfg *config.Config) http.HandlerFunc { return func(writer http.ResponseWriter, r *http.Request) { page, pageSize := extractPageAndPageSizeFromRequest(r) - gzipped := strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") - var exists bool - var value interface{} - if gzipped { - writer.Header().Set("Content-Encoding", "gzip") - value, exists = cache.Get(fmt.Sprintf("endpoint-status-%d-%d-gzipped", page, pageSize)) - } else { - value, exists = cache.Get(fmt.Sprintf("endpoint-status-%d-%d", page, pageSize)) - } + value, exists := cache.Get(fmt.Sprintf("endpoint-status-%d-%d", page, pageSize)) var data []byte if !exists { var err error - buffer := &bytes.Buffer{} - gzipWriter := gzip.NewWriter(buffer) endpointStatuses, err := store.Get().GetAllEndpointStatuses(paging.NewEndpointStatusParams().WithResults(page, pageSize)) if err != nil { log.Printf("[handler][EndpointStatuses] Failed to retrieve endpoint statuses: %s", err.Error()) @@ -69,14 +55,7 @@ func EndpointStatuses(cfg *config.Config) http.HandlerFunc { http.Error(writer, "unable to marshal object to JSON", http.StatusInternalServerError) return } - _, _ = gzipWriter.Write(data) - _ = gzipWriter.Close() - gzippedData := buffer.Bytes() cache.SetWithTTL(fmt.Sprintf("endpoint-status-%d-%d", page, pageSize), data, cacheTTL) - cache.SetWithTTL(fmt.Sprintf("endpoint-status-%d-%d-gzipped", page, pageSize), gzippedData, cacheTTL) - if gzipped { - data = gzippedData - } } else { data = value.([]byte) } diff --git a/controller/handler/handler.go b/controller/handler/handler.go index c50cc9f2..306934a5 100644 --- a/controller/handler/handler.go +++ b/controller/handler/handler.go @@ -16,6 +16,7 @@ func CreateRouter(cfg *config.Config) *mux.Router { if cfg.Metrics { router.Handle("/metrics", promhttp.Handler()).Methods("GET") } + router.Use(GzipHandler) api := router.PathPrefix("/api").Subrouter() protected := api.PathPrefix("/").Subrouter() unprotected := api.PathPrefix("/").Subrouter() @@ -29,8 +30,8 @@ func CreateRouter(cfg *config.Config) *mux.Router { } // Endpoints unprotected.Handle("/v1/config", ConfigHandler{securityConfig: cfg.Security}).Methods("GET") - protected.HandleFunc("/v1/endpoints/statuses", EndpointStatuses(cfg)).Methods("GET") // No GzipHandler for this one, because we cache the content as Gzipped already - protected.HandleFunc("/v1/endpoints/{key}/statuses", GzipHandlerFunc(EndpointStatus)).Methods("GET") + protected.HandleFunc("/v1/endpoints/statuses", EndpointStatuses(cfg)).Methods("GET") + protected.HandleFunc("/v1/endpoints/{key}/statuses", EndpointStatus).Methods("GET") unprotected.HandleFunc("/v1/endpoints/{key}/health/badge.svg", HealthBadge).Methods("GET") unprotected.HandleFunc("/v1/endpoints/{key}/uptimes/{duration}/badge.svg", UptimeBadge).Methods("GET") unprotected.HandleFunc("/v1/endpoints/{key}/response-times/{duration}/badge.svg", ResponseTimeBadge(cfg)).Methods("GET") @@ -45,6 +46,6 @@ func CreateRouter(cfg *config.Config) *mux.Router { if err != nil { panic(err) } - router.PathPrefix("/").Handler(GzipHandler(http.FileServer(http.FS(staticFileSystem)))) + router.PathPrefix("/").Handler(http.FileServer(http.FS(staticFileSystem))) return router }