1
0

healthcheck.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "net"
  7. "net/http"
  8. "os"
  9. "github.com/imgproxy/imgproxy/v3/config"
  10. "github.com/imgproxy/imgproxy/v3/config/configurators"
  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. network := config.Network
  16. bind := config.Bind
  17. pathprefix := config.PathPrefix
  18. configurators.String(&network, "IMGPROXY_NETWORK")
  19. configurators.String(&bind, "IMGPROXY_BIND")
  20. configurators.URLPath(&pathprefix, "IMGPROXY_PATH_PREFIX")
  21. httpc := http.Client{
  22. Transport: &http.Transport{
  23. DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
  24. return net.Dial(network, bind)
  25. },
  26. },
  27. }
  28. res, err := httpc.Get(fmt.Sprintf("http://imgproxy%s/health", pathprefix))
  29. if err != nil {
  30. fmt.Fprintln(os.Stderr, err.Error())
  31. return cli.Exit(err, 1)
  32. }
  33. defer res.Body.Close()
  34. msg, _ := io.ReadAll(res.Body)
  35. fmt.Fprintln(os.Stderr, string(msg))
  36. if res.StatusCode != 200 {
  37. err := fmt.Errorf("healthcheck failed: %s", msg)
  38. return cli.Exit(err, 1)
  39. }
  40. return nil
  41. }