initial support of homepage labels

This commit is contained in:
yusing 2024-10-03 02:53:05 +08:00
parent ae61a2335d
commit 90130411f9
7 changed files with 76 additions and 39 deletions

View file

@ -17,6 +17,7 @@ const (
ListMiddlewares = "middlewares"
ListMiddlewareTrace = "middleware_trace"
ListMatchDomains = "match_domains"
ListHomepageConfig = "homepage_config"
)
func List(cfg *config.Config, w http.ResponseWriter, r *http.Request) {
@ -36,6 +37,8 @@ func List(cfg *config.Config, w http.ResponseWriter, r *http.Request) {
listMiddlewareTrace(w, r)
case ListMatchDomains:
listMatchDomains(cfg, w, r)
case ListHomepageConfig:
listHomepageConfig(cfg, w, r)
default:
U.HandleErr(w, r, U.ErrInvalidKey("what"), http.StatusBadRequest)
}
@ -78,3 +81,7 @@ func listMiddlewares(w http.ResponseWriter, r *http.Request) {
func listMatchDomains(cfg *config.Config, w http.ResponseWriter, r *http.Request) {
U.HandleErr(w, r, U.RespondJson(w, cfg.Value().MatchDomains))
}
func listHomepageConfig(cfg *config.Config, w http.ResponseWriter, r *http.Request) {
U.HandleErr(w, r, U.RespondJson(w, cfg.HomepageConfig()))
}

View file

@ -1,6 +1,7 @@
package config
import (
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"
R "github.com/yusing/go-proxy/internal/route"
@ -24,6 +25,17 @@ func (cfg *Config) DumpProviders() map[string]*PR.Provider {
return entries
}
func (cfg *Config) HomepageConfig() H.HomePageConfig {
hpCfg := H.NewHomePageConfig()
cfg.forEachRoute(func(alias string, r R.Route, p *PR.Provider) {
if !r.Started() {
return
}
hpCfg.Add(*r.Entry().Homepage)
})
return hpCfg
}
func (cfg *Config) RoutesByAlias() map[string]U.SerializedObject {
routes := make(map[string]U.SerializedObject)
cfg.forEachRoute(func(alias string, r R.Route, p *PR.Provider) {

View file

@ -1,32 +0,0 @@
package docker
type (
HomePageConfig struct{ m map[string]HomePageCategory }
HomePageCategory []HomePageItem
HomePageItem struct {
Name string
Icon string
Category string
Description string
WidgetConfig map[string]any
}
)
func NewHomePageConfig() *HomePageConfig {
return &HomePageConfig{m: make(map[string]HomePageCategory)}
}
func NewHomePageItem() *HomePageItem {
return &HomePageItem{}
}
func (c *HomePageConfig) Clear() {
c.m = make(map[string]HomePageCategory)
}
func (c *HomePageConfig) Add(item HomePageItem) {
c.m[item.Category] = HomePageCategory{item}
}
const NSHomePage = "homepage"

View file

@ -4,6 +4,7 @@ const (
WildcardAlias = "*"
NSProxy = "proxy"
NSHomePage = "homepage"
LabelAliases = NSProxy + ".aliases"
LabelExclude = NSProxy + ".exclude"

View file

@ -0,0 +1,37 @@
package homepage
type (
HomePageConfig map[string]HomePageCategory
HomePageCategory []HomePageItem
HomePageItem struct {
Name string `yaml:"name" json:"name"`
Icon string `yaml:"icon" json:"icon,omitempty"` // URL or unicodes
Category string `yaml:"category" json:"category"`
Description string `yaml:"description" json:"description,omitempty"`
WidgetConfig map[string]any `yaml:",flow" json:"widget_config,omitempty"`
}
)
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) {
if c[item.Category] == nil {
c[item.Category] = make(HomePageCategory, 0)
}
c[item.Category] = append(c[item.Category], item)
}

View file

@ -7,6 +7,8 @@ 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"
)
@ -18,9 +20,10 @@ type (
Scheme string `yaml:"scheme" json:"scheme"`
Host string `yaml:"host" json:"host"`
Port string `yaml:"port" json:"port"`
NoTLSVerify bool `yaml:"no_tls_verify" json:"no_tls_verify"` // https proxy only
PathPatterns []string `yaml:"path_patterns" json:"path_patterns"` // http(s) proxy only
Middlewares D.NestedLabelMap `yaml:"middlewares" json:"middlewares"`
NoTLSVerify bool `yaml:"no_tls_verify" json:"no_tls_verify,omitempty"` // https proxy only
PathPatterns []string `yaml:"path_patterns" json:"path_patterns,omitempty"` // http(s) proxy only
Middlewares D.NestedLabelMap `yaml:"middlewares" json:"middlewares,omitempty"`
Homepage *H.HomePageItem `yaml:"homepage" json:"homepage"`
/* Docker only */
*D.ProxyProperties `yaml:"-" json:"proxy_properties"`
@ -37,6 +40,11 @@ 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 {

View file

@ -5,9 +5,13 @@ import (
"strconv"
"strings"
E "github.com/yusing/go-proxy/internal/error"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
// TODO: support other languages
var titleCaser = cases.Title(language.AmericanEnglish)
func CommaSeperatedList(s string) []string {
res := strings.Split(s, ",")
for i, part := range res {
@ -16,8 +20,8 @@ func CommaSeperatedList(s string) []string {
return res
}
func IntParser(value string) (int, E.NestedError) {
return E.Check(strconv.Atoi(value))
func Title(s string) string {
return titleCaser.String(s)
}
func ExtractPort(fullURL string) (int, error) {