fixed json output for ls-routes and its API and homepage api

This commit is contained in:
yusing 2024-10-15 16:23:46 +08:00
parent f4d532598c
commit 56b778f19c
5 changed files with 23 additions and 12 deletions

View file

@ -8,6 +8,7 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
gphttp "github.com/yusing/go-proxy/internal/net/http" gphttp "github.com/yusing/go-proxy/internal/net/http"
"github.com/yusing/go-proxy/internal/net/types"
"github.com/yusing/go-proxy/internal/watcher/health" "github.com/yusing/go-proxy/internal/watcher/health"
) )
@ -72,6 +73,10 @@ func (w *Waker) Uptime() time.Duration {
} }
func (w *Waker) MarshalJSON() ([]byte, error) { func (w *Waker) MarshalJSON() ([]byte, error) {
var url types.URL
if w.URL.String() != "http://:0" {
url = w.URL
}
return (&health.JSONRepresentation{ return (&health.JSONRepresentation{
Name: w.Name(), Name: w.Name(),
Status: w.Status(), Status: w.Status(),
@ -79,7 +84,7 @@ func (w *Waker) MarshalJSON() ([]byte, error) {
Interval: w.IdleTimeout, Interval: w.IdleTimeout,
Timeout: w.WakeTimeout, Timeout: w.WakeTimeout,
}, },
URL: w.URL, URL: url,
}).MarshalJSON() }).MarshalJSON()
} }

View file

@ -27,8 +27,11 @@ func (u URL) String() string {
return u.URL.String() return u.URL.String()
} }
func (u URL) MarshalText() (text []byte, err error) { func (u URL) MarshalJSON() (text []byte, err error) {
return []byte(u.String()), nil if u.URL == nil {
return []byte("null"), nil
}
return []byte("\"" + u.URL.String() + "\""), nil
} }
func (u URL) Equals(other *URL) bool { func (u URL) Equals(other *URL) bool {

View file

@ -54,7 +54,7 @@ func (rp *ReverseProxyEntry) UseIdleWatcher() bool {
} }
func (rp *ReverseProxyEntry) UseLoadBalance() bool { func (rp *ReverseProxyEntry) UseLoadBalance() bool {
return rp.LoadBalance.Link != "" return rp.LoadBalance != nil && rp.LoadBalance.Link != ""
} }
func (rp *ReverseProxyEntry) IsDocker() bool { func (rp *ReverseProxyEntry) IsDocker() bool {

View file

@ -25,7 +25,7 @@ import (
type ( type (
HTTPRoute struct { HTTPRoute struct {
*P.ReverseProxyEntry `json:"entry"` *P.ReverseProxyEntry
HealthMon health.HealthMonitor `json:"health,omitempty"` HealthMon health.HealthMonitor `json:"health,omitempty"`

View file

@ -5,6 +5,7 @@ import (
"time" "time"
"github.com/yusing/go-proxy/internal/net/types" "github.com/yusing/go-proxy/internal/net/types"
U "github.com/yusing/go-proxy/internal/utils"
) )
type JSONRepresentation struct { type JSONRepresentation struct {
@ -22,9 +23,11 @@ func (jsonRepr *JSONRepresentation) MarshalJSON() ([]byte, error) {
"name": jsonRepr.Name, "name": jsonRepr.Name,
"config": jsonRepr.Config, "config": jsonRepr.Config,
"started": jsonRepr.Started.Unix(), "started": jsonRepr.Started.Unix(),
"startedStr": U.FormatTime(jsonRepr.Started),
"status": jsonRepr.Status.String(), "status": jsonRepr.Status.String(),
"uptime": jsonRepr.Uptime.Seconds(), "uptime": jsonRepr.Uptime.Seconds(),
"url": jsonRepr.URL.String(), "uptimeStr": U.FormatDuration(jsonRepr.Uptime),
"url": jsonRepr.URL,
"extra": jsonRepr.Extra, "extra": jsonRepr.Extra,
}) })
} }