This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -168,6 +169,7 @@ func (s *Service) Catalog(includeHidden bool) (map[string]any, error) {
|
||||
"last_checked_at": item.LastCheckedAt,
|
||||
"last_error": item.LastError,
|
||||
"consecutiveFailure": item.ConsecutiveFailure,
|
||||
"meta": parseHealthMeta(item.LastError),
|
||||
},
|
||||
}
|
||||
cat["subcategories"] = append(cat["subcategories"].([]map[string]any), sub)
|
||||
@@ -209,6 +211,7 @@ func (s *Service) Endpoints(includeHidden bool) ([]map[string]any, error) {
|
||||
"lastCheckedAt": item.LastCheckedAt,
|
||||
"lastError": item.LastError,
|
||||
"consecutiveFailure": item.ConsecutiveFailure,
|
||||
"meta": parseHealthMeta(item.LastError),
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -252,13 +255,30 @@ func (s *Service) CheckOne(ctx context.Context, item db.Source) error {
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(ctx, timeout)
|
||||
defer cancel()
|
||||
req, err := http.NewRequestWithContext(ctx, item.Method, item.APIURL, nil)
|
||||
method := strings.TrimSpace(item.Method)
|
||||
if method == "" {
|
||||
method = http.MethodGet
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, method, item.APIURL, nil)
|
||||
if err != nil {
|
||||
_ = s.store.RecordSourceCheck(item.ID, "error", 0, err.Error())
|
||||
return err
|
||||
}
|
||||
redirects := []string{}
|
||||
client := *s.client
|
||||
client.Timeout = timeout
|
||||
client.CheckRedirect = func(next *http.Request, via []*http.Request) error {
|
||||
if next.URL == nil || !isHTTPURL(next.URL) {
|
||||
return errors.New("redirect target must be http or https")
|
||||
}
|
||||
redirects = append(redirects, next.URL.String())
|
||||
if len(via) >= 5 {
|
||||
return errors.New("too many redirects")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
start := time.Now()
|
||||
resp, err := s.client.Do(req)
|
||||
resp, err := client.Do(req)
|
||||
latency := int(time.Since(start).Milliseconds())
|
||||
if err != nil {
|
||||
_ = s.store.RecordSourceCheck(item.ID, "error", latency, err.Error())
|
||||
@@ -267,13 +287,49 @@ func (s *Service) CheckOne(ctx context.Context, item db.Source) error {
|
||||
defer resp.Body.Close()
|
||||
status := "ok"
|
||||
message := ""
|
||||
if len(redirects) > 0 {
|
||||
status = "redirected"
|
||||
message = healthMetaMessage(map[string]any{
|
||||
"redirected": true,
|
||||
"redirectCount": len(redirects),
|
||||
"finalUrl": resp.Request.URL.String(),
|
||||
"finalStatus": resp.StatusCode,
|
||||
})
|
||||
}
|
||||
if resp.StatusCode >= 400 {
|
||||
status = "degraded"
|
||||
message = resp.Status
|
||||
message = healthMetaMessage(map[string]any{
|
||||
"redirected": len(redirects) > 0,
|
||||
"redirectCount": len(redirects),
|
||||
"finalUrl": resp.Request.URL.String(),
|
||||
"finalStatus": resp.StatusCode,
|
||||
"error": resp.Status,
|
||||
})
|
||||
}
|
||||
return s.store.RecordSourceCheck(item.ID, status, latency, message)
|
||||
}
|
||||
|
||||
func isHTTPURL(value *url.URL) bool {
|
||||
scheme := strings.ToLower(value.Scheme)
|
||||
return scheme == "http" || scheme == "https"
|
||||
}
|
||||
|
||||
func healthMetaMessage(meta map[string]any) string {
|
||||
data, err := json.Marshal(meta)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func parseHealthMeta(message string) map[string]any {
|
||||
var meta map[string]any
|
||||
if strings.TrimSpace(message) == "" || json.Unmarshal([]byte(message), &meta) != nil {
|
||||
return map[string]any{}
|
||||
}
|
||||
return meta
|
||||
}
|
||||
|
||||
func defaultString(value, fallback string) string {
|
||||
if strings.TrimSpace(value) == "" {
|
||||
return fallback
|
||||
|
||||
Reference in New Issue
Block a user