-
Notifications
You must be signed in to change notification settings - Fork 3
/
expvars.go
54 lines (43 loc) · 854 Bytes
/
expvars.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
package main
import (
"fmt"
"net/http"
"net/url"
"time"
"github.com/antonholmquist/jason"
)
type Expvars struct {
*jason.Object
}
type Fetcher interface {
Fetch(url url.URL) (*Expvars, error)
}
type fetcher struct {
client *http.Client
}
func NewFetcher() Fetcher {
return &fetcher{
client: &http.Client{
Timeout: time.Second,
},
}
}
func (f *fetcher) Fetch(url url.URL) (*Expvars, error) {
req, err := http.NewRequest(http.MethodGet, url.String(), nil)
if err != nil {
return nil, err
}
resp, err := f.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Could not fetch expvars from %s", url.String())
}
object, err := jason.NewObjectFromReader(resp.Body)
if err != nil {
return nil, err
}
return &Expvars{object}, nil
}