-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcomponent.go
62 lines (52 loc) · 1.58 KB
/
component.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
package edge_apis
import (
"crypto/x509"
"github.com/openziti/edge-api/rest_util"
"net/http"
"net/http/cookiejar"
"net/url"
"time"
)
// Components provides the basic shared lower level pieces used to assemble go-swagger/openapi clients. These
// components are interconnected and have references to each other. This struct is used to set, move, and manage
// them as a set.
type Components struct {
HttpClient *http.Client
HttpTransport *http.Transport
CaPool *x509.CertPool
}
type ComponentsConfig struct {
Proxy func(*http.Request) (*url.URL, error)
}
// NewComponents assembles a new set of components with reasonable production defaults.
func NewComponents() *Components {
return NewComponentsWithConfig(&ComponentsConfig{
Proxy: http.ProxyFromEnvironment,
})
}
// NewComponentsWithConfig assembles a new set of components with reasonable production defaults.
func NewComponentsWithConfig(cfg *ComponentsConfig) *Components {
tlsClientConfig, _ := rest_util.NewTlsConfig()
httpTransport := &http.Transport{
TLSClientConfig: tlsClientConfig,
ForceAttemptHTTP2: true,
MaxIdleConns: 10,
IdleConnTimeout: 10 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
if cfg != nil && cfg.Proxy != nil {
httpTransport.Proxy = cfg.Proxy
}
jar, _ := cookiejar.New(nil)
httpClient := &http.Client{
Transport: httpTransport,
CheckRedirect: nil,
Jar: jar,
Timeout: 10 * time.Second,
}
return &Components{
HttpClient: httpClient,
HttpTransport: httpTransport,
}
}