-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
45 lines (36 loc) · 841 Bytes
/
http.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
package main
import (
"encoding/base64"
"net"
"net/http"
"time"
)
type promRoundTripper struct {
basicAuth string
path string
client *http.Client
}
func (t *promRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
if len(t.basicAuth) > 0 {
r.Header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(t.basicAuth)))
}
if len(t.path) > 0 {
r.URL.Path = t.path + r.URL.Path
}
return t.client.Do(r)
}
func NewHTTPClient() *http.Client {
c := &http.Client{
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 10 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 4 * time.Second,
ResponseHeaderTimeout: 3 * time.Second,
},
Timeout: 1 * time.Minute,
}
return c
}