refactor: moved models/ to types/

This commit is contained in:
yusing 2024-10-04 08:47:53 +08:00
parent e566fd9b57
commit e8f4cd18a4
12 changed files with 48 additions and 44 deletions

View file

@ -8,12 +8,13 @@ import (
"github.com/go-acme/lego/v4/certcrypto" "github.com/go-acme/lego/v4/certcrypto"
"github.com/go-acme/lego/v4/lego" "github.com/go-acme/lego/v4/lego"
E "github.com/yusing/go-proxy/internal/error" E "github.com/yusing/go-proxy/internal/error"
M "github.com/yusing/go-proxy/internal/models"
"github.com/yusing/go-proxy/internal/types"
) )
type Config M.AutoCertConfig type Config types.AutoCertConfig
func NewConfig(cfg *M.AutoCertConfig) *Config { func NewConfig(cfg *types.AutoCertConfig) *Config {
if cfg.CertPath == "" { if cfg.CertPath == "" {
cfg.CertPath = CertFileDefault cfg.CertPath = CertFileDefault
} }

View file

@ -15,7 +15,8 @@ import (
"github.com/go-acme/lego/v4/lego" "github.com/go-acme/lego/v4/lego"
"github.com/go-acme/lego/v4/registration" "github.com/go-acme/lego/v4/registration"
E "github.com/yusing/go-proxy/internal/error" E "github.com/yusing/go-proxy/internal/error"
M "github.com/yusing/go-proxy/internal/models" "github.com/yusing/go-proxy/internal/types"
U "github.com/yusing/go-proxy/internal/utils" U "github.com/yusing/go-proxy/internal/utils"
) )
@ -29,7 +30,7 @@ type Provider struct {
certExpiries CertExpiries certExpiries CertExpiries
} }
type ProviderGenerator func(M.AutocertProviderOpt) (challenge.Provider, E.NestedError) type ProviderGenerator func(types.AutocertProviderOpt) (challenge.Provider, E.NestedError)
type CertExpiries map[string]time.Time type CertExpiries map[string]time.Time
func (p *Provider) GetCert(_ *tls.ClientHelloInfo) (*tls.Certificate, error) { func (p *Provider) GetCert(_ *tls.ClientHelloInfo) (*tls.Certificate, error) {
@ -280,7 +281,7 @@ func providerGenerator[CT any, PT challenge.Provider](
defaultCfg func() *CT, defaultCfg func() *CT,
newProvider func(*CT) (PT, error), newProvider func(*CT) (PT, error),
) ProviderGenerator { ) ProviderGenerator {
return func(opt M.AutocertProviderOpt) (challenge.Provider, E.NestedError) { return func(opt types.AutocertProviderOpt) (challenge.Provider, E.NestedError) {
cfg := defaultCfg() cfg := defaultCfg()
err := U.Deserialize(opt, cfg) err := U.Deserialize(opt, cfg)
if err.HasError() { if err.HasError() {

View file

@ -8,9 +8,10 @@ import (
"github.com/yusing/go-proxy/internal/autocert" "github.com/yusing/go-proxy/internal/autocert"
"github.com/yusing/go-proxy/internal/common" "github.com/yusing/go-proxy/internal/common"
E "github.com/yusing/go-proxy/internal/error" E "github.com/yusing/go-proxy/internal/error"
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"
R "github.com/yusing/go-proxy/internal/route" R "github.com/yusing/go-proxy/internal/route"
"github.com/yusing/go-proxy/internal/types"
U "github.com/yusing/go-proxy/internal/utils" 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"
W "github.com/yusing/go-proxy/internal/watcher" W "github.com/yusing/go-proxy/internal/watcher"
@ -19,7 +20,7 @@ import (
) )
type Config struct { type Config struct {
value *M.Config value *types.Config
proxyProviders F.Map[string, *PR.Provider] proxyProviders F.Map[string, *PR.Provider]
autocertProvider *autocert.Provider autocertProvider *autocert.Provider
@ -42,7 +43,7 @@ func Load() E.NestedError {
return nil return nil
} }
instance = &Config{ instance = &Config{
value: M.DefaultConfig(), value: types.DefaultConfig(),
proxyProviders: F.NewMapOf[string, *PR.Provider](), proxyProviders: F.NewMapOf[string, *PR.Provider](),
l: logrus.WithField("module", "config"), l: logrus.WithField("module", "config"),
watcher: W.NewConfigFileWatcher(common.ConfigFileName), watcher: W.NewConfigFileWatcher(common.ConfigFileName),
@ -62,7 +63,7 @@ func MatchDomains() []string {
return instance.value.MatchDomains return instance.value.MatchDomains
} }
func (cfg *Config) Value() M.Config { func (cfg *Config) Value() types.Config {
if cfg == nil { if cfg == nil {
logrus.Panic("config has not been loaded, please check if there is any errors") logrus.Panic("config has not been loaded, please check if there is any errors")
} }
@ -158,7 +159,7 @@ func (cfg *Config) load() (res E.NestedError) {
} }
} }
model := M.DefaultConfig() model := types.DefaultConfig()
if err := E.From(yaml.Unmarshal(data, model)); err.HasError() { if err := E.From(yaml.Unmarshal(data, model)); err.HasError() {
b.Add(E.FailWith("parse config", err)) b.Add(E.FailWith("parse config", err))
logrus.Fatal(b.Build()) logrus.Fatal(b.Build())
@ -173,7 +174,7 @@ func (cfg *Config) load() (res E.NestedError) {
return return
} }
func (cfg *Config) initAutoCert(autocertCfg *M.AutoCertConfig) (err E.NestedError) { func (cfg *Config) initAutoCert(autocertCfg *types.AutoCertConfig) (err E.NestedError) {
if cfg.autocertProvider != nil { if cfg.autocertProvider != nil {
return return
} }
@ -188,7 +189,7 @@ func (cfg *Config) initAutoCert(autocertCfg *M.AutoCertConfig) (err E.NestedErro
return return
} }
func (cfg *Config) loadProviders(providers *M.ProxyProviders) (res E.NestedError) { func (cfg *Config) loadProviders(providers *types.ProxyProviders) (res E.NestedError) {
cfg.l.Debug("loading providers") cfg.l.Debug("loading providers")
defer cfg.l.Debug("loaded providers") defer cfg.l.Debug("loaded providers")

View file

@ -7,15 +7,15 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/yusing/go-proxy/internal/common" "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"
PR "github.com/yusing/go-proxy/internal/proxy/provider" PR "github.com/yusing/go-proxy/internal/proxy/provider"
R "github.com/yusing/go-proxy/internal/route" R "github.com/yusing/go-proxy/internal/route"
"github.com/yusing/go-proxy/internal/types"
U "github.com/yusing/go-proxy/internal/utils" 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"
) )
func (cfg *Config) DumpEntries() map[string]*M.RawEntry { func (cfg *Config) DumpEntries() map[string]*types.RawEntry {
entries := make(map[string]*M.RawEntry) entries := make(map[string]*types.RawEntry)
cfg.forEachRoute(func(alias string, r R.Route, p *PR.Provider) { cfg.forEachRoute(func(alias string, r R.Route, p *PR.Provider) {
entries[alias] = r.Entry() entries[alias] = r.Entry()
}) })

View file

@ -7,8 +7,8 @@ import (
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"
M "github.com/yusing/go-proxy/internal/models"
T "github.com/yusing/go-proxy/internal/proxy/fields" T "github.com/yusing/go-proxy/internal/proxy/fields"
"github.com/yusing/go-proxy/internal/types"
) )
type ( type (
@ -47,7 +47,7 @@ func (rp *ReverseProxyEntry) IsDocker() bool {
return rp.DockerHost != "" return rp.DockerHost != ""
} }
func ValidateEntry(m *M.RawEntry) (any, E.NestedError) { func ValidateEntry(m *types.RawEntry) (any, E.NestedError) {
m.FillMissingFields() m.FillMissingFields()
scheme, err := T.NewScheme(m.Scheme) scheme, err := T.NewScheme(m.Scheme)
@ -68,7 +68,7 @@ func ValidateEntry(m *M.RawEntry) (any, E.NestedError) {
return entry, nil return entry, nil
} }
func validateRPEntry(m *M.RawEntry, s T.Scheme, b E.Builder) *ReverseProxyEntry { func validateRPEntry(m *types.RawEntry, s T.Scheme, b E.Builder) *ReverseProxyEntry {
var stopTimeOut time.Duration var stopTimeOut time.Duration
host, err := T.ValidateHost(m.Host) host, err := T.ValidateHost(m.Host)
@ -123,7 +123,7 @@ func validateRPEntry(m *M.RawEntry, s T.Scheme, b E.Builder) *ReverseProxyEntry
} }
} }
func validateStreamEntry(m *M.RawEntry, b E.Builder) *StreamEntry { func validateStreamEntry(m *types.RawEntry, b E.Builder) *StreamEntry {
host, err := T.ValidateHost(m.Host) host, err := T.ValidateHost(m.Host)
b.Add(err) b.Add(err)

View file

@ -9,8 +9,9 @@ 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"
M "github.com/yusing/go-proxy/internal/models"
R "github.com/yusing/go-proxy/internal/route" R "github.com/yusing/go-proxy/internal/route"
"github.com/yusing/go-proxy/internal/types"
W "github.com/yusing/go-proxy/internal/watcher" W "github.com/yusing/go-proxy/internal/watcher"
"github.com/yusing/go-proxy/internal/watcher/events" "github.com/yusing/go-proxy/internal/watcher/events"
) )
@ -41,7 +42,7 @@ func (p *DockerProvider) NewWatcher() W.Watcher {
func (p *DockerProvider) LoadRoutesImpl() (routes R.Routes, err E.NestedError) { func (p *DockerProvider) LoadRoutesImpl() (routes R.Routes, err E.NestedError) {
routes = R.NewRoutes() routes = R.NewRoutes()
entries := M.NewProxyEntries() entries := types.NewProxyEntries()
info, err := D.GetClientInfo(p.dockerHost, true) info, err := D.GetClientInfo(p.dockerHost, true)
if err.HasError() { if err.HasError() {
@ -64,12 +65,12 @@ func (p *DockerProvider) LoadRoutesImpl() (routes R.Routes, err E.NestedError) {
// there may be some valid entries in `en` // there may be some valid entries in `en`
dups := entries.MergeFrom(newEntries) dups := entries.MergeFrom(newEntries)
// add the duplicate proxy entries to the error // add the duplicate proxy entries to the error
dups.RangeAll(func(k string, v *M.RawEntry) { dups.RangeAll(func(k string, v *types.RawEntry) {
errors.Addf("duplicate alias %s", k) errors.Addf("duplicate alias %s", k)
}) })
} }
entries.RangeAll(func(_ string, e *M.RawEntry) { entries.RangeAll(func(_ string, e *types.RawEntry) {
e.DockerHost = p.dockerHost e.DockerHost = p.dockerHost
}) })
@ -152,7 +153,7 @@ func (p *DockerProvider) OnEvent(event W.Event, routes R.Routes) (res EventResul
entries, err := p.entriesFromContainerLabels(cont) entries, err := p.entriesFromContainerLabels(cont)
b.Add(err) b.Add(err)
entries.RangeAll(func(alias string, entry *M.RawEntry) { entries.RangeAll(func(alias string, entry *types.RawEntry) {
if routes.Has(alias) { if routes.Has(alias) {
b.Add(E.Duplicated("alias", alias)) b.Add(E.Duplicated("alias", alias))
} else { } else {
@ -171,8 +172,8 @@ func (p *DockerProvider) OnEvent(event W.Event, routes R.Routes) (res EventResul
// Returns a list of proxy entries for a container. // Returns a list of proxy entries for a container.
// Always non-nil // Always non-nil
func (p *DockerProvider) entriesFromContainerLabels(container D.Container) (entries M.RawEntries, _ E.NestedError) { func (p *DockerProvider) entriesFromContainerLabels(container D.Container) (entries types.RawEntries, _ E.NestedError) {
entries = M.NewProxyEntries() entries = types.NewProxyEntries()
if p.shouldIgnore(container) { if p.shouldIgnore(container) {
return return
@ -180,7 +181,7 @@ func (p *DockerProvider) entriesFromContainerLabels(container D.Container) (entr
// init entries map for all aliases // init entries map for all aliases
for _, a := range container.Aliases { for _, a := range container.Aliases {
entries.Store(a, &M.RawEntry{ entries.Store(a, &types.RawEntry{
Alias: a, Alias: a,
Host: p.hostname, Host: p.hostname,
ProxyProperties: container.ProxyProperties, ProxyProperties: container.ProxyProperties,
@ -193,14 +194,14 @@ func (p *DockerProvider) entriesFromContainerLabels(container D.Container) (entr
} }
// remove all entries that failed to fill in missing fields // remove all entries that failed to fill in missing fields
entries.RangeAll(func(_ string, re *M.RawEntry) { entries.RangeAll(func(_ string, re *types.RawEntry) {
re.FillMissingFields() re.FillMissingFields()
}) })
return entries, errors.Build().Subject(container.ContainerName) return entries, errors.Build().Subject(container.ContainerName)
} }
func (p *DockerProvider) applyLabel(container D.Container, entries M.RawEntries, key, val string) (res E.NestedError) { func (p *DockerProvider) applyLabel(container D.Container, entries types.RawEntries, key, val string) (res E.NestedError) {
b := E.NewBuilder("errors in label %s", key) b := E.NewBuilder("errors in label %s", key)
defer b.To(&res) defer b.To(&res)
@ -227,7 +228,7 @@ func (p *DockerProvider) applyLabel(container D.Container, entries M.RawEntries,
} }
if lbl.Target == D.WildcardAlias { if lbl.Target == D.WildcardAlias {
// apply label for all aliases // apply label for all aliases
entries.RangeAll(func(a string, e *M.RawEntry) { entries.RangeAll(func(a string, e *types.RawEntry) {
if err = D.ApplyLabel(e, lbl); err.HasError() { if err = D.ApplyLabel(e, lbl); err.HasError() {
b.Add(err.Subjectf("alias %s", lbl.Target)) b.Add(err.Subjectf("alias %s", lbl.Target))
} }

View file

@ -7,8 +7,8 @@ import (
"github.com/yusing/go-proxy/internal/common" "github.com/yusing/go-proxy/internal/common"
E "github.com/yusing/go-proxy/internal/error" E "github.com/yusing/go-proxy/internal/error"
M "github.com/yusing/go-proxy/internal/models"
R "github.com/yusing/go-proxy/internal/route" R "github.com/yusing/go-proxy/internal/route"
"github.com/yusing/go-proxy/internal/types"
U "github.com/yusing/go-proxy/internal/utils" U "github.com/yusing/go-proxy/internal/utils"
W "github.com/yusing/go-proxy/internal/watcher" W "github.com/yusing/go-proxy/internal/watcher"
) )
@ -71,7 +71,7 @@ func (p *FileProvider) LoadRoutesImpl() (routes R.Routes, res E.NestedError) {
b := E.NewBuilder("file %q validation failure", p.fileName) b := E.NewBuilder("file %q validation failure", p.fileName)
defer b.To(&res) defer b.To(&res)
entries := M.NewProxyEntries() entries := types.NewProxyEntries()
data, err := E.Check(os.ReadFile(p.path)) data, err := E.Check(os.ReadFile(p.path))
if err.HasError() { if err.HasError() {

View file

@ -5,15 +5,15 @@ import (
"net/url" "net/url"
E "github.com/yusing/go-proxy/internal/error" E "github.com/yusing/go-proxy/internal/error"
M "github.com/yusing/go-proxy/internal/models"
P "github.com/yusing/go-proxy/internal/proxy" P "github.com/yusing/go-proxy/internal/proxy"
"github.com/yusing/go-proxy/internal/types"
F "github.com/yusing/go-proxy/internal/utils/functional" F "github.com/yusing/go-proxy/internal/utils/functional"
) )
type ( type (
Route interface { Route interface {
RouteImpl RouteImpl
Entry() *M.RawEntry Entry() *types.RawEntry
Type() RouteType Type() RouteType
URL() *url.URL URL() *url.URL
} }
@ -29,7 +29,7 @@ type (
route struct { route struct {
RouteImpl RouteImpl
type_ RouteType type_ RouteType
entry *M.RawEntry entry *types.RawEntry
} }
) )
@ -41,7 +41,7 @@ const (
// function alias // function alias
var NewRoutes = F.NewMapOf[string, Route] var NewRoutes = F.NewMapOf[string, Route]
func NewRoute(en *M.RawEntry) (Route, E.NestedError) { func NewRoute(en *types.RawEntry) (Route, E.NestedError) {
entry, err := P.ValidateEntry(en) entry, err := P.ValidateEntry(en)
if err != nil { if err != nil {
return nil, err return nil, err
@ -65,7 +65,7 @@ func NewRoute(en *M.RawEntry) (Route, E.NestedError) {
return &route{RouteImpl: rt, entry: en, type_: t}, nil return &route{RouteImpl: rt, entry: en, type_: t}, nil
} }
func (rt *route) Entry() *M.RawEntry { func (rt *route) Entry() *types.RawEntry {
return rt.entry return rt.entry
} }
@ -78,11 +78,11 @@ func (rt *route) URL() *url.URL {
return url return url
} }
func FromEntries(entries M.RawEntries) (Routes, E.NestedError) { func FromEntries(entries types.RawEntries) (Routes, E.NestedError) {
b := E.NewBuilder("errors in routes") b := E.NewBuilder("errors in routes")
routes := NewRoutes() routes := NewRoutes()
entries.RangeAll(func(alias string, entry *M.RawEntry) { entries.RangeAll(func(alias string, entry *types.RawEntry) {
entry.Alias = alias entry.Alias = alias
r, err := NewRoute(entry) r, err := NewRoute(entry)
if err.HasError() { if err.HasError() {

View file

@ -1,4 +1,4 @@
package model package types
type ( type (
AutoCertConfig struct { AutoCertConfig struct {

View file

@ -1,4 +1,4 @@
package model package types
type Config struct { type Config struct {
Providers ProxyProviders `yaml:",flow" json:"providers"` Providers ProxyProviders `yaml:",flow" json:"providers"`

View file

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

View file

@ -1,4 +1,4 @@
package model package types
import ( import (
"fmt" "fmt"