add header for http healthcheck

This commit is contained in:
Kaive Young 2024-03-19 16:18:28 +08:00
parent 002831ea82
commit f095130823
No known key found for this signature in database
GPG Key ID: 6270A77955E33D09
3 changed files with 18 additions and 3 deletions

View File

@ -40,8 +40,8 @@ type Monitor struct {
addr string
// For http
url string
url string
header http.Header
failedTimes uint64
statusOK bool
statusNormalFn func()
@ -73,6 +73,11 @@ func NewMonitor(ctx context.Context, cfg v1.HealthCheckConfig, addr string,
}
url = s + cfg.Path
}
header := make(http.Header)
for k, v := range cfg.Headers.Set {
header.Set(k, v)
}
return &Monitor{
checkType: cfg.Type,
interval: time.Duration(cfg.IntervalSeconds) * time.Second,
@ -80,6 +85,7 @@ func NewMonitor(ctx context.Context, cfg v1.HealthCheckConfig, addr string,
maxFailedTimes: cfg.MaxFailed,
addr: addr,
url: url,
header: header,
statusOK: false,
statusNormalFn: statusNormalFn,
statusFailedFn: statusFailedFn,
@ -163,6 +169,11 @@ func (monitor *Monitor) doHTTPCheck(ctx context.Context) error {
if err != nil {
return err
}
req.Header = monitor.header
for k, v := range req.Header {
fmt.Println(k, v)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err

View File

@ -216,7 +216,8 @@ healthCheck.path = "/status"
healthCheck.intervalSeconds = 10
healthCheck.maxFailed = 3
healthCheck.timeoutSeconds = 3
# set health check headers
healthCheck.headers.set.Host= = "example.com"
[[proxies]]
name = "web02"
type = "https"

View File

@ -97,6 +97,9 @@ type HealthCheckConfig struct {
// Path specifies the path to send health checks to if the
// health check type is "http".
Path string `json:"path,omitempty"`
// Headers specifies the headers to send health checks to if the
// health check type is "http".
Headers HeaderOperations `json:"headers,omitempty"`
}
type DomainConfig struct {