Files
gluetun/internal/cli/healthcheck.go

41 lines
963 B
Go
Raw Normal View History

package cli
import (
"context"
2021-07-22 20:45:17 +00:00
"net"
"net/http"
"time"
2021-07-22 20:45:17 +00:00
"github.com/qdm12/gluetun/internal/configuration"
"github.com/qdm12/gluetun/internal/healthcheck"
2021-07-22 20:45:17 +00:00
"github.com/qdm12/golibs/logging"
"github.com/qdm12/golibs/params"
)
type HealthChecker interface {
2021-08-22 20:44:14 +00:00
HealthCheck(ctx context.Context, env params.Interface, logger logging.Logger) error
}
2021-08-22 20:44:14 +00:00
func (c *CLI) HealthCheck(ctx context.Context, env params.Interface,
logger logging.Logger) error {
2021-07-22 20:45:17 +00:00
// Extract the health server port from the configuration.
config := configuration.Health{}
err := config.Read(env, logger)
2021-07-22 20:45:17 +00:00
if err != nil {
return err
}
_, port, err := net.SplitHostPort(config.ServerAddress)
if err != nil {
return err
}
2020-12-30 15:24:46 +00:00
const timeout = 10 * time.Second
httpClient := &http.Client{Timeout: timeout}
client := healthcheck.NewClient(httpClient)
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
2021-07-22 20:45:17 +00:00
url := "http://127.0.0.1:" + port
return client.Check(ctx, url)
}