From 9470a14fe853bd1b81cedc58a2c20c0d4d0b9de7 Mon Sep 17 00:00:00 2001
From: yusing <yusing@6uo.me>
Date: Thu, 5 Jun 2025 18:25:15 +0800
Subject: [PATCH] refactor(route): unify common fields into routes.go

---
 internal/route/fileserver.go    | 21 ++-------------------
 internal/route/reverse_proxy.go | 25 +++----------------------
 internal/route/route.go         | 17 ++++++++++-------
 internal/route/stream.go        | 20 --------------------
 4 files changed, 15 insertions(+), 68 deletions(-)

diff --git a/internal/route/fileserver.go b/internal/route/fileserver.go
index ce37da3..163a8bc 100644
--- a/internal/route/fileserver.go
+++ b/internal/route/fileserver.go
@@ -11,7 +11,6 @@ import (
 	"github.com/yusing/go-proxy/internal/net/gphttp/middleware"
 	"github.com/yusing/go-proxy/internal/route/routes"
 	"github.com/yusing/go-proxy/internal/task"
-	"github.com/yusing/go-proxy/internal/watcher/health"
 	"github.com/yusing/go-proxy/internal/watcher/health/monitor"
 )
 
@@ -19,9 +18,6 @@ type (
 	FileServer struct {
 		*Route
 
-		Health *monitor.FileServerHealthMonitor `json:"health"`
-
-		task         *task.Task
 		middleware   *middleware.Middleware
 		handler      http.Handler
 		accessLogger *accesslog.AccessLogger
@@ -90,8 +86,8 @@ func (s *FileServer) Start(parent task.Parent) gperr.Error {
 	}
 
 	if s.UseHealthCheck() {
-		s.Health = monitor.NewFileServerHealthMonitor(s.HealthCheck, s.Root)
-		if err := s.Health.Start(s.task); err != nil {
+		s.HealthMon = monitor.NewFileServerHealthMonitor(s.HealthCheck, s.Root)
+		if err := s.HealthMon.Start(s.task); err != nil {
 			return err
 		}
 	}
@@ -111,15 +107,6 @@ func (s *FileServer) Start(parent task.Parent) gperr.Error {
 	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.
 func (s *FileServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
 	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)
 	}
 }
-
-func (s *FileServer) HealthMonitor() health.HealthMonitor {
-	return s.Health
-}
diff --git a/internal/route/reverse_proxy.go b/internal/route/reverse_proxy.go
index 88bbd19..81ff5c9 100755
--- a/internal/route/reverse_proxy.go
+++ b/internal/route/reverse_proxy.go
@@ -18,20 +18,15 @@ import (
 	"github.com/yusing/go-proxy/internal/net/types"
 	"github.com/yusing/go-proxy/internal/route/routes"
 	"github.com/yusing/go-proxy/internal/task"
-	"github.com/yusing/go-proxy/internal/watcher/health"
 	"github.com/yusing/go-proxy/internal/watcher/health/monitor"
 )
 
 type ReveseProxyRoute struct {
 	*Route
 
-	HealthMon health.HealthMonitor `json:"health,omitempty"`
-
 	loadBalancer *loadbalancer.LoadBalancer
 	handler      http.Handler
 	rp           *reverseproxy.ReverseProxy
-
-	task *task.Task
 }
 
 var _ routes.ReverseProxyRoute = (*ReveseProxyRoute)(nil)
@@ -157,24 +152,10 @@ func (r *ReveseProxyRoute) Start(parent task.Parent) gperr.Error {
 	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) {
 	r.handler.ServeHTTP(w, req)
 }
 
-func (r *ReveseProxyRoute) HealthMonitor() health.HealthMonitor {
-	return r.HealthMon
-}
-
 func (r *ReveseProxyRoute) addToLoadBalancer(parent task.Parent) {
 	var lb *loadbalancer.LoadBalancer
 	cfg := r.LoadBalance
@@ -192,10 +173,10 @@ func (r *ReveseProxyRoute) addToLoadBalancer(parent task.Parent) {
 		_ = lb.Start(parent) // always return nil
 		linked = &ReveseProxyRoute{
 			Route: &Route{
-				Alias:    cfg.Link + "-loadbalancer",
-				Homepage: r.Homepage,
+				Alias:     cfg.Link + "-loadbalancer",
+				Homepage:  r.Homepage,
+				HealthMon: lb,
 			},
-			HealthMon:    lb,
 			loadBalancer: lb,
 			handler:      lb,
 		}
diff --git a/internal/route/route.go b/internal/route/route.go
index 684a4db..54e0794 100644
--- a/internal/route/route.go
+++ b/internal/route/route.go
@@ -51,7 +51,8 @@ type (
 		Homepage     *homepage.ItemConfig           `json:"homepage,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:"-"`
 	}
@@ -68,7 +69,9 @@ type (
 
 		Excluded *bool `json:"excluded"`
 
-		impl        routes.Route
+		impl routes.Route
+		task *task.Task
+
 		isValidated bool
 		lastError   gperr.Error
 		provider    routes.Provider
@@ -243,7 +246,7 @@ func (r *Route) Impl() routes.Route {
 }
 
 func (r *Route) Task() *task.Task {
-	return r.impl.Task()
+	return r.task
 }
 
 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 {
 		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
 	} else {
 		// reference here because r.impl will be nil after Finish() is called.
 		impl := r.impl
-		impl.Task().OnCancel("remove_routes_from_all", func() {
+		r.task.OnCancel("remove_routes_from_all", func() {
 			routes.All.Del(impl)
 		})
 	}
@@ -285,7 +288,7 @@ func (r *Route) FinishAndWait(reason any) {
 	if r.impl == nil {
 		return
 	}
-	r.impl.Task().FinishAndWait(reason)
+	r.task.FinishAndWait(reason)
 	r.impl = nil
 }
 
@@ -360,7 +363,7 @@ func (r *Route) IsAgent() bool {
 }
 
 func (r *Route) HealthMonitor() health.HealthMonitor {
-	return r.impl.HealthMonitor()
+	return r.HealthMon
 }
 
 func (r *Route) IdlewatcherConfig() *idlewatcher.Config {
diff --git a/internal/route/stream.go b/internal/route/stream.go
index 077a89c..dddcdf7 100755
--- a/internal/route/stream.go
+++ b/internal/route/stream.go
@@ -11,20 +11,14 @@ import (
 	net "github.com/yusing/go-proxy/internal/net/types"
 	"github.com/yusing/go-proxy/internal/route/routes"
 	"github.com/yusing/go-proxy/internal/task"
-	"github.com/yusing/go-proxy/internal/watcher/health"
 	"github.com/yusing/go-proxy/internal/watcher/health/monitor"
 )
 
 // TODO: support stream load balance.
 type StreamRoute struct {
 	*Route
-
 	net.Stream `json:"-"`
 
-	HealthMon health.HealthMonitor `json:"health"`
-
-	task *task.Task
-
 	l zerolog.Logger
 }
 
@@ -88,20 +82,6 @@ func (r *StreamRoute) Start(parent task.Parent) gperr.Error {
 	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() {
 	defer r.task.Finish("listener closed")