feat: enhance route handling with agent support and refactor port selection mapping

This commit is contained in:
yusing
2025-03-28 08:11:58 +08:00
parent fbb07011f1
commit 7ef8354eb0
7 changed files with 336 additions and 162 deletions

View File

@@ -1,15 +1,12 @@
package routequery
import (
"strings"
"time"
"github.com/yusing/go-proxy/internal"
"github.com/yusing/go-proxy/internal/homepage"
provider "github.com/yusing/go-proxy/internal/route/provider/types"
"github.com/yusing/go-proxy/internal/route/routes"
route "github.com/yusing/go-proxy/internal/route/types"
"github.com/yusing/go-proxy/internal/utils/strutils"
"github.com/yusing/go-proxy/internal/watcher/health"
)
func getHealthInfo(r route.Route) map[string]string {
@@ -28,13 +25,37 @@ func getHealthInfo(r route.Route) map[string]string {
}
}
type HealthInfoRaw struct {
Status health.Status
Latency time.Duration
}
func getHealthInfoRaw(r route.Route) *HealthInfoRaw {
mon := r.HealthMonitor()
if mon == nil {
return &HealthInfoRaw{
Status: health.StatusUnknown,
Latency: time.Duration(0),
}
}
return &HealthInfoRaw{
Status: mon.Status(),
Latency: mon.Latency(),
}
}
func HealthMap() map[string]map[string]string {
healthMap := make(map[string]map[string]string)
routes.GetHTTPRoutes().RangeAll(func(alias string, r route.HTTPRoute) {
healthMap := make(map[string]map[string]string, routes.NumRoutes())
routes.RangeRoutes(func(alias string, r route.Route) {
healthMap[alias] = getHealthInfo(r)
})
routes.GetStreamRoutes().RangeAll(func(alias string, r route.StreamRoute) {
healthMap[alias] = getHealthInfo(r)
return healthMap
}
func HealthInfo() map[string]*HealthInfoRaw {
healthMap := make(map[string]*HealthInfoRaw, routes.NumRoutes())
routes.RangeRoutes(func(alias string, r route.Route) {
healthMap[alias] = getHealthInfoRaw(r)
})
return healthMap
}
@@ -43,105 +64,33 @@ func HomepageCategories() []string {
check := make(map[string]struct{})
categories := make([]string, 0)
routes.GetHTTPRoutes().RangeAll(func(alias string, r route.HTTPRoute) {
homepage := r.HomepageConfig()
if homepage.IsEmpty() || homepage.Category == "" {
item := r.HomepageConfig()
if item == nil || item.Category == "" {
return
}
if _, ok := check[homepage.Category]; ok {
if _, ok := check[item.Category]; ok {
return
}
check[homepage.Category] = struct{}{}
categories = append(categories, homepage.Category)
check[item.Category] = struct{}{}
categories = append(categories, item.Category)
})
return categories
}
func HomepageConfig(useDefaultCategories bool, categoryFilter, providerFilter string) homepage.Categories {
hpCfg := homepage.NewHomePageConfig()
func HomepageConfig(categoryFilter, providerFilter string) homepage.Homepage {
hp := make(homepage.Homepage)
routes.GetHTTPRoutes().RangeAll(func(alias string, r route.HTTPRoute) {
item := r.HomepageConfig()
if item.IsEmpty() {
item = homepage.NewItem(alias)
}
if override := item.GetOverride(); override != item {
if providerFilter != "" && override.Provider != providerFilter ||
categoryFilter != "" && override.Category != categoryFilter {
return
}
hpCfg.Add(override)
if providerFilter != "" && r.ProviderName() != providerFilter {
return
}
item.Alias = alias
item.Provider = r.ProviderName()
if providerFilter != "" && item.Provider != providerFilter {
return
}
if item.Name == "" {
reference := r.TargetName()
cont := r.ContainerInfo()
if cont != nil {
reference = cont.ImageName
}
name, ok := internal.GetDisplayName(reference)
if ok {
item.Name = name
} else {
item.Name = strutils.Title(
strings.ReplaceAll(
strings.ReplaceAll(alias, "-", " "),
"_", " ",
),
)
}
}
if useDefaultCategories {
container := r.ContainerInfo()
if container != nil && item.Category == "" {
if category, ok := homepage.PredefinedCategories[container.ImageName]; ok {
item.Category = category
}
}
if item.Category == "" {
if category, ok := homepage.PredefinedCategories[strings.ToLower(alias)]; ok {
item.Category = category
}
}
}
item := r.HomepageItem()
if categoryFilter != "" && item.Category != categoryFilter {
return
}
switch {
case r.IsDocker():
if item.Category == "" {
item.Category = "Docker"
}
item.SourceType = string(provider.ProviderTypeDocker)
case r.UseLoadBalance():
if item.Category == "" {
item.Category = "Load-balanced"
}
item.SourceType = "loadbalancer"
default:
if item.Category == "" {
item.Category = "Others"
}
item.SourceType = string(provider.ProviderTypeFile)
}
item.AltURL = r.TargetURL().String()
hpCfg.Add(item)
hp.Add(item)
})
return hpCfg
return hp
}
func RoutesByAlias(typeFilter ...route.RouteType) map[string]route.Route {

View File

@@ -10,6 +10,19 @@ var (
streamRoutes = F.NewMapOf[string, types.StreamRoute]()
)
func RangeRoutes(callback func(alias string, r types.Route)) {
httpRoutes.RangeAll(func(alias string, r types.HTTPRoute) {
callback(alias, r)
})
streamRoutes.RangeAll(func(alias string, r types.StreamRoute) {
callback(alias, r)
})
}
func NumRoutes() int {
return httpRoutes.Size() + streamRoutes.Size()
}
func GetHTTPRoutes() F.Map[string, types.HTTPRoute] {
return httpRoutes
}
@@ -35,6 +48,14 @@ func GetStreamRoute(alias string) (types.StreamRoute, bool) {
return streamRoutes.Load(alias)
}
func GetRoute(alias string) (types.Route, bool) {
r, ok := httpRoutes.Load(alias)
if ok {
return r, true
}
return streamRoutes.Load(alias)
}
func SetHTTPRoute(alias string, r types.HTTPRoute) {
httpRoutes.Store(alias, r)
}