made the port and if use https or not configurable
This commit is contained in:
13
main.go
13
main.go
@@ -13,6 +13,8 @@ import (
|
||||
type Config struct {
|
||||
HealthURL string
|
||||
StaticIP string
|
||||
Port string
|
||||
UseHTTPS bool
|
||||
NormalInterval time.Duration
|
||||
FailInterval time.Duration
|
||||
Timeout time.Duration
|
||||
@@ -101,6 +103,8 @@ func parseFlags() *Config {
|
||||
|
||||
flag.StringVar(&cfg.HealthURL, "url", "/nginx-health", "Health endpoint path")
|
||||
flag.StringVar(&cfg.StaticIP, "ip", "", "Static IP to check")
|
||||
flag.StringVar(&cfg.Port, "port", "80", "Port to check")
|
||||
flag.BoolVar(&cfg.UseHTTPS, "https", false, "Use HTTPS")
|
||||
flag.DurationVar(&cfg.NormalInterval, "interval", 120*time.Second, "Normal check interval")
|
||||
flag.DurationVar(&cfg.FailInterval, "fail-interval", 10*time.Second, "Interval during failure detection")
|
||||
flag.DurationVar(&cfg.Timeout, "timeout", 5*time.Second, "HTTP timeout")
|
||||
@@ -123,10 +127,15 @@ func checkHealth(cfg *Config) bool {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), cfg.Timeout)
|
||||
defer cancel()
|
||||
|
||||
scheme := "http"
|
||||
if cfg.UseHTTPS {
|
||||
scheme = "https"
|
||||
}
|
||||
|
||||
dialer := &net.Dialer{}
|
||||
transport := &http.Transport{
|
||||
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
return dialer.DialContext(ctx, "tcp", net.JoinHostPort(cfg.StaticIP, "80"))
|
||||
return dialer.DialContext(ctx, "tcp", net.JoinHostPort(cfg.StaticIP, cfg.Port))
|
||||
},
|
||||
}
|
||||
|
||||
@@ -135,7 +144,7 @@ func checkHealth(cfg *Config) bool {
|
||||
Timeout: cfg.Timeout,
|
||||
}
|
||||
|
||||
req, _ := http.NewRequestWithContext(ctx, "GET", "http://"+cfg.StaticIP+cfg.HealthURL, nil)
|
||||
req, _ := http.NewRequestWithContext(ctx, "GET", scheme+"://"+cfg.StaticIP+cfg.HealthURL, nil)
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user