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
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
package sources
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"ymhut-box/server/unified-management/internal/config"
|
||||
"ymhut-box/server/unified-management/internal/db"
|
||||
)
|
||||
|
||||
func TestCheckOneTreatsRedirectToOKAsRedirected(t *testing.T) {
|
||||
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_, _ = w.Write([]byte("ok"))
|
||||
}))
|
||||
defer target.Close()
|
||||
redirector := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, target.URL, http.StatusFound)
|
||||
}))
|
||||
defer redirector.Close()
|
||||
|
||||
cfg, store := testStore(t)
|
||||
service := NewService(cfg, store)
|
||||
item, err := store.UpsertSource(db.Source{
|
||||
CategoryID: "test",
|
||||
CategoryName: "Test",
|
||||
SourceID: "redirect",
|
||||
Name: "Redirect",
|
||||
Method: "GET",
|
||||
APIURL: redirector.URL,
|
||||
TimeoutMS: 3000,
|
||||
CheckIntervalSec: 300,
|
||||
Enabled: true,
|
||||
ClientVisible: true,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := service.CheckOne(context.Background(), item); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
checked, err := store.GetSourceBySourceID("redirect")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if checked.LastStatus != "redirected" {
|
||||
t.Fatalf("LastStatus = %q, want redirected", checked.LastStatus)
|
||||
}
|
||||
if !strings.Contains(checked.LastError, `"redirected":true`) {
|
||||
t.Fatalf("LastError does not contain redirect metadata: %s", checked.LastError)
|
||||
}
|
||||
if checked.ConsecutiveFailure != 0 {
|
||||
t.Fatalf("ConsecutiveFailure = %d, want 0", checked.ConsecutiveFailure)
|
||||
}
|
||||
}
|
||||
|
||||
func testStore(t *testing.T) (*config.Config, *db.Store) {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
cfg := &config.Config{
|
||||
BaseDir: dir,
|
||||
StorageDir: filepath.Join(dir, "storage"),
|
||||
DataDir: filepath.Join(dir, "data"),
|
||||
UpdatePublicDir: filepath.Join(dir, "data", "update", "public"),
|
||||
DownloadsDir: filepath.Join(dir, "data", "update", "public", "downloads"),
|
||||
BaseURL: "https://update.ymhut.cn",
|
||||
Database: config.DatabaseConfig{
|
||||
Provider: "sqlite",
|
||||
SQLitePath: filepath.Join(dir, "storage", "unified.sqlite"),
|
||||
},
|
||||
}
|
||||
store, err := db.Open(cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { store.Close() })
|
||||
return cfg, store
|
||||
}
|
||||
Reference in New Issue
Block a user