refactor(route): unify common fields into routes.go

This commit is contained in:
yusing 2025-06-05 18:25:15 +08:00
parent d3568d9c35
commit 9470a14fe8
4 changed files with 15 additions and 68 deletions

View file

@ -11,7 +11,6 @@ import (
"github.com/yusing/go-proxy/internal/net/gphttp/middleware" "github.com/yusing/go-proxy/internal/net/gphttp/middleware"
"github.com/yusing/go-proxy/internal/route/routes" "github.com/yusing/go-proxy/internal/route/routes"
"github.com/yusing/go-proxy/internal/task" "github.com/yusing/go-proxy/internal/task"
"github.com/yusing/go-proxy/internal/watcher/health"
"github.com/yusing/go-proxy/internal/watcher/health/monitor" "github.com/yusing/go-proxy/internal/watcher/health/monitor"
) )
@ -19,9 +18,6 @@ type (
FileServer struct { FileServer struct {
*Route *Route
Health *monitor.FileServerHealthMonitor `json:"health"`
task *task.Task
middleware *middleware.Middleware middleware *middleware.Middleware
handler http.Handler handler http.Handler
accessLogger *accesslog.AccessLogger accessLogger *accesslog.AccessLogger
@ -90,8 +86,8 @@ func (s *FileServer) Start(parent task.Parent) gperr.Error {
} }
if s.UseHealthCheck() { if s.UseHealthCheck() {
s.Health = monitor.NewFileServerHealthMonitor(s.HealthCheck, s.Root) s.HealthMon = monitor.NewFileServerHealthMonitor(s.HealthCheck, s.Root)
if err := s.Health.Start(s.task); err != nil { if err := s.HealthMon.Start(s.task); err != nil {
return err return err
} }
} }
@ -111,15 +107,6 @@ func (s *FileServer) Start(parent task.Parent) gperr.Error {
return nil return nil
} }
func (s *FileServer) Task() *task.Task {
return s.task
}
// Finish implements task.TaskFinisher.
func (s *FileServer) Finish(reason any) {
s.task.Finish(reason)
}
// ServeHTTP implements http.Handler. // ServeHTTP implements http.Handler.
func (s *FileServer) ServeHTTP(w http.ResponseWriter, req *http.Request) { func (s *FileServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
s.handler.ServeHTTP(w, req) s.handler.ServeHTTP(w, req)
@ -127,7 +114,3 @@ func (s *FileServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
s.accessLogger.Log(req, req.Response) s.accessLogger.Log(req, req.Response)
} }
} }
func (s *FileServer) HealthMonitor() health.HealthMonitor {
return s.Health
}

View file

@ -18,20 +18,15 @@ import (
"github.com/yusing/go-proxy/internal/net/types" "github.com/yusing/go-proxy/internal/net/types"
"github.com/yusing/go-proxy/internal/route/routes" "github.com/yusing/go-proxy/internal/route/routes"
"github.com/yusing/go-proxy/internal/task" "github.com/yusing/go-proxy/internal/task"
"github.com/yusing/go-proxy/internal/watcher/health"
"github.com/yusing/go-proxy/internal/watcher/health/monitor" "github.com/yusing/go-proxy/internal/watcher/health/monitor"
) )
type ReveseProxyRoute struct { type ReveseProxyRoute struct {
*Route *Route
HealthMon health.HealthMonitor `json:"health,omitempty"`
loadBalancer *loadbalancer.LoadBalancer loadBalancer *loadbalancer.LoadBalancer
handler http.Handler handler http.Handler
rp *reverseproxy.ReverseProxy rp *reverseproxy.ReverseProxy
task *task.Task
} }
var _ routes.ReverseProxyRoute = (*ReveseProxyRoute)(nil) var _ routes.ReverseProxyRoute = (*ReveseProxyRoute)(nil)
@ -157,24 +152,10 @@ func (r *ReveseProxyRoute) Start(parent task.Parent) gperr.Error {
return nil return nil
} }
// Task implements task.TaskStarter.
func (r *ReveseProxyRoute) Task() *task.Task {
return r.task
}
// Finish implements task.TaskFinisher.
func (r *ReveseProxyRoute) Finish(reason any) {
r.task.Finish(reason)
}
func (r *ReveseProxyRoute) ServeHTTP(w http.ResponseWriter, req *http.Request) { func (r *ReveseProxyRoute) ServeHTTP(w http.ResponseWriter, req *http.Request) {
r.handler.ServeHTTP(w, req) r.handler.ServeHTTP(w, req)
} }
func (r *ReveseProxyRoute) HealthMonitor() health.HealthMonitor {
return r.HealthMon
}
func (r *ReveseProxyRoute) addToLoadBalancer(parent task.Parent) { func (r *ReveseProxyRoute) addToLoadBalancer(parent task.Parent) {
var lb *loadbalancer.LoadBalancer var lb *loadbalancer.LoadBalancer
cfg := r.LoadBalance cfg := r.LoadBalance
@ -194,8 +175,8 @@ func (r *ReveseProxyRoute) addToLoadBalancer(parent task.Parent) {
Route: &Route{ Route: &Route{
Alias: cfg.Link + "-loadbalancer", Alias: cfg.Link + "-loadbalancer",
Homepage: r.Homepage, Homepage: r.Homepage,
},
HealthMon: lb, HealthMon: lb,
},
loadBalancer: lb, loadBalancer: lb,
handler: lb, handler: lb,
} }

View file

@ -52,6 +52,7 @@ type (
AccessLog *accesslog.RequestLoggerConfig `json:"access_log,omitempty"` AccessLog *accesslog.RequestLoggerConfig `json:"access_log,omitempty"`
Idlewatcher *idlewatcher.Config `json:"idlewatcher,omitempty"` Idlewatcher *idlewatcher.Config `json:"idlewatcher,omitempty"`
HealthMon health.HealthMonitor `json:"health,omitempty"`
Metadata `deserialize:"-"` Metadata `deserialize:"-"`
} }
@ -69,6 +70,8 @@ type (
Excluded *bool `json:"excluded"` Excluded *bool `json:"excluded"`
impl routes.Route impl routes.Route
task *task.Task
isValidated bool isValidated bool
lastError gperr.Error lastError gperr.Error
provider routes.Provider provider routes.Provider
@ -243,7 +246,7 @@ func (r *Route) Impl() routes.Route {
} }
func (r *Route) Task() *task.Task { func (r *Route) Task() *task.Task {
return r.impl.Task() return r.task
} }
func (r *Route) Start(parent task.Parent) (err gperr.Error) { func (r *Route) Start(parent task.Parent) (err gperr.Error) {
@ -265,12 +268,12 @@ func (r *Route) start(parent task.Parent) gperr.Error {
if conflict, added := routes.All.AddIfNotExists(r.impl); !added { if conflict, added := routes.All.AddIfNotExists(r.impl); !added {
err := gperr.Errorf("route %s already exists: from %s and %s", r.Alias, r.ProviderName(), conflict.ProviderName()) err := gperr.Errorf("route %s already exists: from %s and %s", r.Alias, r.ProviderName(), conflict.ProviderName())
r.impl.Task().FinishAndWait(err) r.task.FinishAndWait(err)
return err return err
} else { } else {
// reference here because r.impl will be nil after Finish() is called. // reference here because r.impl will be nil after Finish() is called.
impl := r.impl impl := r.impl
impl.Task().OnCancel("remove_routes_from_all", func() { r.task.OnCancel("remove_routes_from_all", func() {
routes.All.Del(impl) routes.All.Del(impl)
}) })
} }
@ -285,7 +288,7 @@ func (r *Route) FinishAndWait(reason any) {
if r.impl == nil { if r.impl == nil {
return return
} }
r.impl.Task().FinishAndWait(reason) r.task.FinishAndWait(reason)
r.impl = nil r.impl = nil
} }
@ -360,7 +363,7 @@ func (r *Route) IsAgent() bool {
} }
func (r *Route) HealthMonitor() health.HealthMonitor { func (r *Route) HealthMonitor() health.HealthMonitor {
return r.impl.HealthMonitor() return r.HealthMon
} }
func (r *Route) IdlewatcherConfig() *idlewatcher.Config { func (r *Route) IdlewatcherConfig() *idlewatcher.Config {

View file

@ -11,20 +11,14 @@ import (
net "github.com/yusing/go-proxy/internal/net/types" net "github.com/yusing/go-proxy/internal/net/types"
"github.com/yusing/go-proxy/internal/route/routes" "github.com/yusing/go-proxy/internal/route/routes"
"github.com/yusing/go-proxy/internal/task" "github.com/yusing/go-proxy/internal/task"
"github.com/yusing/go-proxy/internal/watcher/health"
"github.com/yusing/go-proxy/internal/watcher/health/monitor" "github.com/yusing/go-proxy/internal/watcher/health/monitor"
) )
// TODO: support stream load balance. // TODO: support stream load balance.
type StreamRoute struct { type StreamRoute struct {
*Route *Route
net.Stream `json:"-"` net.Stream `json:"-"`
HealthMon health.HealthMonitor `json:"health"`
task *task.Task
l zerolog.Logger l zerolog.Logger
} }
@ -88,20 +82,6 @@ func (r *StreamRoute) Start(parent task.Parent) gperr.Error {
return nil return nil
} }
// Task implements task.TaskStarter.
func (r *StreamRoute) Task() *task.Task {
return r.task
}
// Finish implements task.TaskFinisher.
func (r *StreamRoute) Finish(reason any) {
r.task.Finish(reason)
}
func (r *StreamRoute) HealthMonitor() health.HealthMonitor {
return r.HealthMon
}
func (r *StreamRoute) acceptConnections() { func (r *StreamRoute) acceptConnections() {
defer r.task.Finish("listener closed") defer r.task.Finish("listener closed")