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" "github.com/yusing/go-proxy/internal/net/gphttp/accesslog"
loadbalance "github.com/yusing/go-proxy/internal/net/gphttp/loadbalancer/types" 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/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" "github.com/yusing/go-proxy/internal/utils"
) )
@ -30,12 +30,12 @@ type (
_ utils.NoCopy _ utils.NoCopy
Alias string `json:"alias"` Alias string `json:"alias"`
Scheme types.Scheme `json:"scheme,omitempty"` Scheme route.Scheme `json:"scheme,omitempty"`
Host string `json:"host,omitempty"` Host string `json:"host,omitempty"`
Port types.Port `json:"port,omitempty"` Port route.Port `json:"port,omitempty"`
Root string `json:"root,omitempty"` Root string `json:"root,omitempty"`
types.HTTPConfig route.HTTPConfig
PathPatterns []string `json:"path_patterns,omitempty"` PathPatterns []string `json:"path_patterns,omitempty"`
Rules rules.Rules `json:"rules,omitempty" validate:"omitempty,unique=Name"` Rules rules.Rules `json:"rules,omitempty" validate:"omitempty,unique=Name"`
HealthCheck *health.HealthCheckConfig `json:"healthcheck,omitempty"` HealthCheck *health.HealthCheckConfig `json:"healthcheck,omitempty"`
@ -57,7 +57,7 @@ type (
ProxyURL *net.URL `json:"purl,omitempty"` ProxyURL *net.URL `json:"purl,omitempty"`
Idlewatcher *idlewatcher.Config `json:"idlewatcher,omitempty"` Idlewatcher *idlewatcher.Config `json:"idlewatcher,omitempty"`
impl types.Route impl route.Route
isValidated bool isValidated bool
} }
Routes map[string]*Route Routes map[string]*Route
@ -80,7 +80,7 @@ func (r *Route) Validate() (err gperr.Error) {
case "localhost", "127.0.0.1": case "localhost", "127.0.0.1":
switch r.Port.Proxy { switch r.Port.Proxy {
case common.ProxyHTTPPort, common.ProxyHTTPSPort, common.APIHTTPPort: 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) 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") errs := gperr.NewBuilder("entry validation failed")
switch r.Scheme { switch r.Scheme {
case types.SchemeFileServer: case route.SchemeFileServer:
r.impl, err = NewFileServer(r) r.impl, err = NewFileServer(r)
if err != nil { if err != nil {
errs.Add(err) errs.Add(err)
} }
case types.SchemeHTTP, types.SchemeHTTPS: case route.SchemeHTTP, route.SchemeHTTPS:
if r.Port.Listening != 0 { if r.Port.Listening != 0 {
errs.Addf("unexpected listening port for %s scheme", r.Scheme) errs.Addf("unexpected listening port for %s scheme", r.Scheme)
} }
fallthrough 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)) r.LisURL = gperr.Collect(errs, net.ParseURL, fmt.Sprintf("%s://:%d", r.Scheme, r.Port.Listening))
fallthrough fallthrough
default: default:
@ -119,11 +119,11 @@ func (r *Route) Validate() (err gperr.Error) {
} }
switch r.Scheme { switch r.Scheme {
case types.SchemeFileServer: case route.SchemeFileServer:
r.impl, err = NewFileServer(r) r.impl, err = NewFileServer(r)
case types.SchemeHTTP, types.SchemeHTTPS: case route.SchemeHTTP, route.SchemeHTTPS:
r.impl, err = NewReverseProxyRoute(r) r.impl, err = NewReverseProxyRoute(r)
case types.SchemeTCP, types.SchemeUDP: case route.SchemeTCP, route.SchemeUDP:
r.impl, err = NewStreamRoute(r) r.impl, err = NewStreamRoute(r)
default: default:
panic(fmt.Errorf("unexpected scheme %s for alias %s", r.Scheme, r.Alias)) 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 return r.ProxyURL
} }
func (r *Route) Type() types.RouteType { func (r *Route) Type() route.RouteType {
switch r.Scheme { switch r.Scheme {
case types.SchemeHTTP, types.SchemeHTTPS, types.SchemeFileServer: case route.SchemeHTTP, route.SchemeHTTPS, route.SchemeFileServer:
return types.RouteTypeHTTP return route.RouteTypeHTTP
case types.SchemeTCP, types.SchemeUDP: case route.SchemeTCP, route.SchemeUDP:
return types.RouteTypeStream return route.RouteTypeStream
} }
panic(fmt.Errorf("unexpected scheme %s for alias %s", r.Scheme, r.Alias)) 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) scheme, port, ok := getSchemePortByImageName(cont.Image.Name)
if ok { if ok {
if r.Scheme == "" { if r.Scheme == "" {
r.Scheme = types.Scheme(scheme) r.Scheme = route.Scheme(scheme)
} }
if pp == 0 { if pp == 0 {
pp = port pp = port
@ -300,7 +300,7 @@ func (r *Route) Finalize() {
if scheme, port, ok := getSchemePortByAlias(r.Alias); ok { if scheme, port, ok := getSchemePortByAlias(r.Alias); ok {
if r.Scheme == "" { if r.Scheme == "" {
r.Scheme = types.Scheme(scheme) r.Scheme = route.Scheme(scheme)
} }
if pp == 0 { if pp == 0 {
pp = port pp = port

View file

@ -7,7 +7,7 @@ import (
"github.com/yusing/go-proxy/internal/common" "github.com/yusing/go-proxy/internal/common"
"github.com/yusing/go-proxy/internal/docker" "github.com/yusing/go-proxy/internal/docker"
loadbalance "github.com/yusing/go-proxy/internal/net/gphttp/loadbalancer/types" 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" "github.com/yusing/go-proxy/internal/watcher/health"
) )
@ -15,9 +15,9 @@ func TestRouteValidate(t *testing.T) {
t.Run("AlreadyValidated", func(t *testing.T) { t.Run("AlreadyValidated", func(t *testing.T) {
r := &Route{ r := &Route{
Alias: "test", Alias: "test",
Scheme: types.SchemeHTTP, Scheme: route.SchemeHTTP,
Host: "example.com", Host: "example.com",
Port: types.Port{Proxy: 80}, Port: route.Port{Proxy: 80},
Metadata: Metadata{ Metadata: Metadata{
isValidated: true, isValidated: true,
}, },
@ -29,9 +29,9 @@ func TestRouteValidate(t *testing.T) {
t.Run("ReservedPort", func(t *testing.T) { t.Run("ReservedPort", func(t *testing.T) {
r := &Route{ r := &Route{
Alias: "test", Alias: "test",
Scheme: types.SchemeHTTP, Scheme: route.SchemeHTTP,
Host: "localhost", Host: "localhost",
Port: types.Port{Proxy: common.ProxyHTTPPort}, Port: route.Port{Proxy: common.ProxyHTTPPort},
} }
err := r.Validate() err := r.Validate()
require.Error(t, err, "Validate should return error for localhost with reserved port") 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) { t.Run("ListeningPortWithHTTP", func(t *testing.T) {
r := &Route{ r := &Route{
Alias: "test", Alias: "test",
Scheme: types.SchemeHTTP, Scheme: route.SchemeHTTP,
Host: "example.com", Host: "example.com",
Port: types.Port{Proxy: 80, Listening: 1234}, Port: route.Port{Proxy: 80, Listening: 1234},
} }
err := r.Validate() err := r.Validate()
require.Error(t, err, "Validate should return error for HTTP scheme with listening port") 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) { t.Run("DisabledHealthCheckWithLoadBalancer", func(t *testing.T) {
r := &Route{ r := &Route{
Alias: "test", Alias: "test",
Scheme: types.SchemeHTTP, Scheme: route.SchemeHTTP,
Host: "example.com", Host: "example.com",
Port: types.Port{Proxy: 80}, Port: route.Port{Proxy: 80},
HealthCheck: &health.HealthCheckConfig{ HealthCheck: &health.HealthCheckConfig{
Disable: true, Disable: true,
}, },
@ -71,9 +71,9 @@ func TestRouteValidate(t *testing.T) {
t.Run("FileServerScheme", func(t *testing.T) { t.Run("FileServerScheme", func(t *testing.T) {
r := &Route{ r := &Route{
Alias: "test", Alias: "test",
Scheme: types.SchemeFileServer, Scheme: route.SchemeFileServer,
Host: "example.com", Host: "example.com",
Port: types.Port{Proxy: 80}, Port: route.Port{Proxy: 80},
Root: "/tmp", // Root is required for file server Root: "/tmp", // Root is required for file server
} }
err := r.Validate() err := r.Validate()
@ -84,9 +84,9 @@ func TestRouteValidate(t *testing.T) {
t.Run("HTTPScheme", func(t *testing.T) { t.Run("HTTPScheme", func(t *testing.T) {
r := &Route{ r := &Route{
Alias: "test", Alias: "test",
Scheme: types.SchemeHTTP, Scheme: route.SchemeHTTP,
Host: "example.com", Host: "example.com",
Port: types.Port{Proxy: 80}, Port: route.Port{Proxy: 80},
} }
err := r.Validate() err := r.Validate()
require.NoError(t, err, "Validate should not return error for valid HTTP route") 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) { t.Run("TCPScheme", func(t *testing.T) {
r := &Route{ r := &Route{
Alias: "test", Alias: "test",
Scheme: types.SchemeTCP, Scheme: route.SchemeTCP,
Host: "example.com", Host: "example.com",
Port: types.Port{Proxy: 80, Listening: 8080}, Port: route.Port{Proxy: 80, Listening: 8080},
} }
err := r.Validate() err := r.Validate()
require.NoError(t, err, "Validate should not return error for valid TCP route") 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) { t.Run("DockerContainer", func(t *testing.T) {
r := &Route{ r := &Route{
Alias: "test", Alias: "test",
Scheme: types.SchemeHTTP, Scheme: route.SchemeHTTP,
Host: "example.com", Host: "example.com",
Port: types.Port{Proxy: 80}, Port: route.Port{Proxy: 80},
Metadata: Metadata{ Metadata: Metadata{
Container: &docker.Container{ DockerContainer: &docker.Container{
ContainerID: "test-id", ContainerID: "test-id",
Image: &docker.ContainerImage{ Image: &docker.ContainerImage{
Name: "test-image", Name: "test-image",
@ -130,7 +130,7 @@ func TestRouteValidate(t *testing.T) {
Alias: "test", Alias: "test",
Scheme: "invalid", Scheme: "invalid",
Host: "example.com", Host: "example.com",
Port: types.Port{Proxy: 80}, Port: route.Port{Proxy: 80},
} }
require.Panics(t, func() { require.Panics(t, func() {
_ = r.Validate() _ = r.Validate()
@ -140,9 +140,9 @@ func TestRouteValidate(t *testing.T) {
t.Run("ModifiedFields", func(t *testing.T) { t.Run("ModifiedFields", func(t *testing.T) {
r := &Route{ r := &Route{
Alias: "test", Alias: "test",
Scheme: types.SchemeHTTP, Scheme: route.SchemeHTTP,
Host: "example.com", Host: "example.com",
Port: types.Port{Proxy: 80}, Port: route.Port{Proxy: 80},
} }
err := r.Validate() err := r.Validate()
require.NoError(t, err) require.NoError(t, err)

View file

@ -1,20 +1,20 @@
package routes package routes
import ( 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" F "github.com/yusing/go-proxy/internal/utils/functional"
) )
var ( var (
httpRoutes = F.NewMapOf[string, types.HTTPRoute]() httpRoutes = F.NewMapOf[string, route.HTTPRoute]()
streamRoutes = F.NewMapOf[string, types.StreamRoute]() streamRoutes = F.NewMapOf[string, route.StreamRoute]()
) )
func RangeRoutes(callback func(alias string, r types.Route)) { func RangeRoutes(callback func(alias string, r route.Route)) {
httpRoutes.RangeAll(func(alias string, r types.HTTPRoute) { httpRoutes.RangeAll(func(alias string, r route.HTTPRoute) {
callback(alias, r) callback(alias, r)
}) })
streamRoutes.RangeAll(func(alias string, r types.StreamRoute) { streamRoutes.RangeAll(func(alias string, r route.StreamRoute) {
callback(alias, r) callback(alias, r)
}) })
} }
@ -23,15 +23,15 @@ func NumRoutes() int {
return httpRoutes.Size() + streamRoutes.Size() return httpRoutes.Size() + streamRoutes.Size()
} }
func GetHTTPRoutes() F.Map[string, types.HTTPRoute] { func GetHTTPRoutes() F.Map[string, route.HTTPRoute] {
return httpRoutes return httpRoutes
} }
func GetStreamRoutes() F.Map[string, types.StreamRoute] { func GetStreamRoutes() F.Map[string, route.StreamRoute] {
return streamRoutes return streamRoutes
} }
func GetHTTPRouteOrExact(alias, host string) (types.HTTPRoute, bool) { func GetHTTPRouteOrExact(alias, host string) (route.HTTPRoute, bool) {
r, ok := httpRoutes.Load(alias) r, ok := httpRoutes.Load(alias)
if ok { if ok {
return r, true return r, true
@ -40,15 +40,15 @@ func GetHTTPRouteOrExact(alias, host string) (types.HTTPRoute, bool) {
return httpRoutes.Load(host) return httpRoutes.Load(host)
} }
func GetHTTPRoute(alias string) (types.HTTPRoute, bool) { func GetHTTPRoute(alias string) (route.HTTPRoute, bool) {
return httpRoutes.Load(alias) return httpRoutes.Load(alias)
} }
func GetStreamRoute(alias string) (types.StreamRoute, bool) { func GetStreamRoute(alias string) (route.StreamRoute, bool) {
return streamRoutes.Load(alias) return streamRoutes.Load(alias)
} }
func GetRoute(alias string) (types.Route, bool) { func GetRoute(alias string) (route.Route, bool) {
r, ok := httpRoutes.Load(alias) r, ok := httpRoutes.Load(alias)
if ok { if ok {
return r, true return r, true
@ -56,11 +56,11 @@ func GetRoute(alias string) (types.Route, bool) {
return streamRoutes.Load(alias) return streamRoutes.Load(alias)
} }
func SetHTTPRoute(alias string, r types.HTTPRoute) { func SetHTTPRoute(alias string, r route.HTTPRoute) {
httpRoutes.Store(alias, r) httpRoutes.Store(alias, r)
} }
func SetStreamRoute(alias string, r types.StreamRoute) { func SetStreamRoute(alias string, r route.StreamRoute) {
streamRoutes.Store(alias, r) streamRoutes.Store(alias, r)
} }
@ -73,6 +73,6 @@ func DeleteStreamRoute(alias string) {
} }
func TestClear() { func TestClear() {
httpRoutes = F.NewMapOf[string, types.HTTPRoute]() httpRoutes = F.NewMapOf[string, route.HTTPRoute]()
streamRoutes = F.NewMapOf[string, types.StreamRoute]() streamRoutes = F.NewMapOf[string, route.StreamRoute]()
} }

View file

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

View file

@ -1,11 +1,11 @@
package types_test package route_test
import ( import (
"testing" "testing"
"time" "time"
. "github.com/yusing/go-proxy/internal/route" . "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"
. "github.com/yusing/go-proxy/internal/utils/testing" . "github.com/yusing/go-proxy/internal/utils/testing"
) )
@ -14,14 +14,14 @@ func TestHTTPConfigDeserialize(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
input map[string]any input map[string]any
expected types.HTTPConfig expected route.HTTPConfig
}{ }{
{ {
name: "no_tls_verify", name: "no_tls_verify",
input: map[string]any{ input: map[string]any{
"no_tls_verify": "true", "no_tls_verify": "true",
}, },
expected: types.HTTPConfig{ expected: route.HTTPConfig{
NoTLSVerify: true, NoTLSVerify: true,
}, },
}, },
@ -30,7 +30,7 @@ func TestHTTPConfigDeserialize(t *testing.T) {
input: map[string]any{ input: map[string]any{
"response_header_timeout": "1s", "response_header_timeout": "1s",
}, },
expected: types.HTTPConfig{ expected: route.HTTPConfig{
ResponseHeaderTimeout: 1 * time.Second, ResponseHeaderTimeout: 1 * time.Second,
}, },
}, },

View file

@ -1,4 +1,4 @@
package types package route
import ( import (
"strconv" "strconv"
@ -8,8 +8,8 @@ import (
) )
type Port struct { type Port struct {
Listening int `json:"listening"` Listening int `json:"listening,omitempty"`
Proxy int `json:"proxy"` Proxy int `json:"proxy,omitempty"`
} }
var ( var (

View file

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

View file

@ -1,4 +1,4 @@
package types package route
import ( import (
"net/http" "net/http"
@ -12,6 +12,7 @@ import (
"github.com/yusing/go-proxy/internal/watcher/health" "github.com/yusing/go-proxy/internal/watcher/health"
loadbalance "github.com/yusing/go-proxy/internal/net/gphttp/loadbalancer/types" loadbalance "github.com/yusing/go-proxy/internal/net/gphttp/loadbalancer/types"
"github.com/yusing/go-proxy/internal/net/gphttp/reverseproxy"
) )
type ( type (
@ -46,6 +47,10 @@ type (
Route Route
http.Handler http.Handler
} }
ReverseProxyRoute interface {
HTTPRoute
ReverseProxy() *reverseproxy.ReverseProxy
}
StreamRoute interface { StreamRoute interface {
Route Route
net.Stream net.Stream

View file

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

View file

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