From c59567ae8f19679b163adc959286e28102497103 Mon Sep 17 00:00:00 2001 From: yusing Date: Tue, 8 Apr 2025 05:04:49 +0800 Subject: [PATCH] refactor: rename module route/types to route --- internal/route/route.go | 38 ++++++++++----------- internal/route/route_test.go | 42 ++++++++++++------------ internal/route/routes/routes.go | 32 +++++++++--------- internal/route/types/http_config.go | 2 +- internal/route/types/http_config_test.go | 10 +++--- internal/route/types/port.go | 6 ++-- internal/route/types/port_test.go | 2 +- internal/route/types/route.go | 7 +++- internal/route/types/route_type.go | 2 +- internal/route/types/scheme.go | 2 +- 10 files changed, 74 insertions(+), 69 deletions(-) diff --git a/internal/route/route.go b/internal/route/route.go index 3441488..74e78e5 100644 --- a/internal/route/route.go +++ b/internal/route/route.go @@ -21,7 +21,7 @@ import ( "github.com/yusing/go-proxy/internal/net/gphttp/accesslog" loadbalance "github.com/yusing/go-proxy/internal/net/gphttp/loadbalancer/types" "github.com/yusing/go-proxy/internal/route/rules" - "github.com/yusing/go-proxy/internal/route/types" + route "github.com/yusing/go-proxy/internal/route/types" "github.com/yusing/go-proxy/internal/utils" ) @@ -30,12 +30,12 @@ type ( _ utils.NoCopy Alias string `json:"alias"` - Scheme types.Scheme `json:"scheme,omitempty"` + Scheme route.Scheme `json:"scheme,omitempty"` Host string `json:"host,omitempty"` - Port types.Port `json:"port,omitempty"` + Port route.Port `json:"port,omitempty"` Root string `json:"root,omitempty"` - types.HTTPConfig + route.HTTPConfig PathPatterns []string `json:"path_patterns,omitempty"` Rules rules.Rules `json:"rules,omitempty" validate:"omitempty,unique=Name"` HealthCheck *health.HealthCheckConfig `json:"healthcheck,omitempty"` @@ -57,7 +57,7 @@ type ( ProxyURL *net.URL `json:"purl,omitempty"` Idlewatcher *idlewatcher.Config `json:"idlewatcher,omitempty"` - impl types.Route + impl route.Route isValidated bool } Routes map[string]*Route @@ -80,7 +80,7 @@ func (r *Route) Validate() (err gperr.Error) { case "localhost", "127.0.0.1": switch r.Port.Proxy { case common.ProxyHTTPPort, common.ProxyHTTPSPort, common.APIHTTPPort: - if r.Scheme.IsReverseProxy() || r.Scheme == types.SchemeTCP { + if r.Scheme.IsReverseProxy() || r.Scheme == route.SchemeTCP { return gperr.Errorf("localhost:%d is reserved for godoxy", r.Port.Proxy) } } @@ -89,17 +89,17 @@ func (r *Route) Validate() (err gperr.Error) { errs := gperr.NewBuilder("entry validation failed") switch r.Scheme { - case types.SchemeFileServer: + case route.SchemeFileServer: r.impl, err = NewFileServer(r) if err != nil { errs.Add(err) } - case types.SchemeHTTP, types.SchemeHTTPS: + case route.SchemeHTTP, route.SchemeHTTPS: if r.Port.Listening != 0 { errs.Addf("unexpected listening port for %s scheme", r.Scheme) } fallthrough - case types.SchemeTCP, types.SchemeUDP: + case route.SchemeTCP, route.SchemeUDP: r.LisURL = gperr.Collect(errs, net.ParseURL, fmt.Sprintf("%s://:%d", r.Scheme, r.Port.Listening)) fallthrough default: @@ -119,11 +119,11 @@ func (r *Route) Validate() (err gperr.Error) { } switch r.Scheme { - case types.SchemeFileServer: + case route.SchemeFileServer: r.impl, err = NewFileServer(r) - case types.SchemeHTTP, types.SchemeHTTPS: + case route.SchemeHTTP, route.SchemeHTTPS: r.impl, err = NewReverseProxyRoute(r) - case types.SchemeTCP, types.SchemeUDP: + case route.SchemeTCP, route.SchemeUDP: r.impl, err = NewStreamRoute(r) default: panic(fmt.Errorf("unexpected scheme %s for alias %s", r.Scheme, r.Alias)) @@ -164,12 +164,12 @@ func (r *Route) TargetURL() *net.URL { return r.ProxyURL } -func (r *Route) Type() types.RouteType { +func (r *Route) Type() route.RouteType { switch r.Scheme { - case types.SchemeHTTP, types.SchemeHTTPS, types.SchemeFileServer: - return types.RouteTypeHTTP - case types.SchemeTCP, types.SchemeUDP: - return types.RouteTypeStream + case route.SchemeHTTP, route.SchemeHTTPS, route.SchemeFileServer: + return route.RouteTypeHTTP + case route.SchemeTCP, route.SchemeUDP: + return route.RouteTypeStream } panic(fmt.Errorf("unexpected scheme %s for alias %s", r.Scheme, r.Alias)) } @@ -290,7 +290,7 @@ func (r *Route) Finalize() { scheme, port, ok := getSchemePortByImageName(cont.Image.Name) if ok { if r.Scheme == "" { - r.Scheme = types.Scheme(scheme) + r.Scheme = route.Scheme(scheme) } if pp == 0 { pp = port @@ -300,7 +300,7 @@ func (r *Route) Finalize() { if scheme, port, ok := getSchemePortByAlias(r.Alias); ok { if r.Scheme == "" { - r.Scheme = types.Scheme(scheme) + r.Scheme = route.Scheme(scheme) } if pp == 0 { pp = port diff --git a/internal/route/route_test.go b/internal/route/route_test.go index 0df3c59..b126db9 100644 --- a/internal/route/route_test.go +++ b/internal/route/route_test.go @@ -7,7 +7,7 @@ import ( "github.com/yusing/go-proxy/internal/common" "github.com/yusing/go-proxy/internal/docker" loadbalance "github.com/yusing/go-proxy/internal/net/gphttp/loadbalancer/types" - "github.com/yusing/go-proxy/internal/route/types" + route "github.com/yusing/go-proxy/internal/route/types" "github.com/yusing/go-proxy/internal/watcher/health" ) @@ -15,9 +15,9 @@ func TestRouteValidate(t *testing.T) { t.Run("AlreadyValidated", func(t *testing.T) { r := &Route{ Alias: "test", - Scheme: types.SchemeHTTP, + Scheme: route.SchemeHTTP, Host: "example.com", - Port: types.Port{Proxy: 80}, + Port: route.Port{Proxy: 80}, Metadata: Metadata{ isValidated: true, }, @@ -29,9 +29,9 @@ func TestRouteValidate(t *testing.T) { t.Run("ReservedPort", func(t *testing.T) { r := &Route{ Alias: "test", - Scheme: types.SchemeHTTP, + Scheme: route.SchemeHTTP, Host: "localhost", - Port: types.Port{Proxy: common.ProxyHTTPPort}, + Port: route.Port{Proxy: common.ProxyHTTPPort}, } err := r.Validate() require.Error(t, err, "Validate should return error for localhost with reserved port") @@ -41,9 +41,9 @@ func TestRouteValidate(t *testing.T) { t.Run("ListeningPortWithHTTP", func(t *testing.T) { r := &Route{ Alias: "test", - Scheme: types.SchemeHTTP, + Scheme: route.SchemeHTTP, Host: "example.com", - Port: types.Port{Proxy: 80, Listening: 1234}, + Port: route.Port{Proxy: 80, Listening: 1234}, } err := r.Validate() require.Error(t, err, "Validate should return error for HTTP scheme with listening port") @@ -53,9 +53,9 @@ func TestRouteValidate(t *testing.T) { t.Run("DisabledHealthCheckWithLoadBalancer", func(t *testing.T) { r := &Route{ Alias: "test", - Scheme: types.SchemeHTTP, + Scheme: route.SchemeHTTP, Host: "example.com", - Port: types.Port{Proxy: 80}, + Port: route.Port{Proxy: 80}, HealthCheck: &health.HealthCheckConfig{ Disable: true, }, @@ -71,9 +71,9 @@ func TestRouteValidate(t *testing.T) { t.Run("FileServerScheme", func(t *testing.T) { r := &Route{ Alias: "test", - Scheme: types.SchemeFileServer, + Scheme: route.SchemeFileServer, Host: "example.com", - Port: types.Port{Proxy: 80}, + Port: route.Port{Proxy: 80}, Root: "/tmp", // Root is required for file server } err := r.Validate() @@ -84,9 +84,9 @@ func TestRouteValidate(t *testing.T) { t.Run("HTTPScheme", func(t *testing.T) { r := &Route{ Alias: "test", - Scheme: types.SchemeHTTP, + Scheme: route.SchemeHTTP, Host: "example.com", - Port: types.Port{Proxy: 80}, + Port: route.Port{Proxy: 80}, } err := r.Validate() require.NoError(t, err, "Validate should not return error for valid HTTP route") @@ -96,9 +96,9 @@ func TestRouteValidate(t *testing.T) { t.Run("TCPScheme", func(t *testing.T) { r := &Route{ Alias: "test", - Scheme: types.SchemeTCP, + Scheme: route.SchemeTCP, Host: "example.com", - Port: types.Port{Proxy: 80, Listening: 8080}, + Port: route.Port{Proxy: 80, Listening: 8080}, } err := r.Validate() require.NoError(t, err, "Validate should not return error for valid TCP route") @@ -108,11 +108,11 @@ func TestRouteValidate(t *testing.T) { t.Run("DockerContainer", func(t *testing.T) { r := &Route{ Alias: "test", - Scheme: types.SchemeHTTP, + Scheme: route.SchemeHTTP, Host: "example.com", - Port: types.Port{Proxy: 80}, + Port: route.Port{Proxy: 80}, Metadata: Metadata{ - Container: &docker.Container{ + DockerContainer: &docker.Container{ ContainerID: "test-id", Image: &docker.ContainerImage{ Name: "test-image", @@ -130,7 +130,7 @@ func TestRouteValidate(t *testing.T) { Alias: "test", Scheme: "invalid", Host: "example.com", - Port: types.Port{Proxy: 80}, + Port: route.Port{Proxy: 80}, } require.Panics(t, func() { _ = r.Validate() @@ -140,9 +140,9 @@ func TestRouteValidate(t *testing.T) { t.Run("ModifiedFields", func(t *testing.T) { r := &Route{ Alias: "test", - Scheme: types.SchemeHTTP, + Scheme: route.SchemeHTTP, Host: "example.com", - Port: types.Port{Proxy: 80}, + Port: route.Port{Proxy: 80}, } err := r.Validate() require.NoError(t, err) diff --git a/internal/route/routes/routes.go b/internal/route/routes/routes.go index 40e8e74..5a510a7 100644 --- a/internal/route/routes/routes.go +++ b/internal/route/routes/routes.go @@ -1,20 +1,20 @@ package routes import ( - "github.com/yusing/go-proxy/internal/route/types" + route "github.com/yusing/go-proxy/internal/route/types" F "github.com/yusing/go-proxy/internal/utils/functional" ) var ( - httpRoutes = F.NewMapOf[string, types.HTTPRoute]() - streamRoutes = F.NewMapOf[string, types.StreamRoute]() + httpRoutes = F.NewMapOf[string, route.HTTPRoute]() + streamRoutes = F.NewMapOf[string, route.StreamRoute]() ) -func RangeRoutes(callback func(alias string, r types.Route)) { - httpRoutes.RangeAll(func(alias string, r types.HTTPRoute) { +func RangeRoutes(callback func(alias string, r route.Route)) { + httpRoutes.RangeAll(func(alias string, r route.HTTPRoute) { callback(alias, r) }) - streamRoutes.RangeAll(func(alias string, r types.StreamRoute) { + streamRoutes.RangeAll(func(alias string, r route.StreamRoute) { callback(alias, r) }) } @@ -23,15 +23,15 @@ func NumRoutes() int { return httpRoutes.Size() + streamRoutes.Size() } -func GetHTTPRoutes() F.Map[string, types.HTTPRoute] { +func GetHTTPRoutes() F.Map[string, route.HTTPRoute] { return httpRoutes } -func GetStreamRoutes() F.Map[string, types.StreamRoute] { +func GetStreamRoutes() F.Map[string, route.StreamRoute] { return streamRoutes } -func GetHTTPRouteOrExact(alias, host string) (types.HTTPRoute, bool) { +func GetHTTPRouteOrExact(alias, host string) (route.HTTPRoute, bool) { r, ok := httpRoutes.Load(alias) if ok { return r, true @@ -40,15 +40,15 @@ func GetHTTPRouteOrExact(alias, host string) (types.HTTPRoute, bool) { return httpRoutes.Load(host) } -func GetHTTPRoute(alias string) (types.HTTPRoute, bool) { +func GetHTTPRoute(alias string) (route.HTTPRoute, bool) { return httpRoutes.Load(alias) } -func GetStreamRoute(alias string) (types.StreamRoute, bool) { +func GetStreamRoute(alias string) (route.StreamRoute, bool) { return streamRoutes.Load(alias) } -func GetRoute(alias string) (types.Route, bool) { +func GetRoute(alias string) (route.Route, bool) { r, ok := httpRoutes.Load(alias) if ok { return r, true @@ -56,11 +56,11 @@ func GetRoute(alias string) (types.Route, bool) { return streamRoutes.Load(alias) } -func SetHTTPRoute(alias string, r types.HTTPRoute) { +func SetHTTPRoute(alias string, r route.HTTPRoute) { httpRoutes.Store(alias, r) } -func SetStreamRoute(alias string, r types.StreamRoute) { +func SetStreamRoute(alias string, r route.StreamRoute) { streamRoutes.Store(alias, r) } @@ -73,6 +73,6 @@ func DeleteStreamRoute(alias string) { } func TestClear() { - httpRoutes = F.NewMapOf[string, types.HTTPRoute]() - streamRoutes = F.NewMapOf[string, types.StreamRoute]() + httpRoutes = F.NewMapOf[string, route.HTTPRoute]() + streamRoutes = F.NewMapOf[string, route.StreamRoute]() } diff --git a/internal/route/types/http_config.go b/internal/route/types/http_config.go index 3aba13d..881903d 100644 --- a/internal/route/types/http_config.go +++ b/internal/route/types/http_config.go @@ -1,4 +1,4 @@ -package types +package route import ( "time" diff --git a/internal/route/types/http_config_test.go b/internal/route/types/http_config_test.go index 58d7226..2846212 100644 --- a/internal/route/types/http_config_test.go +++ b/internal/route/types/http_config_test.go @@ -1,11 +1,11 @@ -package types_test +package route_test import ( "testing" "time" . "github.com/yusing/go-proxy/internal/route" - "github.com/yusing/go-proxy/internal/route/types" + route "github.com/yusing/go-proxy/internal/route/types" "github.com/yusing/go-proxy/internal/utils" . "github.com/yusing/go-proxy/internal/utils/testing" ) @@ -14,14 +14,14 @@ func TestHTTPConfigDeserialize(t *testing.T) { tests := []struct { name string input map[string]any - expected types.HTTPConfig + expected route.HTTPConfig }{ { name: "no_tls_verify", input: map[string]any{ "no_tls_verify": "true", }, - expected: types.HTTPConfig{ + expected: route.HTTPConfig{ NoTLSVerify: true, }, }, @@ -30,7 +30,7 @@ func TestHTTPConfigDeserialize(t *testing.T) { input: map[string]any{ "response_header_timeout": "1s", }, - expected: types.HTTPConfig{ + expected: route.HTTPConfig{ ResponseHeaderTimeout: 1 * time.Second, }, }, diff --git a/internal/route/types/port.go b/internal/route/types/port.go index 57c08fd..03442aa 100644 --- a/internal/route/types/port.go +++ b/internal/route/types/port.go @@ -1,4 +1,4 @@ -package types +package route import ( "strconv" @@ -8,8 +8,8 @@ import ( ) type Port struct { - Listening int `json:"listening"` - Proxy int `json:"proxy"` + Listening int `json:"listening,omitempty"` + Proxy int `json:"proxy,omitempty"` } var ( diff --git a/internal/route/types/port_test.go b/internal/route/types/port_test.go index 12ca517..28b7d3e 100644 --- a/internal/route/types/port_test.go +++ b/internal/route/types/port_test.go @@ -1,4 +1,4 @@ -package types +package route import ( "errors" diff --git a/internal/route/types/route.go b/internal/route/types/route.go index 299eac6..9dc9212 100644 --- a/internal/route/types/route.go +++ b/internal/route/types/route.go @@ -1,4 +1,4 @@ -package types +package route import ( "net/http" @@ -12,6 +12,7 @@ import ( "github.com/yusing/go-proxy/internal/watcher/health" loadbalance "github.com/yusing/go-proxy/internal/net/gphttp/loadbalancer/types" + "github.com/yusing/go-proxy/internal/net/gphttp/reverseproxy" ) type ( @@ -46,6 +47,10 @@ type ( Route http.Handler } + ReverseProxyRoute interface { + HTTPRoute + ReverseProxy() *reverseproxy.ReverseProxy + } StreamRoute interface { Route net.Stream diff --git a/internal/route/types/route_type.go b/internal/route/types/route_type.go index 6d13d17..c0ac822 100644 --- a/internal/route/types/route_type.go +++ b/internal/route/types/route_type.go @@ -1,4 +1,4 @@ -package types +package route type RouteType string diff --git a/internal/route/types/scheme.go b/internal/route/types/scheme.go index cbb4ce2..5983539 100644 --- a/internal/route/types/scheme.go +++ b/internal/route/types/scheme.go @@ -1,4 +1,4 @@ -package types +package route import ( "github.com/yusing/go-proxy/internal/gperr"