added golangci-linting, refactor, simplified error msgs and fixed some error handling

This commit is contained in:
yusing
2024-10-10 11:52:09 +08:00
parent d91b66ae87
commit da04a0dff4
63 changed files with 690 additions and 410 deletions

View File

@@ -2,12 +2,12 @@ package types
type (
AutoCertConfig struct {
Email string `json:"email"`
Domains []string `yaml:",flow" json:"domains"`
CertPath string `yaml:"cert_path" json:"cert_path"`
KeyPath string `yaml:"key_path" json:"key_path"`
Provider string `json:"provider"`
Options AutocertProviderOpt `yaml:",flow" json:"options"`
Email string `json:"email,omitempty" yaml:"email"`
Domains []string `json:"domains,omitempty" yaml:",flow"`
CertPath string `json:"cert_path,omitempty" yaml:"cert_path"`
KeyPath string `json:"key_path,omitempty" yaml:"key_path"`
Provider string `json:"provider,omitempty" yaml:"provider"`
Options AutocertProviderOpt `json:"options,omitempty" yaml:",flow"`
}
AutocertProviderOpt map[string]any
)

View File

@@ -1,12 +1,12 @@
package types
type Config struct {
Providers ProxyProviders `yaml:",flow" json:"providers"`
AutoCert AutoCertConfig `yaml:",flow" json:"autocert"`
ExplicitOnly bool `yaml:"explicit_only" json:"explicit_only"`
MatchDomains []string `yaml:"match_domains" json:"match_domains"`
TimeoutShutdown int `yaml:"timeout_shutdown" json:"timeout_shutdown"`
RedirectToHTTPS bool `yaml:"redirect_to_https" json:"redirect_to_https"`
Providers ProxyProviders `json:"providers" yaml:",flow"`
AutoCert AutoCertConfig `json:"autocert" yaml:",flow"`
ExplicitOnly bool `json:"explicit_only" yaml:"explicit_only"`
MatchDomains []string `json:"match_domains" yaml:"match_domains"`
TimeoutShutdown int `json:"timeout_shutdown" yaml:"timeout_shutdown"`
RedirectToHTTPS bool `json:"redirect_to_https" yaml:"redirect_to_https"`
}
func DefaultConfig() *Config {

View File

@@ -1,6 +1,6 @@
package types
type ProxyProviders struct {
Files []string `yaml:"include" json:"include"` // docker, file
Docker map[string]string `yaml:"docker" json:"docker"`
Files []string `json:"include" yaml:"include"` // docker, file
Docker map[string]string `json:"docker" yaml:"docker"`
}

View File

@@ -1,14 +1,14 @@
package types
import (
"fmt"
"strconv"
"strings"
. "github.com/yusing/go-proxy/internal/common"
"github.com/yusing/go-proxy/internal/common"
D "github.com/yusing/go-proxy/internal/docker"
H "github.com/yusing/go-proxy/internal/homepage"
"github.com/yusing/go-proxy/internal/net/http/loadbalancer"
U "github.com/yusing/go-proxy/internal/utils"
F "github.com/yusing/go-proxy/internal/utils/functional"
)
@@ -16,18 +16,18 @@ type (
RawEntry struct {
// raw entry object before validation
// loaded from docker labels or yaml file
Alias string `yaml:"-" json:"-"`
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,omitempty"` // https proxy only
PathPatterns []string `yaml:"path_patterns" json:"path_patterns,omitempty"` // http(s) proxy only
LoadBalance loadbalancer.Config `yaml:"load_balance" json:"load_balance"`
Middlewares D.NestedLabelMap `yaml:"middlewares" json:"middlewares,omitempty"`
Homepage *H.HomePageItem `yaml:"homepage" json:"homepage,omitempty"`
Alias string `json:"-" yaml:"-"`
Scheme string `json:"scheme" yaml:"scheme"`
Host string `json:"host" yaml:"host"`
Port string `json:"port" yaml:"port"`
NoTLSVerify bool `json:"no_tls_verify,omitempty" yaml:"no_tls_verify"` // https proxy only
PathPatterns []string `json:"path_patterns,omitempty" yaml:"path_patterns"` // http(s) proxy only
LoadBalance loadbalancer.Config `json:"load_balance" yaml:"load_balance"`
Middlewares D.NestedLabelMap `json:"middlewares,omitempty" yaml:"middlewares"`
Homepage *H.HomePageItem `json:"homepage,omitempty" yaml:"homepage"`
/* Docker only */
*D.ProxyProperties `yaml:"-" json:"proxy_properties"`
*D.ProxyProperties `json:"proxy_properties" yaml:"-"`
}
RawEntries = F.Map[string, *RawEntry]
@@ -43,14 +43,14 @@ func (e *RawEntry) FillMissingFields() {
lp, pp, extra := e.splitPorts()
if port, ok := ServiceNamePortMapTCP[e.ImageName]; ok {
if port, ok := common.ServiceNamePortMapTCP[e.ImageName]; ok {
if pp == "" {
pp = strconv.Itoa(port)
}
if e.Scheme == "" {
e.Scheme = "tcp"
}
} else if port, ok := ImageNamePortMap[e.ImageName]; ok {
} else if port, ok := common.ImageNamePortMap[e.ImageName]; ok {
if pp == "" {
pp = strconv.Itoa(port)
}
@@ -61,7 +61,7 @@ func (e *RawEntry) FillMissingFields() {
pp = "443"
} else if pp == "" {
if p, ok := F.FirstValueOf(e.PrivatePortMapping); ok {
pp = fmt.Sprint(p.PrivatePort)
pp = U.PortString(p.PrivatePort)
} else if !isDocker {
pp = "80"
}
@@ -70,12 +70,12 @@ func (e *RawEntry) FillMissingFields() {
// replace private port with public port (if any)
if isDocker && e.NetworkMode != "host" {
if p, ok := e.PrivatePortMapping[pp]; ok {
pp = fmt.Sprint(p.PublicPort)
pp = U.PortString(p.PublicPort)
}
if _, ok := e.PublicPortMapping[pp]; !ok { // port is not exposed, but specified
// try to fallback to first public port
if p, ok := F.FirstValueOf(e.PublicPortMapping); ok {
pp = fmt.Sprint(p.PublicPort)
pp = U.PortString(p.PublicPort)
}
}
}
@@ -87,13 +87,12 @@ func (e *RawEntry) FillMissingFields() {
}
if e.Scheme == "" {
if lp != "" {
switch {
case lp != "":
e.Scheme = "tcp"
} else if strings.HasSuffix(pp, "443") {
case strings.HasSuffix(pp, "443"):
e.Scheme = "https"
} else if _, ok := WellKnownHTTPPorts[pp]; ok {
e.Scheme = "http"
} else {
default:
// assume its http
e.Scheme = "http"
}
@@ -103,16 +102,16 @@ func (e *RawEntry) FillMissingFields() {
e.Host = "localhost"
}
if e.IdleTimeout == "" {
e.IdleTimeout = IdleTimeoutDefault
e.IdleTimeout = common.IdleTimeoutDefault
}
if e.WakeTimeout == "" {
e.WakeTimeout = WakeTimeoutDefault
e.WakeTimeout = common.WakeTimeoutDefault
}
if e.StopTimeout == "" {
e.StopTimeout = StopTimeoutDefault
e.StopTimeout = common.StopTimeoutDefault
}
if e.StopMethod == "" {
e.StopMethod = StopMethodDefault
e.StopMethod = common.StopMethodDefault
}
e.Port = joinPorts(lp, pp, extra)