-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.go
More file actions
117 lines (99 loc) · 2.48 KB
/
options.go
File metadata and controls
117 lines (99 loc) · 2.48 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
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
package healthchecks
import (
"errors"
"fmt"
"net/http"
"net/url"
"time"
)
type options struct {
RootURL *url.URL
HTTPClient *http.Client
}
func defaultOptions() *options {
return &options{
RootURL: mustURL("https://hc-ping.com"),
HTTPClient: &http.Client{
Transport: http.DefaultTransport,
CheckRedirect: http.DefaultClient.CheckRedirect,
Jar: http.DefaultClient.Jar,
Timeout: 10 * time.Second,
},
}
}
func optsFromDefaults(opts []Option) (*options, error) {
options := defaultOptions()
for _, o := range opts {
if err := o.apply(options); err != nil {
return nil, fmt.Errorf("applying option: %w", err)
}
}
return options, nil
}
// Option applies a configuration option.
type Option interface {
apply(opts *options) error
}
type urlOption string
var _ Option = urlOption("")
func (u urlOption) apply(opts *options) error {
parsed, err := url.Parse(string(u))
if err != nil {
return fmt.Errorf("parse URL: %w", err)
}
if parsed.Scheme == "" || parsed.Host == "" {
return fmt.Errorf("invalid URL: %s", u)
}
opts.RootURL = parsed
return nil
}
// WithURL overrides the default URL https://hc-ping.com.
//
// The format should be http[s]://example.com[/suffix].
func WithURL(u string) Option {
return urlOption(u)
}
func mustURL(u string) *url.URL {
parsed, err := url.Parse(u)
if err != nil {
panic(err)
}
return parsed
}
type timeoutOption time.Duration
var _ Option = timeoutOption(0)
func (t timeoutOption) apply(opts *options) error {
if t < 0 {
return fmt.Errorf("timeout is %d, needs to be > 0", t)
}
opts.HTTPClient.Timeout = time.Duration(t)
return nil
}
// WithTimeout sets the timeout for signalling requests (/start, ...).
//
// The default is 10s.
//
// Note that you can also supply a timeout via [WithHTTPClient].
// Whichever option is provided last will take precedence.
func WithTimeout(t time.Duration) Option {
return timeoutOption(t)
}
type httpClientOption struct {
client *http.Client
}
var _ Option = httpClientOption{}
func (h httpClientOption) apply(opts *options) error {
if h.client == nil {
return errors.New("HTTP client must be non-nil")
}
opts.HTTPClient = h.client
return nil
}
// WithHTTPClient sets a custom HTTP client to be used during signalling requests.
//
// Default is [http.DefaultClient].
//
// Note that providing [WithTimeout] after [WithHTTPClient] will overwrite this client's timeout value.
func WithHTTPClient(client *http.Client) Option {
return httpClientOption{client: client}
}