mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-23 09:18:51 +02:00
improve initialization flow
This commit is contained in:
@@ -87,6 +87,7 @@ func (p *Provider) MarshalText() ([]byte, error) {
|
||||
}
|
||||
|
||||
func (p *Provider) startRoute(parent task.Parent, r *route.Route) gperr.Error {
|
||||
r.FinalizeHomepageConfig()
|
||||
err := r.Start(parent)
|
||||
if err != nil {
|
||||
delete(p.routes, r.Alias)
|
||||
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
|
||||
dockertypes "github.com/docker/docker/api/types"
|
||||
"github.com/yusing/go-proxy/internal/common"
|
||||
config "github.com/yusing/go-proxy/internal/config/types"
|
||||
"github.com/yusing/go-proxy/internal/net/gphttp/accesslog"
|
||||
loadbalance "github.com/yusing/go-proxy/internal/net/gphttp/loadbalancer/types"
|
||||
"github.com/yusing/go-proxy/internal/route/rules"
|
||||
@@ -356,13 +357,27 @@ func (r *Route) Finalize() {
|
||||
cont.StopMethod = common.StopMethodDefault
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if r.Homepage.IsEmpty() {
|
||||
r.Homepage = homepage.NewItem(r.Alias)
|
||||
func (r *Route) FinalizeHomepageConfig() {
|
||||
if r.Alias == "" {
|
||||
panic("alias is empty")
|
||||
}
|
||||
|
||||
if r.Homepage.Name == "" {
|
||||
var key string
|
||||
isDocker := r.Container != nil
|
||||
|
||||
hp := r.Homepage
|
||||
if hp.IsEmpty() {
|
||||
hp = homepage.NewItem(r.Alias)
|
||||
}
|
||||
hp = hp.GetOverride()
|
||||
hp.Alias = r.Alias
|
||||
hp.Provider = r.Provider
|
||||
|
||||
r.Homepage = hp
|
||||
|
||||
var key string
|
||||
if hp.Name == "" {
|
||||
if r.Container != nil {
|
||||
key = r.Container.ImageName
|
||||
} else {
|
||||
@@ -370,16 +385,40 @@ func (r *Route) Finalize() {
|
||||
}
|
||||
displayName, ok := internal.GetDisplayName(key)
|
||||
if ok {
|
||||
r.Homepage.Name = displayName
|
||||
hp.Name = displayName
|
||||
} else {
|
||||
r.Homepage.Name = strutils.Title(
|
||||
hp.Name = strutils.Title(
|
||||
strings.ReplaceAll(
|
||||
strings.ReplaceAll(r.Alias, "-", " "),
|
||||
strings.ReplaceAll(key, "-", " "),
|
||||
"_", " ",
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if hp.Category == "" {
|
||||
if config.GetInstance().Value().Homepage.UseDefaultCategories {
|
||||
if isDocker {
|
||||
key = r.Container.ImageName
|
||||
} else {
|
||||
key = strings.ToLower(r.Alias)
|
||||
}
|
||||
if category, ok := homepage.PredefinedCategories[key]; ok {
|
||||
hp.Category = category
|
||||
}
|
||||
}
|
||||
|
||||
if hp.Category == "" {
|
||||
switch {
|
||||
case r.UseLoadBalance():
|
||||
hp.Category = "Load-balanced"
|
||||
case isDocker:
|
||||
hp.Category = "Docker"
|
||||
default:
|
||||
hp.Category = "Others"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func lowestPort(ports map[int]dockertypes.Port) (res int) {
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
package routequery
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"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/watcher/health"
|
||||
@@ -79,70 +77,17 @@ func HomepageCategories() []string {
|
||||
return categories
|
||||
}
|
||||
|
||||
func HomepageConfig(useDefaultCategories bool, categoryFilter, providerFilter string) homepage.Categories {
|
||||
func HomepageConfig(categoryFilter, providerFilter string) homepage.Categories {
|
||||
hpCfg := homepage.NewHomePageConfig()
|
||||
|
||||
routes.GetHTTPRoutes().RangeAll(func(alias string, r route.HTTPRoute) {
|
||||
item := r.HomepageConfig()
|
||||
|
||||
if override := item.GetOverride(); override != item {
|
||||
if providerFilter != "" && override.Provider != providerFilter ||
|
||||
categoryFilter != "" && override.Category != categoryFilter {
|
||||
return
|
||||
}
|
||||
hpCfg.Add(override)
|
||||
return
|
||||
}
|
||||
|
||||
item.Alias = alias
|
||||
item.Provider = r.ProviderName()
|
||||
|
||||
if providerFilter != "" && item.Provider != providerFilter {
|
||||
return
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if categoryFilter != "" && item.Category != categoryFilter {
|
||||
return
|
||||
}
|
||||
|
||||
switch {
|
||||
case r.IsDocker():
|
||||
if item.Category == "" {
|
||||
item.Category = "Docker"
|
||||
}
|
||||
if r.IsAgent() {
|
||||
item.SourceType = string(provider.ProviderTypeAgent)
|
||||
} else {
|
||||
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)
|
||||
})
|
||||
return hpCfg
|
||||
|
||||
Reference in New Issue
Block a user