-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
147 lines (118 loc) · 2.78 KB
/
client.go
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package health
import (
"context"
"fmt"
"log/slog"
"net"
"net/http"
"os"
"strconv"
"time"
"github.com/go-http-utils/headers"
sauté "gitlab.com/biffen/saute"
)
var _ Option = optionFunc(nil)
// CheckHealth gets a Response from an HTTP server.
func CheckHealth(ctx context.Context, options ...Option) (*Response, error) {
ctx, span := sauté.TraceFunc(ctx, nil)
defer span.End()
c := config{
Host: "localhost",
Port: port(),
Timeout: timeout,
}
for _, option := range options {
if err := option.apply(&c); err != nil {
return nil, fmt.Errorf("invalid option: %w", err)
}
}
var (
addr = fmt.Sprintf(
"http://%s/",
net.JoinHostPort(c.Host, strconv.FormatUint(uint64(c.Port), 10)),
)
client = http.Client{
Timeout: c.Timeout,
}
)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, addr, nil)
if err != nil {
return nil, fmt.Errorf("failed to create HTTP request: %w", err)
}
req.Header.Add(headers.Accept, ContentType)
httpResp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to send HTTP request: %w", err)
}
defer httpResp.Body.Close()
resp, err := ReadResponse(httpResp.Body)
if err != nil {
return nil, err
}
return resp, nil
}
const (
// ExitErr is the exit code on failure.
ExitErr = 1
// ExitUser is the exit code when the user did something wrong.
ExitUser = 2
timeout = 30 * time.Second
)
// Main is a utility for services that exits the current process with 0 or 1 for
// a healthy or unhealthy state, respectively.
func Main(ctx context.Context) {
var exit int
defer func() {
//nolint:revive // Exiting here is intentional.
os.Exit(exit)
}()
ctx, span := sauté.TraceFunc(ctx, nil)
defer span.End()
resp, err := CheckHealth(ctx)
if err != nil {
slog.ErrorContext(ctx, "error",
slog.Any("error", err),
)
exit = ExitErr
return
}
_, _ = resp.Write(os.Stdout)
if !resp.Good() {
exit = ExitErr
}
}
// Option is an optional configuration for [CheckHealth].
type Option interface {
apply(*config) error
}
// WithHost is an [Option] for [CheckHealth] to specify the host.
func WithHost(host string) Option {
return optionFunc(func(c *config) error {
c.Host = host
return nil
})
}
// WithPort is an [Option] for [CheckHealth] to specify the port number.
func WithPort(port uint16) Option {
return optionFunc(func(c *config) error {
c.Port = port
return nil
})
}
// WithTimeout is an [Option] for [CheckHealth] to specify a timeout. See
// [net/http.Client.Timeout].
func WithTimeout(timeout time.Duration) Option {
return optionFunc(func(c *config) error {
c.Timeout = timeout
return nil
})
}
type config struct {
Host string
Port uint16
Timeout time.Duration
}
type optionFunc func(*config) error
func (f optionFunc) apply(c *config) error {
return f(c)
}