handler.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package health
  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. // Handler handles health requests
  11. type Handler struct{}
  12. // New creates new handler object
  13. func New() *Handler {
  14. return &Handler{}
  15. }
  16. // Execute handles the health request
  17. func (h *Handler) Execute(
  18. reqID string,
  19. rw server.ResponseWriter,
  20. req *http.Request,
  21. ) error {
  22. var (
  23. status int
  24. msg []byte
  25. ierr *ierrors.Error
  26. )
  27. if err := vips.Health(); err == nil {
  28. status = http.StatusOK
  29. msg = imgproxyIsRunningMsg
  30. } else {
  31. status = http.StatusInternalServerError
  32. msg = []byte("Error")
  33. ierr = ierrors.Wrap(err, 1)
  34. }
  35. if len(msg) == 0 {
  36. msg = []byte{' '}
  37. }
  38. // Log response only if something went wrong
  39. if ierr != nil {
  40. server.LogResponse(reqID, req, status, ierr)
  41. }
  42. rw.Header().Set(httpheaders.ContentType, "text/plain")
  43. rw.Header().Set(httpheaders.CacheControl, "no-cache")
  44. rw.WriteHeader(status)
  45. rw.Write(msg)
  46. return nil
  47. }