From 84bc1c5e564a1ce3535bc2e47ce1648e732dc512 Mon Sep 17 00:00:00 2001 From: proffapt Date: Mon, 1 Jul 2024 09:43:19 +0530 Subject: [PATCH] feat: added /health route for naarad-api --- backend/main.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/backend/main.go b/backend/main.go index be37102..8588570 100644 --- a/backend/main.go +++ b/backend/main.go @@ -37,6 +37,10 @@ var jwtValidateResp struct { Email string `json:"email"` } +var healthResponse struct { + Healthy bool `json:"healthy"` +} + func PasswordGenerator(passwordLength int) string { password := "" source := rand.NewSource(time.Now().UnixNano()) @@ -158,6 +162,26 @@ func register(res http.ResponseWriter, req *http.Request) { } } +func healthCheck(res http.ResponseWriter, req *http.Request) { + res.Header().Set("Content-Type", "application/json") + + // Check database connection + err := db.Ping() + if err != nil { + healthResponse.Healthy = false + } else { + healthResponse.Healthy = true + } + + if !healthResponse.Healthy { + res.WriteHeader(http.StatusServiceUnavailable) + } else { + res.WriteHeader(http.StatusOK) + } + + json.NewEncoder(res).Encode(healthResponse) +} + func main() { initMailer() @@ -184,6 +208,7 @@ func main() { panic(err) } + http.HandleFunc("GET /health", healthCheck) http.HandleFunc("GET /register", register) c := cors.New(cors.Options{ AllowedOrigins: []string{"https://naarad.metakgp.org", "https://naarad-signup.metakgp.org", "http://localhost:3000"},