healthcheck.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "net"
  7. "net/http"
  8. "os"
  9. "github.com/imgproxy/imgproxy/v3/env"
  10. "github.com/imgproxy/imgproxy/v3/server"
  11. "github.com/urfave/cli/v3"
  12. )
  13. // healthcheck performs a healthcheck on a running imgproxy instance
  14. func healthcheck(ctx context.Context, c *cli.Command) error {
  15. var network, bind, pathprefix string
  16. env.String(&network, server.IMGPROXY_NETWORK)
  17. env.String(&bind, server.IMGPROXY_BIND)
  18. env.String(&pathprefix, server.IMGPROXY_PATH_PREFIX)
  19. httpc := http.Client{
  20. Transport: &http.Transport{
  21. DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
  22. return net.Dial(network, bind)
  23. },
  24. },
  25. }
  26. res, err := httpc.Get(fmt.Sprintf("http://imgproxy%s/health", pathprefix))
  27. if err != nil {
  28. fmt.Fprintln(os.Stderr, err.Error())
  29. return cli.Exit(err, 1)
  30. }
  31. defer res.Body.Close()
  32. msg, _ := io.ReadAll(res.Body)
  33. fmt.Fprintln(os.Stderr, string(msg))
  34. if res.StatusCode != 200 {
  35. err := fmt.Errorf("healthcheck failed: %s", msg)
  36. return cli.Exit(err, 1)
  37. }
  38. return nil
  39. }