mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-07 15:10:01 +08:00
add custom health response information (#4034)
Co-authored-by: Kevin Wan <wanjunfeng@gmail.com>
This commit is contained in:
@@ -2,11 +2,12 @@ package devserver
|
||||
|
||||
// Config is config for inner http server.
|
||||
type Config struct {
|
||||
Enabled bool `json:",default=true"`
|
||||
Host string `json:",optional"`
|
||||
Port int `json:",default=6060"`
|
||||
MetricsPath string `json:",default=/metrics"`
|
||||
HealthPath string `json:",default=/healthz"`
|
||||
EnableMetrics bool `json:",default=true"`
|
||||
EnablePprof bool `json:",default=true"`
|
||||
Enabled bool `json:",default=true"`
|
||||
Host string `json:",optional"`
|
||||
Port int `json:",default=6060"`
|
||||
MetricsPath string `json:",default=/metrics"`
|
||||
HealthPath string `json:",default=/healthz"`
|
||||
EnableMetrics bool `json:",default=true"`
|
||||
EnablePprof bool `json:",default=true"`
|
||||
HealthRespInfo string `json:",default=OK"`
|
||||
}
|
||||
|
||||
@@ -32,13 +32,13 @@ func NewServer(config Config) *Server {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) addRoutes() {
|
||||
func (s *Server) addRoutes(c Config) {
|
||||
// route path, routes list
|
||||
s.handleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
|
||||
_ = json.NewEncoder(w).Encode(s.routes)
|
||||
})
|
||||
// health
|
||||
s.handleFunc(s.config.HealthPath, health.CreateHttpHandler())
|
||||
s.handleFunc(s.config.HealthPath, health.CreateHttpHandler(c.HealthRespInfo))
|
||||
|
||||
// metrics
|
||||
if s.config.EnableMetrics {
|
||||
@@ -62,8 +62,8 @@ func (s *Server) handleFunc(pattern string, handler http.HandlerFunc) {
|
||||
}
|
||||
|
||||
// StartAsync start inner http server background.
|
||||
func (s *Server) StartAsync() {
|
||||
s.addRoutes()
|
||||
func (s *Server) StartAsync(c Config) {
|
||||
s.addRoutes(c)
|
||||
threading.GoSafe(func() {
|
||||
addr := fmt.Sprintf("%s:%d", s.config.Host, s.config.Port)
|
||||
logx.Infof("Starting dev http server at %s", addr)
|
||||
@@ -78,7 +78,7 @@ func StartAgent(c Config) {
|
||||
once.Do(func() {
|
||||
if c.Enabled {
|
||||
s := NewServer(c)
|
||||
s.StartAsync()
|
||||
s.StartAsync(c)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user