From 369fd9b92bbc80d9dc504bbc5e1b92d93ad907b9 Mon Sep 17 00:00:00 2001 From: Yunhe Liu Date: Wed, 9 Aug 2023 19:04:17 +0000 Subject: [PATCH 1/3] adding health check routes --- app/routes.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/routes.go b/app/routes.go index e8f70029..77128be0 100644 --- a/app/routes.go +++ b/app/routes.go @@ -19,7 +19,7 @@ func (a *App) routes() { a.clusterRoutes(apiV1) a.configRoutes(apiV1) a.targetRoutes(apiV1) - + a.healthRoutes(apiV1) } func (a *App) clusterRoutes(r *mux.Router) { @@ -59,3 +59,7 @@ func (a *App) targetRoutes(r *mux.Router) { r.HandleFunc("/targets/{id}", a.handleTargetsPost).Methods(http.MethodPost) r.HandleFunc("/targets/{id}", a.handleTargetsDelete).Methods(http.MethodDelete) } + +func (a *App) healthRoutes(r *mux.Router) { + r.HandleFunc("/healthz", a.handleHealthzGet).Methods(http.MethodGet) +} From 45eed910936967792c42f4ede286d1311eab4ccd Mon Sep 17 00:00:00 2001 From: Yunhe Liu Date: Wed, 9 Aug 2023 19:24:21 +0000 Subject: [PATCH 2/3] add health check handler --- app/api.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/api.go b/app/api.go index 156d25bc..f15372d3 100644 --- a/app/api.go +++ b/app/api.go @@ -287,6 +287,10 @@ func (a *App) handleClusteringGet(w http.ResponseWriter, r *http.Request) { w.Write(b) } +func (a *App) handleHealthzGet(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("I'm healthy!\n")) +} + func (a *App) handleClusteringMembersGet(w http.ResponseWriter, r *http.Request) { if a.Config.Clustering == nil { return From ed01699366bd59e527f4d9f65ccce0cf5565a433 Mon Sep 17 00:00:00 2001 From: Yunhe Liu Date: Wed, 9 Aug 2023 20:38:25 +0000 Subject: [PATCH 3/3] make health check reponse idiomatic --- app/api.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/api.go b/app/api.go index f15372d3..d85e8c25 100644 --- a/app/api.go +++ b/app/api.go @@ -288,7 +288,14 @@ func (a *App) handleClusteringGet(w http.ResponseWriter, r *http.Request) { } func (a *App) handleHealthzGet(w http.ResponseWriter, r *http.Request) { - w.Write([]byte("I'm healthy!\n")) + s := map[string]string{"status": "healthy",} + b, err := json.Marshal(s) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + json.NewEncoder(w).Encode(APIErrors{Errors: []string{err.Error()}}) + return + } + w.Write(b) } func (a *App) handleClusteringMembersGet(w http.ResponseWriter, r *http.Request) {