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

@@ -4,8 +4,10 @@ import (
E "github.com/yusing/go-proxy/internal/error"
)
type Host string
type Subdomain = Alias
type (
Host string
Subdomain = Alias
)
func ValidateHost[String ~string](s String) (Host, E.NestedError) {
return Host(s), nil

View File

@@ -6,8 +6,12 @@ import (
E "github.com/yusing/go-proxy/internal/error"
)
type PathPattern string
type PathPatterns = []PathPattern
type (
PathPattern string
PathPatterns = []PathPattern
)
var pathPattern = regexp.MustCompile(`^(/[-\w./]*({\$\})?|((GET|POST|DELETE|PUT|HEAD|OPTION) /[-\w./]*({\$\})?))$`)
func NewPathPattern(s string) (PathPattern, E.NestedError) {
if len(s) == 0 {
@@ -25,13 +29,11 @@ func ValidatePathPatterns(s []string) (PathPatterns, E.NestedError) {
}
pp := make(PathPatterns, len(s))
for i, v := range s {
if pattern, err := NewPathPattern(v); err.HasError() {
pattern, err := NewPathPattern(v)
if err != nil {
return nil, err
} else {
pp[i] = pattern
}
pp[i] = pattern
}
return pp, nil
}
var pathPattern = regexp.MustCompile(`^(/[-\w./]*({\$\})?|((GET|POST|DELETE|PUT|HEAD|OPTION) /[-\w./]*({\$\})?))$`)

View File

@@ -1,7 +1,6 @@
package provider
import (
"fmt"
"regexp"
"strconv"
"strings"
@@ -9,7 +8,6 @@ import (
"github.com/sirupsen/logrus"
D "github.com/yusing/go-proxy/internal/docker"
E "github.com/yusing/go-proxy/internal/error"
R "github.com/yusing/go-proxy/internal/route"
"github.com/yusing/go-proxy/internal/types"
W "github.com/yusing/go-proxy/internal/watcher"
@@ -21,8 +19,10 @@ type DockerProvider struct {
ExplicitOnly bool
}
var AliasRefRegex = regexp.MustCompile(`#\d+`)
var AliasRefRegexOld = regexp.MustCompile(`\$\d+`)
var (
AliasRefRegex = regexp.MustCompile(`#\d+`)
AliasRefRegexOld = regexp.MustCompile(`\$\d+`)
)
func DockerProviderImpl(name, dockerHost string, explicitOnly bool) (ProviderImpl, E.NestedError) {
hostname, err := D.ParseDockerHostname(dockerHost)
@@ -33,7 +33,7 @@ func DockerProviderImpl(name, dockerHost string, explicitOnly bool) (ProviderImp
}
func (p *DockerProvider) String() string {
return fmt.Sprintf("docker: %s", p.name)
return "docker: " + p.name
}
func (p *DockerProvider) NewWatcher() W.Watcher {
@@ -49,7 +49,7 @@ func (p *DockerProvider) LoadRoutesImpl() (routes R.Routes, err E.NestedError) {
return routes, E.FailWith("connect to docker", err)
}
errors := E.NewBuilder("errors when parse docker labels")
errors := E.NewBuilder("errors in docker labels")
for _, c := range info.Containers {
container := D.FromDocker(&c, p.dockerHost)
@@ -172,7 +172,7 @@ func (p *DockerProvider) OnEvent(event W.Event, routes R.Routes) (res EventResul
}
// Returns a list of proxy entries for a container.
// Always non-nil
// Always non-nil.
func (p *DockerProvider) entriesFromContainerLabels(container D.Container) (entries types.RawEntries, _ E.NestedError) {
entries = types.NewProxyEntries()
@@ -206,7 +206,7 @@ func (p *DockerProvider) applyLabel(container D.Container, entries types.RawEntr
b := E.NewBuilder("errors in label %s", key)
defer b.To(&res)
refErr := E.NewBuilder("errors parsing alias references")
refErr := E.NewBuilder("errors in alias references")
replaceIndexRef := func(ref string) string {
index, err := strconv.Atoi(ref[1:])
if err != nil {
@@ -231,7 +231,7 @@ func (p *DockerProvider) applyLabel(container D.Container, entries types.RawEntr
// apply label for all aliases
entries.RangeAll(func(a string, e *types.RawEntry) {
if err = D.ApplyLabel(e, lbl); err.HasError() {
b.Add(err.Subjectf("alias %s", lbl.Target))
b.Add(err)
}
})
} else {
@@ -250,7 +250,7 @@ func (p *DockerProvider) applyLabel(container D.Container, entries types.RawEntr
return
}
if err = D.ApplyLabel(config, lbl); err.HasError() {
b.Add(err.Subjectf("alias %s", lbl.Target))
b.Add(err)
}
}
return

View File

@@ -99,31 +99,21 @@ func (p *Provider) GetType() ProviderType {
return p.t
}
// to work with json marshaller
// to work with json marshaller.
func (p *Provider) MarshalText() ([]byte, error) {
return []byte(p.String()), nil
}
func (p *Provider) StartAllRoutes() (res E.NestedError) {
errors := E.NewBuilder("errors in routes")
errors := E.NewBuilder("errors starting routes")
defer errors.To(&res)
// start watcher no matter load success or not
go p.watchEvents()
nStarted := 0
nFailed := 0
p.routes.RangeAllParallel(func(alias string, r R.Route) {
if err := r.Start(); err.HasError() {
errors.Add(err.Subject(r))
nFailed++
} else {
nStarted++
}
errors.Add(r.Start().Subject(r))
})
p.l.Debugf("%d routes started, %d failed", nStarted, nFailed)
return
}
@@ -133,20 +123,12 @@ func (p *Provider) StopAllRoutes() (res E.NestedError) {
p.watcherCancel = nil
}
errors := E.NewBuilder("errors stopping routes for provider %q", p.name)
errors := E.NewBuilder("errors stopping routes")
defer errors.To(&res)
nStopped := 0
nFailed := 0
p.routes.RangeAllParallel(func(alias string, r R.Route) {
if err := r.Stop(); err.HasError() {
errors.Add(err.Subject(r))
nFailed++
} else {
nStopped++
}
errors.Add(r.Stop().Subject(r))
})
p.l.Debugf("%d routes stopped, %d failed", nStopped, nFailed)
return
}
@@ -165,6 +147,9 @@ func (p *Provider) LoadRoutes() E.NestedError {
p.l.Infof("loaded %d routes", p.routes.Size())
return err
}
if err == nil {
return nil
}
return E.FailWith("loading routes", err)
}