health.go 995 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package handlers
  2. import (
  3. "net/http"
  4. "github.com/imgproxy/imgproxy/v3/httpheaders"
  5. "github.com/imgproxy/imgproxy/v3/ierrors"
  6. "github.com/imgproxy/imgproxy/v3/server"
  7. "github.com/imgproxy/imgproxy/v3/vips"
  8. )
  9. var imgproxyIsRunningMsg = []byte("imgproxy is running")
  10. // HealthHandler handles the health check requests
  11. func HealthHandler(reqID string, rw http.ResponseWriter, r *http.Request) error {
  12. var (
  13. status int
  14. msg []byte
  15. ierr *ierrors.Error
  16. )
  17. if err := vips.Health(); err == nil {
  18. status = http.StatusOK
  19. msg = imgproxyIsRunningMsg
  20. } else {
  21. status = http.StatusInternalServerError
  22. msg = []byte("Error")
  23. ierr = ierrors.Wrap(err, 1)
  24. }
  25. if len(msg) == 0 {
  26. msg = []byte{' '}
  27. }
  28. // Log response only if something went wrong
  29. if ierr != nil {
  30. server.LogResponse(reqID, r, status, ierr)
  31. }
  32. rw.Header().Set(httpheaders.ContentType, "text/plain")
  33. rw.Header().Set(httpheaders.CacheControl, "no-cache")
  34. rw.WriteHeader(status)
  35. rw.Write(msg)
  36. return nil
  37. }