fix(proxy): Improves response handling for health check and statistics interfaces.

Replace json.NewEncoder.Encode with json.Marshal and w.Write.
Add a Content-Length header to ensure the integrity and correctness of the response data.
At the same time, error handling is enhanced to avoid potential coding failures.
This commit is contained in:
Gouryella
2025-12-08 15:51:02 +08:00
parent aa5b0bfde3
commit 3bc7978999

View File

@@ -573,8 +573,15 @@ func (h *Handler) serveHealth(w http.ResponseWriter, r *http.Request) {
"timestamp": time.Now().Unix(),
}
data, err := json.Marshal(health)
if err != nil {
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(health)
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(data)))
w.Write(data)
}
func (h *Handler) serveStats(w http.ResponseWriter, r *http.Request) {
@@ -607,6 +614,13 @@ func (h *Handler) serveStats(w http.ResponseWriter, r *http.Request) {
})
}
data, err := json.Marshal(stats)
if err != nil {
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(stats)
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(data)))
w.Write(data)
}