1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2024-12-09 09:56:01 +02:00
imgproxy/healthcheck.go

48 lines
913 B
Go
Raw Normal View History

2020-02-04 13:48:33 +02:00
package main
import (
"context"
"fmt"
2022-10-28 17:15:13 +02:00
"io"
2020-02-04 13:48:33 +02:00
"net"
"net/http"
"os"
2021-04-26 13:52:50 +02:00
2021-09-30 16:23:30 +02:00
"github.com/imgproxy/imgproxy/v3/config"
"github.com/imgproxy/imgproxy/v3/config/configurators"
2020-02-04 13:48:33 +02:00
)
2020-02-27 17:44:59 +02:00
func healthcheck() int {
2021-04-26 13:52:50 +02:00
network := config.Network
bind := config.Bind
pathprefix := config.PathPrefix
2020-02-04 13:48:33 +02:00
2021-04-26 13:52:50 +02:00
configurators.String(&network, "IMGPROXY_NETWORK")
configurators.String(&bind, "IMGPROXY_BIND")
configurators.URLPath(&pathprefix, "IMGPROXY_PATH_PREFIX")
2020-02-04 13:48:33 +02:00
httpc := http.Client{
Transport: &http.Transport{
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
return net.Dial(network, bind)
},
},
}
res, err := httpc.Get(fmt.Sprintf("http://imgproxy%s/health", pathprefix))
2020-02-04 13:48:33 +02:00
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
2020-02-27 17:44:59 +02:00
return 1
2020-02-04 13:48:33 +02:00
}
defer res.Body.Close()
2022-10-28 17:15:13 +02:00
msg, _ := io.ReadAll(res.Body)
2020-02-04 13:48:33 +02:00
fmt.Fprintln(os.Stderr, string(msg))
if res.StatusCode != 200 {
2020-02-27 17:44:59 +02:00
return 1
2020-02-04 13:48:33 +02:00
}
2020-02-27 17:44:59 +02:00
return 0
2020-02-04 13:48:33 +02:00
}