-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchecker.go
More file actions
51 lines (42 loc) · 1.04 KB
/
Copy pathchecker.go
File metadata and controls
51 lines (42 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package deploy
import (
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
type HealthStatus struct {
Status string `json:"status"`
CanRestart bool `json:"can_restart"`
}
type HealthChecker interface {
Check(url string) (*HealthStatus, error)
}
type Checker struct {
client *http.Client
}
func NewChecker() *Checker {
return &Checker{
client: &http.Client{Timeout: 5 * time.Second},
}
}
// Check performs a health check on the given URL.
func (c *Checker) Check(url string) (*HealthStatus, error) {
resp, err := c.client.Get(url)
if err != nil {
return nil, fmt.Errorf("health check failed: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("health check returned status %d", resp.StatusCode)
}
return ParseHealthResponse(resp.Body)
}
func ParseHealthResponse(r io.Reader) (*HealthStatus, error) {
var status HealthStatus
if err := json.NewDecoder(r).Decode(&status); err != nil {
return nil, fmt.Errorf("failed to decode health response: %w", err)
}
return &status, nil
}