health_handler.go 868 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package api
  2. import (
  3. "net/http"
  4. )
  5. // HandleLive responds immediately with an HTTP 200 status.
  6. func (app *App) HandleLive(w http.ResponseWriter, r *http.Request) {
  7. writeHealthy(w)
  8. }
  9. // HandleReady responds with HTTP 200 if healthy, 500 otherwise
  10. func (app *App) HandleReady(w http.ResponseWriter, r *http.Request) {
  11. if app.db == nil {
  12. writeHealthy(w)
  13. return
  14. }
  15. db, err := app.db.DB()
  16. if err != nil {
  17. app.handleErrorInternal(err, w)
  18. return
  19. }
  20. if err := db.Ping(); err != nil {
  21. app.handleErrorInternal(err, w)
  22. return
  23. }
  24. writeHealthy(w)
  25. }
  26. func writeHealthy(w http.ResponseWriter) {
  27. w.Header().Set("Content-Type", "text/plain")
  28. w.WriteHeader(http.StatusOK)
  29. w.Write([]byte("."))
  30. }
  31. func writeUnhealthy(w http.ResponseWriter) {
  32. w.Header().Set("Content-Type", "text/plain")
  33. w.WriteHeader(http.StatusInternalServerError)
  34. w.Write([]byte("."))
  35. }