mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-11 17:56:37 +08:00
refactor(config): render routes from zone domains
This commit is contained in:
@@ -10,7 +10,6 @@ import (
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/Rain-kl/Wavelet/internal/apps/openflare/routeidentity"
|
||||
"github.com/Rain-kl/Wavelet/internal/model"
|
||||
"github.com/Rain-kl/Wavelet/internal/repository"
|
||||
)
|
||||
@@ -123,7 +122,7 @@ func SyncToUptimeKuma(ctx context.Context) error {
|
||||
}
|
||||
|
||||
existingOpenFlareMonitors := filterOpenFlareMonitors(client.GetMonitorList(), openFlareTagID)
|
||||
expectedSitesMap := syncRouteMonitors(client, expectedRoutes, existingOpenFlareMonitors, openFlareTagID, config)
|
||||
expectedSitesMap := syncRouteMonitors(ctx, client, expectedRoutes, existingOpenFlareMonitors, openFlareTagID, config)
|
||||
removeStaleMonitors(client, existingOpenFlareMonitors, expectedSitesMap)
|
||||
|
||||
return nil
|
||||
@@ -224,12 +223,18 @@ func filterOpenFlareMonitors(monitors map[string]Monitor, openFlareTagID int) ma
|
||||
return existingOpenFlareMonitors
|
||||
}
|
||||
|
||||
func routeMonitorURL(route *model.ProxyRoute) (string, error) {
|
||||
domains, err := routeidentity.DecodeDomains(route.Domains, route.Domain)
|
||||
func routeMonitorURL(ctx context.Context, route *model.ProxyRoute) (string, error) {
|
||||
if route == nil {
|
||||
return "", fmt.Errorf("proxy route is nil")
|
||||
}
|
||||
domains, err := model.ListZoneDomainsByRouteID(ctx, route.ID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
domain := domains[0]
|
||||
if len(domains) == 0 {
|
||||
return "", fmt.Errorf("route %s has no zone domains", route.SiteName)
|
||||
}
|
||||
domain := domains[0].Domain
|
||||
if route.EnableHTTPS {
|
||||
return "https://" + domain, nil
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
package uptimekuma
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
@@ -69,11 +70,11 @@ func connectAndLoginUptimeKuma(kumaURL, kumaUsername, kumaPassword string) (*Soc
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func syncRouteMonitors(client *SocketIOClient, expectedRoutes []*model.ProxyRoute, existingMonitors map[string]Monitor, openFlareTagID int, config *kumaConfig) map[string]bool {
|
||||
func syncRouteMonitors(ctx context.Context, client *SocketIOClient, expectedRoutes []*model.ProxyRoute, existingMonitors map[string]Monitor, openFlareTagID int, config *kumaConfig) map[string]bool {
|
||||
expectedSitesMap := make(map[string]bool, len(expectedRoutes))
|
||||
for _, route := range expectedRoutes {
|
||||
expectedSitesMap[route.SiteName] = true
|
||||
targetURL, urlErr := routeMonitorURL(route)
|
||||
targetURL, urlErr := routeMonitorURL(ctx, route)
|
||||
if urlErr != nil {
|
||||
slog.Error("Failed to resolve monitor URL", "name", route.SiteName, "error", urlErr)
|
||||
continue
|
||||
|
||||
@@ -122,7 +122,7 @@ func setupSyncTestDB(t *testing.T) func() {
|
||||
DisableForeignKeyConstraintWhenMigrating: true,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, sqliteDB.AutoMigrate(&model.ProxyRoute{}, &model.SystemConfig{}))
|
||||
require.NoError(t, sqliteDB.AutoMigrate(&model.ProxyRoute{}, &model.Zone{}, &model.ZoneDomain{}, &model.SystemConfig{}))
|
||||
|
||||
db.SetDB(sqliteDB)
|
||||
return func() {
|
||||
@@ -130,6 +130,17 @@ func setupSyncTestDB(t *testing.T) func() {
|
||||
}
|
||||
}
|
||||
|
||||
func createRouteZoneDomain(t *testing.T, ctx context.Context, route *model.ProxyRoute, domain string) {
|
||||
t.Helper()
|
||||
zone := &model.Zone{Domain: fmt.Sprintf("zone-%d.example", route.ID)}
|
||||
require.NoError(t, db.DB(ctx).Create(zone).Error)
|
||||
require.NoError(t, db.DB(ctx).Create(&model.ZoneDomain{
|
||||
ZoneID: zone.ID,
|
||||
ProxyRouteID: &route.ID,
|
||||
Domain: domain,
|
||||
}).Error)
|
||||
}
|
||||
|
||||
func backupUptimeKumaConfig(ctx context.Context) func() {
|
||||
// 备份所有 UptimeKuma 相关配置
|
||||
configs := []string{
|
||||
@@ -215,6 +226,9 @@ func TestSyncToUptimeKumaSuccess(t *testing.T) {
|
||||
require.NoError(t, model.CreateProxyRouteRecord(ctx, routeA))
|
||||
require.NoError(t, model.CreateProxyRouteRecord(ctx, routeB))
|
||||
require.NoError(t, model.CreateProxyRouteRecord(ctx, routeC))
|
||||
createRouteZoneDomain(t, ctx, routeA, "site-a.com")
|
||||
createRouteZoneDomain(t, ctx, routeB, "site-b.com")
|
||||
createRouteZoneDomain(t, ctx, routeC, "site-c.com")
|
||||
|
||||
monitorListJSON := `{
|
||||
"99": {
|
||||
@@ -318,6 +332,8 @@ func TestSyncToUptimeKumaSelectedScope(t *testing.T) {
|
||||
|
||||
require.NoError(t, model.CreateProxyRouteRecord(ctx, routeA))
|
||||
require.NoError(t, model.CreateProxyRouteRecord(ctx, routeB))
|
||||
createRouteZoneDomain(t, ctx, routeA, "site-a.com")
|
||||
createRouteZoneDomain(t, ctx, routeB, "site-b.com")
|
||||
|
||||
mockSrv := newMockKumaServer(`{}`)
|
||||
server := httptest.NewServer(mockSrv)
|
||||
|
||||
Reference in New Issue
Block a user