-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_checker.go
43 lines (34 loc) · 880 Bytes
/
http_checker.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
package checker
import (
"context"
"errors"
"fmt"
"net/http"
)
// ErrInvalidStatusCode when we have errors that are not in the 200 range.
var ErrInvalidStatusCode = errors.New("invalid status code")
// NewHTTPChecker with URL and client.
func NewHTTPChecker(url string, client *http.Client) *HTTPChecker {
return &HTTPChecker{url: url, client: client}
}
// HTTPChecker for a URL.
type HTTPChecker struct {
url string
client *http.Client
}
// Check the URL with a GET.
func (c *HTTPChecker) Check(ctx context.Context) error {
req, err := http.NewRequestWithContext(ctx, "GET", c.url, nil)
if err != nil {
return fmt.Errorf("http checker: %w", err)
}
resp, err := c.client.Do(req)
if err != nil {
return fmt.Errorf("http checker: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return ErrInvalidStatusCode
}
return nil
}