healthcheck.go 804 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "io/ioutil"
  6. "net"
  7. "net/http"
  8. "os"
  9. "github.com/imgproxy/imgproxy/v3/config"
  10. "github.com/imgproxy/imgproxy/v3/config/configurators"
  11. )
  12. func healthcheck() int {
  13. network := config.Network
  14. bind := config.Bind
  15. configurators.String(&network, "IMGPROXY_NETWORK")
  16. configurators.String(&bind, "IMGPROXY_BIND")
  17. httpc := http.Client{
  18. Transport: &http.Transport{
  19. DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
  20. return net.Dial(network, bind)
  21. },
  22. },
  23. }
  24. res, err := httpc.Get("http://imgproxy/health")
  25. if err != nil {
  26. fmt.Fprintln(os.Stderr, err.Error())
  27. return 1
  28. }
  29. defer res.Body.Close()
  30. msg, _ := ioutil.ReadAll(res.Body)
  31. fmt.Fprintln(os.Stderr, string(msg))
  32. if res.StatusCode != 200 {
  33. return 1
  34. }
  35. return 0
  36. }