You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

30 lines
839 B

  1. package handler
  2. import (
  3. "net/http"
  4. "github.com/AFASystems/presence/internal/pkg/api/response"
  5. "gorm.io/gorm"
  6. )
  7. // Health returns OK and status "ok". Useful for liveness.
  8. func Health(w http.ResponseWriter, r *http.Request) {
  9. response.JSON(w, http.StatusOK, map[string]string{"status": "ok"})
  10. }
  11. // Ready checks DB connectivity and returns 200 if ready, 503 otherwise.
  12. func Ready(db *gorm.DB) http.HandlerFunc {
  13. return func(w http.ResponseWriter, r *http.Request) {
  14. sqlDB, err := db.DB()
  15. if err != nil {
  16. response.Error(w, http.StatusServiceUnavailable, "not_ready", "database not available")
  17. return
  18. }
  19. if err := sqlDB.Ping(); err != nil {
  20. response.Error(w, http.StatusServiceUnavailable, "not_ready", "database ping failed")
  21. return
  22. }
  23. response.JSON(w, http.StatusOK, map[string]string{"status": "ready"})
  24. }
  25. }