Skip to content

Commit

Permalink
feat: added /health route for naarad-api
Browse files Browse the repository at this point in the history
  • Loading branch information
proffapt committed Jul 1, 2024
1 parent b25ac4d commit 84bc1c5
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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()

Expand All @@ -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"},
Expand Down

0 comments on commit 84bc1c5

Please sign in to comment.