Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions internal/sitecheck/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,24 +293,25 @@ func (sc *SiteChecker) CheckSite(ctx context.Context, siteURL string) (*SiteInfo
// Try enhanced health check first if config exists
config, err := LoadSiteConfig(siteURL)

// If health check is disabled, return a SiteInfo without status
// If health check is disabled, return cached metadata without issuing any network requests (#1446)
if err == nil && config != nil && !config.HealthCheckEnabled {
protocol := "http"
if config.HealthCheckConfig != nil && config.HealthCheckConfig.Protocol != "" {
protocol = config.HealthCheckConfig.Protocol
}

siteInfo := &SiteInfo{
SiteConfig: *config,
Name: extractDomainName(siteURL),
Title: config.DisplayURL,
}

// Try to get favicon if enabled and not a gRPC check
if sc.options.CheckFavicon && !isGRPCProtocol(protocol) {
faviconURL, faviconData := sc.tryGetFavicon(ctx, siteURL)
siteInfo.FaviconURL = faviconURL
siteInfo.FaviconData = faviconData
if existing := sc.getExistingSiteSnapshot(siteURL); existing != nil {
siteInfo.FaviconURL = existing.FaviconURL
siteInfo.FaviconData = existing.FaviconData
siteInfo.Status = existing.Status
siteInfo.StatusCode = existing.StatusCode
siteInfo.ResponseTime = existing.ResponseTime
siteInfo.LastChecked = existing.LastChecked
siteInfo.Error = existing.Error
if siteInfo.Title == "" {
siteInfo.Title = existing.Title
}
}

return siteInfo, nil
Expand Down Expand Up @@ -522,6 +523,20 @@ func (sc *SiteChecker) GetSitesList() []*SiteInfo {
return result
}

// getExistingSiteSnapshot returns a copy of the last known site info, if present.
func (sc *SiteChecker) getExistingSiteSnapshot(siteURL string) *SiteInfo {
sc.mu.RLock()
defer sc.mu.RUnlock()

existing, ok := sc.sites[siteURL]
if !ok || existing == nil {
return nil
}

clone := *existing
return &clone
}

// extractDomainName extracts domain name from URL
func extractDomainName(siteURL string) string {
parsed, err := url.Parse(siteURL)
Expand Down
48 changes: 48 additions & 0 deletions internal/sitecheck/checker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package sitecheck

import (
"context"
"net/http"
"testing"

"github.com/0xJacky/Nginx-UI/model"
)

type roundTripFunc func(*http.Request) (*http.Response, error)

func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req)
}

func TestCheckSiteSkipsNetworkWhenDisabled(t *testing.T) {
t.Cleanup(InvalidateSiteConfigCache)

options := DefaultCheckOptions()
checker := NewSiteChecker(options)

// Any HTTP request made by the checker should fail this test.
checker.client = &http.Client{
Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
t.Fatalf("unexpected HTTP request to %s while health check is disabled", req.URL.String())
return nil, nil
}),
}

const siteURL = "https://example.com"
config := &model.SiteConfig{
Model: model.Model{ID: 1},
Host: "example.com:443",
Scheme: "https",
DisplayURL: siteURL,
HealthCheckEnabled: false,
HealthCheckConfig: &model.HealthCheckConfig{
Protocol: "https",
},
}

setCachedSiteConfig(config.Host, config)

if _, err := checker.CheckSite(context.Background(), siteURL); err != nil {
t.Fatalf("CheckSite returned error: %v", err)
}
}