diff --git a/internal/application/system/service.go b/internal/application/system/service.go index acf7911..1b7d2b1 100644 --- a/internal/application/system/service.go +++ b/internal/application/system/service.go @@ -113,6 +113,50 @@ func (s *Service) UpdateSettings(req *UpdateSettingsRequest) error { return nil } +// GetMonitoringIntervals reads latencyCheckInterval and locationCheckInterval +// from settings general JSON. Falls back to defaults if not set or on error. +func (s *Service) GetMonitoringIntervals() (latencyInterval, geoInterval time.Duration) { + const defaultLatency = 5 * time.Second + const defaultGeo = 24 * time.Hour + + settings, err := s.GetSettings() + if err != nil { + s.app.Logger().Warn("Failed to load settings for monitoring intervals, using defaults", "error", err) + return defaultLatency, defaultGeo + } + + latencyInterval = defaultLatency + if v, ok := settings.General["latencyCheckInterval"]; ok { + if secs, ok := toFloat64(v); ok && secs > 0 { + latencyInterval = time.Duration(secs) * time.Second + } + } + + geoInterval = defaultGeo + if v, ok := settings.General["locationCheckInterval"]; ok { + if secs, ok := toFloat64(v); ok && secs > 0 { + geoInterval = time.Duration(secs) * time.Second + } + } + + return latencyInterval, geoInterval +} + +func toFloat64(v interface{}) (float64, bool) { + switch n := v.(type) { + case float64: + return n, true + case int: + return float64(n), true + case int64: + return float64(n), true + case json.Number: + f, err := n.Float64() + return f, err == nil + } + return 0, false +} + // GetAppVersion returns the application version. func (s *Service) GetAppVersion() string { return buildinfo.AppVersion diff --git a/main.go b/main.go index e4af2a5..41c491d 100644 --- a/main.go +++ b/main.go @@ -4,8 +4,6 @@ import ( "embed" "io/fs" "log" - "time" - "github.com/spf13/cobra" "frpc-hub/internal/application/dashboard" @@ -86,10 +84,9 @@ func main() { // Auto-start servers with autoConnection enabled (non-blocking) go frpcService.AutoStartServers() - // Start network monitoring service - // Latency check: every 30 seconds (frequent for real-time monitoring) - // Geolocation check: every 24 hours (only for servers without geo data) - networkMonitorService.Start(5*time.Second, 24*time.Hour) + // Start network monitoring service using intervals from settings + latencyInterval, geoInterval := settingsService.GetMonitoringIntervals() + networkMonitorService.Start(latencyInterval, geoInterval) // Register routes for each module //githubHandler.RegisterHandlers(e)