diff --git a/internal/route/fileserver.go b/internal/route/fileserver.go index b2278e8..35b8d6b 100644 --- a/internal/route/fileserver.go +++ b/internal/route/fileserver.go @@ -2,7 +2,6 @@ package route import ( "net/http" - "time" "github.com/yusing/go-proxy/internal/common" gphttp "github.com/yusing/go-proxy/internal/net/http" @@ -21,11 +20,12 @@ type ( FileServer struct { *Route + Health *monitor.FileServerHealthMonitor `json:"health"` + task *task.Task middleware *middleware.Middleware handler http.Handler accessLogger *accesslog.AccessLogger - startTime time.Time } ) @@ -49,8 +49,7 @@ func NewFileServer(base *Route) (*FileServer, E.Error) { // Start implements task.TaskStarter. func (s *FileServer) Start(parent task.Parent) E.Error { - s.startTime = time.Now() - s.task = parent.Subtask("fileserver."+s.Name(), false) + s.task = parent.Subtask("fileserver."+s.TargetName(), false) pathPatterns := s.PathPatterns switch { @@ -90,6 +89,11 @@ func (s *FileServer) Start(parent task.Parent) E.Error { s.task.OnCancel("reset_metrics", metricsLogger.ResetMetrics) } + if s.UseHealthCheck() { + s.Health = monitor.NewFileServerHealthMonitor(s.TargetName(), s.HealthCheck, s.Root) + s.Health.Start(s.task) + } + routes.SetHTTPRoute(s.TargetName(), s) s.task.OnCancel("entrypoint_remove_route", func() { routes.DeleteHTTPRoute(s.TargetName()) @@ -114,40 +118,6 @@ func (s *FileServer) ServeHTTP(w http.ResponseWriter, req *http.Request) { } } -// Status implements health.HealthMonitor. -func (s *FileServer) Status() health.Status { - return health.StatusHealthy -} - -// Uptime implements health.HealthMonitor. -func (s *FileServer) Uptime() time.Duration { - return time.Since(s.startTime) -} - -// Latency implements health.HealthMonitor. -func (s *FileServer) Latency() time.Duration { - return 0 -} - -// MarshalJSON implements json.Marshaler. -func (s *FileServer) MarshalJSON() ([]byte, error) { - return (&monitor.JSONRepresentation{ - Name: s.Alias, - Config: nil, - Status: s.Status(), - Started: s.startTime, - Uptime: s.Uptime(), - Latency: s.Latency(), - LastSeen: time.Now(), - Detail: "", - URL: nil, - }).MarshalJSON() -} - -func (s *FileServer) String() string { - return "FileServer " + s.Alias -} - -func (s *FileServer) Name() string { - return s.Alias +func (s *FileServer) HealthMonitor() health.HealthMonitor { + return s.Health } diff --git a/internal/watcher/health/monitor/fileserver.go b/internal/watcher/health/monitor/fileserver.go new file mode 100644 index 0000000..e62e392 --- /dev/null +++ b/internal/watcher/health/monitor/fileserver.go @@ -0,0 +1,36 @@ +package monitor + +import ( + "os" + "time" + + "github.com/yusing/go-proxy/internal/watcher/health" +) + +type FileServerHealthMonitor struct { + *monitor + path string +} + +func NewFileServerHealthMonitor(alias string, config *health.HealthCheckConfig, path string) *FileServerHealthMonitor { + mon := &FileServerHealthMonitor{path: path} + mon.monitor = newMonitor(nil, config, mon.CheckHealth) + mon.service = alias + return mon +} + +func (s *FileServerHealthMonitor) CheckHealth() (*health.HealthCheckResult, error) { + start := time.Now() + _, err := os.Stat(s.path) + + detail := "" + if err != nil { + detail = err.Error() + } + + return &health.HealthCheckResult{ + Healthy: err == nil, + Latency: time.Since(start), + Detail: detail, + }, nil +}