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 {
|
type Config struct {
|
||||||
HealthURL string
|
HealthURL string
|
||||||
StaticIP string
|
StaticIP string
|
||||||
|
Port string
|
||||||
|
UseHTTPS bool
|
||||||
NormalInterval time.Duration
|
NormalInterval time.Duration
|
||||||
FailInterval time.Duration
|
FailInterval time.Duration
|
||||||
Timeout 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.HealthURL, "url", "/nginx-health", "Health endpoint path")
|
||||||
flag.StringVar(&cfg.StaticIP, "ip", "", "Static IP to check")
|
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.NormalInterval, "interval", 120*time.Second, "Normal check interval")
|
||||||
flag.DurationVar(&cfg.FailInterval, "fail-interval", 10*time.Second, "Interval during failure detection")
|
flag.DurationVar(&cfg.FailInterval, "fail-interval", 10*time.Second, "Interval during failure detection")
|
||||||
flag.DurationVar(&cfg.Timeout, "timeout", 5*time.Second, "HTTP timeout")
|
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)
|
ctx, cancel := context.WithTimeout(context.Background(), cfg.Timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
|
scheme := "http"
|
||||||
|
if cfg.UseHTTPS {
|
||||||
|
scheme = "https"
|
||||||
|
}
|
||||||
|
|
||||||
dialer := &net.Dialer{}
|
dialer := &net.Dialer{}
|
||||||
transport := &http.Transport{
|
transport := &http.Transport{
|
||||||
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
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,
|
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)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user