fix(api): corrected json marshaling for HealthInfo

This commit is contained in:
yusing
2025-08-16 23:05:29 +08:00
parent 90efa36193
commit 7a9b8b3fb9
3 changed files with 29 additions and 7 deletions

View File

@@ -18,8 +18,8 @@ import (
type (
StatusByAlias struct {
Map map[string]routes.HealthInfo `json:"statuses"`
Timestamp int64 `json:"timestamp"`
Map map[string]*routes.HealthInfo `json:"statuses"`
Timestamp int64 `json:"timestamp"`
} // @name RouteStatusesByAlias
Status struct {
Status types.HealthStatus `json:"status" swaggertype:"string" enums:"healthy,unhealthy,unknown,napping,starting"`

View File

@@ -54,23 +54,23 @@ func (info *HealthInfo) UnmarshalJSON(data []byte) error {
return nil
}
func GetHealthInfo() map[string]HealthInfo {
healthMap := make(map[string]HealthInfo, NumRoutes())
func GetHealthInfo() map[string]*HealthInfo {
healthMap := make(map[string]*HealthInfo, NumRoutes())
for r := range Iter {
healthMap[r.Name()] = getHealthInfo(r)
}
return healthMap
}
func getHealthInfo(r types.Route) HealthInfo {
func getHealthInfo(r types.Route) *HealthInfo {
mon := r.HealthMonitor()
if mon == nil {
return HealthInfo{
return &HealthInfo{
Status: types.StatusUnknown,
Detail: "n/a",
}
}
return HealthInfo{
return &HealthInfo{
Status: mon.Status(),
Uptime: mon.Uptime(),
Latency: mon.Latency(),

View File

@@ -139,6 +139,28 @@ func (s HealthStatus) Idling() bool {
return s&IdlingMask != 0
}
func (s HealthStatus) MarshalJSON() ([]byte, error) {
return strconv.AppendQuote(nil, s.String()), nil
}
func (s *HealthStatus) UnmarshalJSON(data []byte) error {
var v any
if err := json.Unmarshal(data, &v); err != nil {
return err
}
switch v := v.(type) {
case string:
*s = NewHealthStatusFromString(v)
return nil
case int:
if v > 0 && v < NumStatuses {
*s = HealthStatus(v)
return nil
}
}
return fmt.Errorf("invalid health status type %T of value %v", v, v)
}
func (jsonRepr *HealthJSONRepr) MarshalJSON() ([]byte, error) {
var url string
if jsonRepr.URL != nil {