diff --git a/go.mod b/go.mod index fa17780..983bbb5 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/santhosh-tekuri/jsonschema v1.2.4 github.com/sirupsen/logrus v1.9.3 golang.org/x/net v0.29.0 + golang.org/x/text v0.18.0 gopkg.in/yaml.v3 v3.0.1 ) @@ -50,7 +51,6 @@ require ( golang.org/x/oauth2 v0.23.0 // indirect golang.org/x/sync v0.8.0 // indirect golang.org/x/sys v0.25.0 // indirect - golang.org/x/text v0.18.0 // indirect golang.org/x/time v0.6.0 // indirect golang.org/x/tools v0.25.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect diff --git a/internal/config/query.go b/internal/config/query.go index e61c36b..8546cb7 100644 --- a/internal/config/query.go +++ b/internal/config/query.go @@ -1,6 +1,10 @@ package config import ( + "fmt" + "strings" + + "github.com/yusing/go-proxy/internal/common" H "github.com/yusing/go-proxy/internal/homepage" M "github.com/yusing/go-proxy/internal/models" PR "github.com/yusing/go-proxy/internal/proxy/provider" @@ -26,12 +30,51 @@ func (cfg *Config) DumpProviders() map[string]*PR.Provider { } func (cfg *Config) HomepageConfig() H.HomePageConfig { + var proto, port string + domains := cfg.value.MatchDomains + cert, _ := cfg.autocertProvider.GetCert(nil) + if cert != nil { + proto = "https" + port = common.ProxyHTTPPort + } else { + proto = "http" + port = common.ProxyHTTPSPort + } + hpCfg := H.NewHomePageConfig() cfg.forEachRoute(func(alias string, r R.Route, p *PR.Provider) { if !r.Started() { return } - hpCfg.Add(*r.Entry().Homepage) + + entry := r.Entry() + item := entry.Homepage + if item == nil { + item = &H.HomePageItem{} + } + + if item.Name == "" { + item.Name = U.Title(alias) + } + + if p.GetType() == PR.ProviderTypeDocker { + if item.Category == "" { + item.Category = "Docker" + } + item.SourceType = string(PR.ProviderTypeDocker) + } else if p.GetType() == PR.ProviderTypeFile { + if item.Category == "" { + item.Category = "Others" + } + item.SourceType = string(PR.ProviderTypeFile) + } + + if item.URL == "" && r.Type() == R.RouteTypeReverseProxy { + if len(domains) > 0 { + item.URL = fmt.Sprintf("%s://%s.%s:%s", proto, strings.ToLower(alias), domains[0], port) + } + } + hpCfg.Add(item) }) return hpCfg } diff --git a/internal/homepage/homepage.go b/internal/homepage/homepage.go index 80f6c6c..5a44319 100644 --- a/internal/homepage/homepage.go +++ b/internal/homepage/homepage.go @@ -2,12 +2,14 @@ package homepage type ( HomePageConfig map[string]HomePageCategory - HomePageCategory []HomePageItem + HomePageCategory []*HomePageItem HomePageItem struct { Name string `yaml:"name" json:"name"` - Icon string `yaml:"icon" json:"icon,omitempty"` // URL or unicodes + Icon string `yaml:"icon" json:"icon,omitempty"` + URL string `yaml:"url" json:"url,omitempty"` // URL or unicodes Category string `yaml:"category" json:"category"` + SourceType string `yaml:"source_type" json:"source_type,omitempty"` Description string `yaml:"description" json:"description,omitempty"` WidgetConfig map[string]any `yaml:",flow" json:"widget_config,omitempty"` } @@ -17,19 +19,11 @@ func NewHomePageConfig() HomePageConfig { return HomePageConfig(make(map[string]HomePageCategory)) } -func HomePageItemDefault() *HomePageItem { - return &HomePageItem{ - Name: "Docker", - Category: "Uncategorized", - WidgetConfig: make(map[string]any), - } -} - func (c *HomePageConfig) Clear() { *c = make(HomePageConfig) } -func (c HomePageConfig) Add(item HomePageItem) { +func (c HomePageConfig) Add(item *HomePageItem) { if c[item.Category] == nil { c[item.Category] = make(HomePageCategory, 0) } diff --git a/internal/models/raw_entry.go b/internal/models/raw_entry.go index 5386f59..bc280c8 100644 --- a/internal/models/raw_entry.go +++ b/internal/models/raw_entry.go @@ -8,7 +8,6 @@ import ( . "github.com/yusing/go-proxy/internal/common" D "github.com/yusing/go-proxy/internal/docker" H "github.com/yusing/go-proxy/internal/homepage" - U "github.com/yusing/go-proxy/internal/utils" F "github.com/yusing/go-proxy/internal/utils/functional" ) @@ -40,11 +39,6 @@ func (e *RawEntry) FillMissingFields() { e.ProxyProperties = &D.ProxyProperties{} } - if e.Homepage == nil { - e.Homepage = H.HomePageItemDefault() - e.Homepage.Name = U.Title(e.Alias) - } - lp, pp, extra := e.splitPorts() if port, ok := ServiceNamePortMapTCP[e.ImageName]; ok { diff --git a/internal/proxy/provider/docker.go b/internal/proxy/provider/docker.go index 2a06c63..ae53a40 100755 --- a/internal/proxy/provider/docker.go +++ b/internal/proxy/provider/docker.go @@ -9,6 +9,7 @@ import ( "github.com/sirupsen/logrus" D "github.com/yusing/go-proxy/internal/docker" E "github.com/yusing/go-proxy/internal/error" + H "github.com/yusing/go-proxy/internal/homepage" M "github.com/yusing/go-proxy/internal/models" R "github.com/yusing/go-proxy/internal/route" W "github.com/yusing/go-proxy/internal/watcher" @@ -184,6 +185,7 @@ func (p *DockerProvider) entriesFromContainerLabels(container D.Container) (entr Alias: a, Host: p.hostname, ProxyProperties: container.ProxyProperties, + Homepage: &H.HomePageItem{}, }) }