61 lines
1.6 KiB
Go
61 lines
1.6 KiB
Go
package app
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func handleTransportClientHealthAction(w http.ResponseWriter, r *http.Request, id string) {
|
|
if r.Method != http.MethodGet {
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
transportMu.Lock()
|
|
st := loadTransportClientsState()
|
|
transportMu.Unlock()
|
|
idx := findTransportClientIndex(st.Items, id)
|
|
if idx < 0 {
|
|
writeJSON(w, http.StatusNotFound, TransportClientHealthResponse{
|
|
OK: false,
|
|
Message: "not found",
|
|
Code: "TRANSPORT_CLIENT_NOT_FOUND",
|
|
})
|
|
return
|
|
}
|
|
it := st.Items[idx]
|
|
now := time.Now().UTC()
|
|
backend := selectTransportBackend(it)
|
|
probe := backend.Health(it)
|
|
snapshot := applyTransportHealthProbeSnapshot(it, backend.ID(), probe, now)
|
|
resp := buildTransportHealthResponse(snapshot, now)
|
|
if !probe.OK && strings.TrimSpace(probe.Code) != "" {
|
|
resp.Code = probe.Code
|
|
if strings.TrimSpace(probe.Message) != "" {
|
|
resp.Message = probe.Message
|
|
}
|
|
}
|
|
writeJSON(w, http.StatusOK, resp)
|
|
}
|
|
|
|
func handleTransportClientMetricsAction(w http.ResponseWriter, r *http.Request, id string) {
|
|
if r.Method != http.MethodGet {
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
transportMu.Lock()
|
|
st := loadTransportClientsState()
|
|
transportMu.Unlock()
|
|
idx := findTransportClientIndex(st.Items, id)
|
|
if idx < 0 {
|
|
writeJSON(w, http.StatusNotFound, TransportClientMetricsResponse{
|
|
OK: false,
|
|
Message: "not found",
|
|
Code: "TRANSPORT_CLIENT_NOT_FOUND",
|
|
})
|
|
return
|
|
}
|
|
it := st.Items[idx]
|
|
writeJSON(w, http.StatusOK, buildTransportMetricsResponse(it, time.Now().UTC()))
|
|
}
|