Skip to content

Commit c085d41

Browse files
0x46616c6bOpenCode
andcommitted
♻️ Use json.NewEncoder for HTTP JSON responses
Replace manual fmt.Fprintf JSON string formatting with proper json.NewEncoder for health and readiness handlers. This prevents potential issues with unescaped characters in error messages and ensures consistent JSON output. Co-Authored-By: OpenCode <noreply@opencode.ai>
1 parent 0da86fb commit c085d41

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

prometheus.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package main
22

33
import (
44
"context"
5-
"fmt"
5+
"encoding/json"
66
"net/http"
77
"time"
88

@@ -110,7 +110,7 @@ func StartMetricsServer(ctx context.Context, listenAddr string, userliClient Use
110110
func healthHandler(w http.ResponseWriter, r *http.Request) {
111111
w.Header().Set("Content-Type", "application/json")
112112
w.WriteHeader(http.StatusOK)
113-
fmt.Fprintf(w, `{"status":"ok"}`)
113+
_ = json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
114114
}
115115

116116
// readyHandler handles readiness probe requests (checks Userli API connectivity)
@@ -134,17 +134,17 @@ func readyHandler(userliClient UserliService) http.HandlerFunc {
134134
if err != nil {
135135
healthCheckStatus.Set(0)
136136
w.WriteHeader(http.StatusServiceUnavailable)
137-
fmt.Fprintf(w, `{"status":"unavailable","error":"%s"}`, err.Error())
137+
_ = json.NewEncoder(w).Encode(map[string]string{"status": "unavailable", "error": err.Error()})
138138
logger.Warn("Readiness check failed", zap.Error(err))
139139
return
140140
}
141141
healthCheckStatus.Set(1)
142142
w.WriteHeader(http.StatusOK)
143-
fmt.Fprintf(w, `{"status":"ready"}`)
143+
_ = json.NewEncoder(w).Encode(map[string]string{"status": "ready"})
144144
case <-ctx.Done():
145145
healthCheckStatus.Set(0)
146146
w.WriteHeader(http.StatusServiceUnavailable)
147-
fmt.Fprintf(w, `{"status":"unavailable","error":"timeout"}`)
147+
_ = json.NewEncoder(w).Encode(map[string]string{"status": "unavailable", "error": "timeout"})
148148
logger.Warn("Readiness check timeout")
149149
}
150150
}

prometheus_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (s *PrometheusTestSuite) TestHealthHandler() {
2525

2626
s.Equal(http.StatusOK, w.Code)
2727
s.Equal("application/json", w.Header().Get("Content-Type"))
28-
s.Equal(`{"status":"ok"}`, w.Body.String())
28+
s.Equal("{\"status\":\"ok\"}\n", w.Body.String())
2929
})
3030
}
3131

@@ -42,7 +42,7 @@ func (s *PrometheusTestSuite) TestReadyHandler() {
4242

4343
s.Equal(http.StatusOK, w.Code)
4444
s.Equal("application/json", w.Header().Get("Content-Type"))
45-
s.Equal(`{"status":"ready"}`, w.Body.String())
45+
s.Equal("{\"status\":\"ready\"}\n", w.Body.String())
4646

4747
mockClient.AssertExpectations(s.T())
4848
})
@@ -59,7 +59,7 @@ func (s *PrometheusTestSuite) TestReadyHandler() {
5959

6060
s.Equal(http.StatusServiceUnavailable, w.Code)
6161
s.Equal("application/json", w.Header().Get("Content-Type"))
62-
s.Equal(`{"status":"unavailable","error":"connection refused"}`, w.Body.String())
62+
s.Equal("{\"error\":\"connection refused\",\"status\":\"unavailable\"}\n", w.Body.String())
6363

6464
mockClient.AssertExpectations(s.T())
6565
})
@@ -80,7 +80,7 @@ func (s *PrometheusTestSuite) TestReadyHandler() {
8080

8181
s.Equal(http.StatusServiceUnavailable, w.Code)
8282
s.Equal("application/json", w.Header().Get("Content-Type"))
83-
s.Equal(`{"status":"unavailable","error":"timeout"}`, w.Body.String())
83+
s.Equal("{\"error\":\"timeout\",\"status\":\"unavailable\"}\n", w.Body.String())
8484
})
8585
}
8686

0 commit comments

Comments
 (0)