refactor: rename module route/types to route

This commit is contained in:
yusing 2025-04-08 05:04:49 +08:00
parent 8ed63fe4b0
commit c59567ae8f
10 changed files with 74 additions and 69 deletions

View file

@ -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

View file

@ -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)

View file

@ -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]()
}

View file

@ -1,4 +1,4 @@
package types
package route
import (
"time"

View file

@ -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,
},
},

View file

@ -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 (

View file

@ -1,4 +1,4 @@
package types
package route
import (
"errors"

View file

@ -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

View file

@ -1,4 +1,4 @@
package types
package route
type RouteType string

View file

@ -1,4 +1,4 @@
package types
package route
import (
"github.com/yusing/go-proxy/internal/gperr"