improved homepage labels

This commit is contained in:
yusing 2024-10-03 04:00:02 +08:00
parent 90130411f9
commit 90bababd38
5 changed files with 52 additions and 19 deletions

2
go.mod
View file

@ -11,6 +11,7 @@ require (
github.com/santhosh-tekuri/jsonschema v1.2.4 github.com/santhosh-tekuri/jsonschema v1.2.4
github.com/sirupsen/logrus v1.9.3 github.com/sirupsen/logrus v1.9.3
golang.org/x/net v0.29.0 golang.org/x/net v0.29.0
golang.org/x/text v0.18.0
gopkg.in/yaml.v3 v3.0.1 gopkg.in/yaml.v3 v3.0.1
) )
@ -50,7 +51,6 @@ require (
golang.org/x/oauth2 v0.23.0 // indirect golang.org/x/oauth2 v0.23.0 // indirect
golang.org/x/sync v0.8.0 // indirect golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.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/time v0.6.0 // indirect
golang.org/x/tools v0.25.0 // indirect golang.org/x/tools v0.25.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect

View file

@ -1,6 +1,10 @@
package config package config
import ( import (
"fmt"
"strings"
"github.com/yusing/go-proxy/internal/common"
H "github.com/yusing/go-proxy/internal/homepage" H "github.com/yusing/go-proxy/internal/homepage"
M "github.com/yusing/go-proxy/internal/models" M "github.com/yusing/go-proxy/internal/models"
PR "github.com/yusing/go-proxy/internal/proxy/provider" 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 { 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() hpCfg := H.NewHomePageConfig()
cfg.forEachRoute(func(alias string, r R.Route, p *PR.Provider) { cfg.forEachRoute(func(alias string, r R.Route, p *PR.Provider) {
if !r.Started() { if !r.Started() {
return 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 return hpCfg
} }

View file

@ -2,12 +2,14 @@ package homepage
type ( type (
HomePageConfig map[string]HomePageCategory HomePageConfig map[string]HomePageCategory
HomePageCategory []HomePageItem HomePageCategory []*HomePageItem
HomePageItem struct { HomePageItem struct {
Name string `yaml:"name" json:"name"` 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"` Category string `yaml:"category" json:"category"`
SourceType string `yaml:"source_type" json:"source_type,omitempty"`
Description string `yaml:"description" json:"description,omitempty"` Description string `yaml:"description" json:"description,omitempty"`
WidgetConfig map[string]any `yaml:",flow" json:"widget_config,omitempty"` WidgetConfig map[string]any `yaml:",flow" json:"widget_config,omitempty"`
} }
@ -17,19 +19,11 @@ func NewHomePageConfig() HomePageConfig {
return HomePageConfig(make(map[string]HomePageCategory)) return HomePageConfig(make(map[string]HomePageCategory))
} }
func HomePageItemDefault() *HomePageItem {
return &HomePageItem{
Name: "Docker",
Category: "Uncategorized",
WidgetConfig: make(map[string]any),
}
}
func (c *HomePageConfig) Clear() { func (c *HomePageConfig) Clear() {
*c = make(HomePageConfig) *c = make(HomePageConfig)
} }
func (c HomePageConfig) Add(item HomePageItem) { func (c HomePageConfig) Add(item *HomePageItem) {
if c[item.Category] == nil { if c[item.Category] == nil {
c[item.Category] = make(HomePageCategory, 0) c[item.Category] = make(HomePageCategory, 0)
} }

View file

@ -8,7 +8,6 @@ import (
. "github.com/yusing/go-proxy/internal/common" . "github.com/yusing/go-proxy/internal/common"
D "github.com/yusing/go-proxy/internal/docker" D "github.com/yusing/go-proxy/internal/docker"
H "github.com/yusing/go-proxy/internal/homepage" 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" F "github.com/yusing/go-proxy/internal/utils/functional"
) )
@ -40,11 +39,6 @@ func (e *RawEntry) FillMissingFields() {
e.ProxyProperties = &D.ProxyProperties{} e.ProxyProperties = &D.ProxyProperties{}
} }
if e.Homepage == nil {
e.Homepage = H.HomePageItemDefault()
e.Homepage.Name = U.Title(e.Alias)
}
lp, pp, extra := e.splitPorts() lp, pp, extra := e.splitPorts()
if port, ok := ServiceNamePortMapTCP[e.ImageName]; ok { if port, ok := ServiceNamePortMapTCP[e.ImageName]; ok {

View file

@ -9,6 +9,7 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
D "github.com/yusing/go-proxy/internal/docker" D "github.com/yusing/go-proxy/internal/docker"
E "github.com/yusing/go-proxy/internal/error" E "github.com/yusing/go-proxy/internal/error"
H "github.com/yusing/go-proxy/internal/homepage"
M "github.com/yusing/go-proxy/internal/models" M "github.com/yusing/go-proxy/internal/models"
R "github.com/yusing/go-proxy/internal/route" R "github.com/yusing/go-proxy/internal/route"
W "github.com/yusing/go-proxy/internal/watcher" W "github.com/yusing/go-proxy/internal/watcher"
@ -184,6 +185,7 @@ func (p *DockerProvider) entriesFromContainerLabels(container D.Container) (entr
Alias: a, Alias: a,
Host: p.hostname, Host: p.hostname,
ProxyProperties: container.ProxyProperties, ProxyProperties: container.ProxyProperties,
Homepage: &H.HomePageItem{},
}) })
} }