package api import ( "net/http" "git.coopgo.io/coopgo-apps/parcoursmob/core/utils/cache" "github.com/gorilla/mux" "github.com/rs/zerolog/log" ) func (h *Handler) GetCache(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) cacheID := vars["cacheid"] // Parse query parameters limitsMinStr := r.URL.Query().Get("limits.min") limitsMaxStr := r.URL.Query().Get("limits.max") limitsMin, limitsMax := cache.ParseLimits(limitsMinStr, limitsMaxStr) // Use a channel to synchronize the goroutines ch := make(chan []byte) // Fetch data from cache asynchronously go func() { result, err := h.cacheService.GetCacheData(cacheID, limitsMin, limitsMax) if err != nil { log.Error().Err(err).Msg("Failed to get cache data") w.WriteHeader(http.StatusNotFound) ch <- nil return } ch <- result.Data // Signal that the data has been fetched successfully close(ch) }() // Wait for the JSON marshaling goroutine to finish data := <-ch if data == nil { return // Stop processing if an error occurred } // Send the JSON response to the client w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) w.Write(data) <-ch }