2020-12-29 18:24:03 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2021-07-22 20:45:17 +00:00
|
|
|
"net"
|
2020-12-29 18:24:03 +00:00
|
|
|
"net/http"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/qdm12/gluetun/internal/healthcheck"
|
|
|
|
|
)
|
|
|
|
|
|
2022-08-26 15:03:59 +00:00
|
|
|
func (c *CLI) HealthCheck(ctx context.Context, source Source, warner Warner) error {
|
2021-07-22 20:45:17 +00:00
|
|
|
// Extract the health server port from the configuration.
|
2022-01-06 06:40:23 -05:00
|
|
|
config, err := source.ReadHealth()
|
2021-07-22 20:45:17 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2022-01-06 06:40:23 -05:00
|
|
|
|
|
|
|
|
err = config.Validate()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-22 20:45:17 +00:00
|
|
|
_, port, err := net.SplitHostPort(config.ServerAddress)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-30 15:24:46 +00:00
|
|
|
const timeout = 10 * time.Second
|
2020-12-29 18:24:03 +00:00
|
|
|
httpClient := &http.Client{Timeout: timeout}
|
2021-07-23 19:22:41 +00:00
|
|
|
client := healthcheck.NewClient(httpClient)
|
2020-12-29 18:24:03 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, timeout)
|
|
|
|
|
defer cancel()
|
2021-07-22 20:45:17 +00:00
|
|
|
|
|
|
|
|
url := "http://127.0.0.1:" + port
|
2021-07-23 19:22:41 +00:00
|
|
|
return client.Check(ctx, url)
|
2020-12-29 18:24:03 +00:00
|
|
|
}
|