healthcheck.go 685 B

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