-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtraefik_client.go
86 lines (71 loc) · 1.74 KB
/
traefik_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
package traefik
import (
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)
type ITraefikClient interface {
GetHttpRouters() (*[]HttpRouter, error)
}
type TraefikClient struct {
ITraefikClient
httpRoutersUrl string
config *TraefikConfig
client *http.Client
}
func NewTraefikClient(cfg *TraefikConfig) (*TraefikClient, error) {
httpRoutersUrl, err := url.JoinPath(cfg.baseUrl.String(), "/http/routers")
if err != nil {
return nil, err
}
client := &TraefikClient{
httpRoutersUrl: httpRoutersUrl,
config: cfg,
}
if cfg.insecureSkipVerify {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client.client = &http.Client{Transport: tr}
} else {
client.client = &http.Client{}
}
return client, nil
}
func (c *TraefikClient) GetHttpRouters() (*[]HttpRouter, error) {
log.Debugf("Connecting to %s", c.httpRoutersUrl)
response, err := c.client.Get(c.httpRoutersUrl)
if err != nil {
log.Errorf("Failed to fetch http routers: %q", err)
return nil, err
}
var body []byte
var readErr error
if response.Body != nil {
defer response.Body.Close()
body, readErr = io.ReadAll(response.Body)
}
if readErr != nil {
log.Errorf("Failed to read response body: %q", readErr)
return nil, readErr
}
if response.StatusCode != 200 {
if body != nil {
bodyStr := string(body[:])
return nil, fmt.Errorf("Received %d response from API: %s", response.StatusCode, bodyStr)
} else {
return nil, fmt.Errorf("Received %d response from API", response.StatusCode)
}
}
result := []HttpRouter{}
if body != nil {
err = json.Unmarshal(body, &result)
if err != nil {
return nil, fmt.Errorf("Failed to parse json body: %q", err)
}
}
return &result, nil
}