add router component

This commit is contained in:
ginuerzh
2023-11-19 14:23:21 +08:00
parent 88cc6ff4d5
commit 74639e9c4e
25 changed files with 788 additions and 123 deletions

View File

@ -30,19 +30,19 @@ type ingressWrapper struct {
r *ingressRegistry
}
func (w *ingressWrapper) Get(ctx context.Context, host string, opts ...ingress.GetOption) string {
func (w *ingressWrapper) GetRule(ctx context.Context, host string, opts ...ingress.Option) *ingress.Rule {
v := w.r.get(w.name)
if v == nil {
return ""
return nil
}
return v.Get(ctx, host, opts...)
return v.GetRule(ctx, host, opts...)
}
func (w *ingressWrapper) Set(ctx context.Context, host, endpoint string, opts ...ingress.SetOption) bool {
func (w *ingressWrapper) SetRule(ctx context.Context, rule *ingress.Rule, opts ...ingress.Option) bool {
v := w.r.get(w.name)
if v == nil {
return false
}
return v.Set(ctx, host, endpoint, opts...)
return v.SetRule(ctx, rule, opts...)
}

View File

@ -18,6 +18,7 @@ import (
"github.com/go-gost/core/recorder"
reg "github.com/go-gost/core/registry"
"github.com/go-gost/core/resolver"
"github.com/go-gost/core/router"
"github.com/go-gost/core/sd"
"github.com/go-gost/core/service"
)
@ -46,6 +47,7 @@ var (
rateLimiterReg reg.Registry[rate.RateLimiter] = new(rateLimiterRegistry)
ingressReg reg.Registry[ingress.Ingress] = new(ingressRegistry)
routerReg reg.Registry[router.Router] = new(routerRegistry)
sdReg reg.Registry[sd.SD] = new(sdRegistry)
)
@ -166,6 +168,10 @@ func IngressRegistry() reg.Registry[ingress.Ingress] {
return ingressReg
}
func RouterRegistry() reg.Registry[router.Router] {
return routerReg
}
func SDRegistry() reg.Registry[sd.SD] {
return sdReg
}

49
registry/router.go Normal file
View File

@ -0,0 +1,49 @@
package registry
import (
"context"
"net"
"github.com/go-gost/core/router"
)
type routerRegistry struct {
registry[router.Router]
}
func (r *routerRegistry) Register(name string, v router.Router) error {
return r.registry.Register(name, v)
}
func (r *routerRegistry) Get(name string) router.Router {
if name != "" {
return &routerWrapper{name: name, r: r}
}
return nil
}
func (r *routerRegistry) get(name string) router.Router {
return r.registry.Get(name)
}
type routerWrapper struct {
name string
r *routerRegistry
}
func (w *routerWrapper) GetRoute(ctx context.Context, dst net.IP, opts ...router.Option) *router.Route {
v := w.r.get(w.name)
if v == nil {
return nil
}
return v.GetRoute(ctx, dst, opts...)
}
func (w *routerWrapper) SetRoute(ctx context.Context, route *router.Route, opts ...router.Option) bool {
v := w.r.get(w.name)
if v == nil {
return false
}
return v.SetRoute(ctx, route, opts...)
}