mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-14 18:30:02 +08:00
chore: coding style (#4037)
This commit is contained in:
@@ -9,5 +9,5 @@ type Config struct {
|
|||||||
HealthPath string `json:",default=/healthz"`
|
HealthPath string `json:",default=/healthz"`
|
||||||
EnableMetrics bool `json:",default=true"`
|
EnableMetrics bool `json:",default=true"`
|
||||||
EnablePprof bool `json:",default=true"`
|
EnablePprof bool `json:",default=true"`
|
||||||
HealthRespInfo string `json:",default=OK"`
|
HealthResponse string `json:",default=OK"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,8 +16,8 @@ import (
|
|||||||
|
|
||||||
var once sync.Once
|
var once sync.Once
|
||||||
|
|
||||||
// Server is inner http server, expose some useful observability information of app.
|
// Server is an inner http server, expose some useful observability information of app.
|
||||||
// For example health check, metrics and pprof.
|
// For example, health check, metrics and pprof.
|
||||||
type Server struct {
|
type Server struct {
|
||||||
config Config
|
config Config
|
||||||
server *http.ServeMux
|
server *http.ServeMux
|
||||||
@@ -37,8 +37,9 @@ func (s *Server) addRoutes(c Config) {
|
|||||||
s.handleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
|
s.handleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
|
||||||
_ = json.NewEncoder(w).Encode(s.routes)
|
_ = json.NewEncoder(w).Encode(s.routes)
|
||||||
})
|
})
|
||||||
|
|
||||||
// health
|
// health
|
||||||
s.handleFunc(s.config.HealthPath, health.CreateHttpHandler(c.HealthRespInfo))
|
s.handleFunc(s.config.HealthPath, health.CreateHttpHandler(c.HealthResponse))
|
||||||
|
|
||||||
// metrics
|
// metrics
|
||||||
if s.config.EnableMetrics {
|
if s.config.EnableMetrics {
|
||||||
@@ -46,6 +47,7 @@ func (s *Server) addRoutes(c Config) {
|
|||||||
prometheus.Enable()
|
prometheus.Enable()
|
||||||
s.handleFunc(s.config.MetricsPath, promhttp.Handler().ServeHTTP)
|
s.handleFunc(s.config.MetricsPath, promhttp.Handler().ServeHTTP)
|
||||||
}
|
}
|
||||||
|
|
||||||
// pprof
|
// pprof
|
||||||
if s.config.EnablePprof {
|
if s.config.EnablePprof {
|
||||||
s.handleFunc("/debug/pprof/", pprof.Index)
|
s.handleFunc("/debug/pprof/", pprof.Index)
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import (
|
|||||||
var defaultHealthManager = newComboHealthManager()
|
var defaultHealthManager = newComboHealthManager()
|
||||||
|
|
||||||
type (
|
type (
|
||||||
// Probe represents readiness status of given component.
|
// Probe represents readiness status of a given component.
|
||||||
Probe interface {
|
Probe interface {
|
||||||
// MarkReady sets a ready state for the endpoint handlers.
|
// MarkReady sets a ready state for the endpoint handlers.
|
||||||
MarkReady()
|
MarkReady()
|
||||||
@@ -44,10 +44,10 @@ func AddProbe(probe Probe) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateHttpHandler create health http handler base on given 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) {
|
return func(w http.ResponseWriter, _ *http.Request) {
|
||||||
if defaultHealthManager.IsReady() {
|
if defaultHealthManager.IsReady() {
|
||||||
_, _ = w.Write([]byte(healthRespInfo))
|
_, _ = w.Write([]byte(healthResponse))
|
||||||
} else {
|
} else {
|
||||||
http.Error(w, "Service Unavailable\n"+defaultHealthManager.verboseInfo(),
|
http.Error(w, "Service Unavailable\n"+defaultHealthManager.verboseInfo(),
|
||||||
http.StatusServiceUnavailable)
|
http.StatusServiceUnavailable)
|
||||||
|
|||||||
Reference in New Issue
Block a user