From 08563482e595ed6b2280844639e4c75f73dc1138 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Wed, 3 Apr 2024 22:55:52 +0800 Subject: [PATCH] chore: coding style (#4037) --- internal/devserver/config.go | 2 +- internal/devserver/server.go | 8 +++++--- internal/health/health.go | 6 +++--- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/internal/devserver/config.go b/internal/devserver/config.go index aa11b2fd..1f94e475 100644 --- a/internal/devserver/config.go +++ b/internal/devserver/config.go @@ -9,5 +9,5 @@ type Config struct { HealthPath string `json:",default=/healthz"` EnableMetrics bool `json:",default=true"` EnablePprof bool `json:",default=true"` - HealthRespInfo string `json:",default=OK"` + HealthResponse string `json:",default=OK"` } diff --git a/internal/devserver/server.go b/internal/devserver/server.go index 57293adc..e6572271 100644 --- a/internal/devserver/server.go +++ b/internal/devserver/server.go @@ -16,8 +16,8 @@ import ( var once sync.Once -// Server is inner http server, expose some useful observability information of app. -// For example health check, metrics and pprof. +// Server is an inner http server, expose some useful observability information of app. +// For example, health check, metrics and pprof. type Server struct { config Config server *http.ServeMux @@ -37,8 +37,9 @@ func (s *Server) addRoutes(c Config) { s.handleFunc("/", func(w http.ResponseWriter, _ *http.Request) { _ = json.NewEncoder(w).Encode(s.routes) }) + // health - s.handleFunc(s.config.HealthPath, health.CreateHttpHandler(c.HealthRespInfo)) + s.handleFunc(s.config.HealthPath, health.CreateHttpHandler(c.HealthResponse)) // metrics if s.config.EnableMetrics { @@ -46,6 +47,7 @@ func (s *Server) addRoutes(c Config) { prometheus.Enable() s.handleFunc(s.config.MetricsPath, promhttp.Handler().ServeHTTP) } + // pprof if s.config.EnablePprof { s.handleFunc("/debug/pprof/", pprof.Index) diff --git a/internal/health/health.go b/internal/health/health.go index 57071e72..54d1f57f 100644 --- a/internal/health/health.go +++ b/internal/health/health.go @@ -13,7 +13,7 @@ import ( var defaultHealthManager = newComboHealthManager() type ( - // Probe represents readiness status of given component. + // Probe represents readiness status of a given component. Probe interface { // MarkReady sets a ready state for the endpoint handlers. MarkReady() @@ -44,10 +44,10 @@ func AddProbe(probe Probe) { } // CreateHttpHandler create health http handler base on given probe. -func CreateHttpHandler(healthRespInfo string) http.HandlerFunc { +func CreateHttpHandler(healthResponse string) http.HandlerFunc { return func(w http.ResponseWriter, _ *http.Request) { if defaultHealthManager.IsReady() { - _, _ = w.Write([]byte(healthRespInfo)) + _, _ = w.Write([]byte(healthResponse)) } else { http.Error(w, "Service Unavailable\n"+defaultHealthManager.verboseInfo(), http.StatusServiceUnavailable)