first compiling version
This commit is contained in:
parent
60fb40d61a
commit
3d54199faa
27 changed files with 908 additions and 243 deletions
|
|
@ -5,7 +5,6 @@ package client
|
|||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
|
|
@ -13,19 +12,15 @@ import (
|
|||
"net/http"
|
||||
"net/http/httputil"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
endpoint string
|
||||
username string
|
||||
password string
|
||||
auth bool
|
||||
httpClient *http.Client
|
||||
debugWriter io.Writer
|
||||
insecure bool
|
||||
//insecure bool
|
||||
}
|
||||
|
||||
var (
|
||||
|
|
@ -52,36 +47,21 @@ func WithHTTPClient(httpClient *http.Client) ClientOption {
|
|||
}
|
||||
}
|
||||
|
||||
func WithInsecureClient(insecure bool) ClientOption {
|
||||
return func(client *Client) {
|
||||
client.insecure = insecure
|
||||
}
|
||||
}
|
||||
|
||||
func WithCredentials(username, password string) ClientOption {
|
||||
return func(client *Client) {
|
||||
client.username = username
|
||||
client.password = password
|
||||
client.auth = true
|
||||
}
|
||||
}
|
||||
//func WithInsecureClient(insecure bool) ClientOption {
|
||||
// return func(client *Client) {
|
||||
// client.insecure = insecure
|
||||
// }
|
||||
//}
|
||||
|
||||
var (
|
||||
ErrMissingEndpointEnv = errors.New("missing environment variable HAPROXY_LB_ENDPOINT")
|
||||
ErrMissingUserEnv = errors.New("missing environment variable HAPROXY_LB_USER")
|
||||
ErrMissingPasswordEnv = errors.New("missing environment variable HAPROXY_LB_PASSWORD")
|
||||
ErrMissingEndpointEnv = errors.New("missing environment variable KEYCTL_ENDPOINT")
|
||||
|
||||
ErrMissingEndpoint = errors.New("missing Endpoint in config")
|
||||
ErrMissingUser = errors.New("missing User in config")
|
||||
ErrMissingPassword = errors.New("missing Password in config")
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Endpoint string `json:"endpoint"`
|
||||
NoAuth bool `json:"no_auth"`
|
||||
User string `json:"user"`
|
||||
Password string `json:"password"`
|
||||
Insecure bool `json:"insecure"`
|
||||
//Insecure bool `json:"insecure"`
|
||||
}
|
||||
|
||||
func ClientOptionsFromConfig(cfg Config) (options []ClientOption, err error) {
|
||||
|
|
@ -91,53 +71,23 @@ func ClientOptionsFromConfig(cfg Config) (options []ClientOption, err error) {
|
|||
}
|
||||
options = []ClientOption{
|
||||
WithEndpoint(cfg.Endpoint),
|
||||
WithInsecureClient(cfg.Insecure),
|
||||
//WithInsecureClient(cfg.Insecure),
|
||||
}
|
||||
if cfg.NoAuth {
|
||||
return
|
||||
}
|
||||
if cfg.User == "" {
|
||||
err = ErrMissingUser
|
||||
return
|
||||
}
|
||||
if cfg.Password == "" {
|
||||
err = ErrMissingPassword
|
||||
return
|
||||
}
|
||||
options = append(options, WithCredentials(cfg.User, cfg.Password))
|
||||
return
|
||||
}
|
||||
|
||||
func ClientOptionsFromEnv() (options []ClientOption, err error) {
|
||||
endpoint := os.Getenv("HAPROXY_LB_ENDPOINT")
|
||||
endpoint := os.Getenv("KEYCTL_ENDPOINT")
|
||||
if endpoint == "" {
|
||||
err = ErrMissingEndpointEnv
|
||||
return
|
||||
}
|
||||
insecure, _ := strconv.ParseBool(os.Getenv("HAPROXY_LB_INSECURE"))
|
||||
//insecure, _ := strconv.ParseBool(os.Getenv("HAPROXY_LB_INSECURE"))
|
||||
|
||||
options = []ClientOption{
|
||||
WithEndpoint(endpoint),
|
||||
WithInsecureClient(insecure),
|
||||
//WithInsecureClient(insecure),
|
||||
}
|
||||
noAuth, _ := strconv.ParseBool(os.Getenv("HAPROXY_LB_NO_AUTH"))
|
||||
if noAuth {
|
||||
return
|
||||
}
|
||||
|
||||
user := os.Getenv("HAPROXY_LB_USER")
|
||||
if user == "" {
|
||||
err = ErrMissingUserEnv
|
||||
return
|
||||
}
|
||||
|
||||
pass := os.Getenv("HAPROXY_LB_PASSWORD")
|
||||
if pass == "" {
|
||||
err = ErrMissingPasswordEnv
|
||||
return
|
||||
}
|
||||
|
||||
options = append(options, WithCredentials(user, pass))
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -149,20 +99,25 @@ func NewClient(options ...ClientOption) *Client {
|
|||
}
|
||||
|
||||
if client.httpClient == nil {
|
||||
client.httpClient = &http.Client{Transport: &http.Transport{
|
||||
DialContext: (&net.Dialer{
|
||||
Timeout: 30 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
}).DialContext,
|
||||
ForceAttemptHTTP2: true,
|
||||
MaxIdleConns: 100,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
TLSHandshakeTimeout: 10 * time.Second,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: client.insecure,
|
||||
client.httpClient = &http.Client{
|
||||
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
},
|
||||
}}
|
||||
Transport: &http.Transport{
|
||||
DialContext: (&net.Dialer{
|
||||
Timeout: 30 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
}).DialContext,
|
||||
//ForceAttemptHTTP2: true,
|
||||
MaxIdleConns: 100,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
//TLSHandshakeTimeout: 10 * time.Second,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
//TLSClientConfig: &tls.Config{
|
||||
// InsecureSkipVerify: client.insecure,
|
||||
//},
|
||||
},
|
||||
}
|
||||
}
|
||||
return client
|
||||
}
|
||||
|
|
@ -173,14 +128,11 @@ func (c *Client) NewRequest(ctx context.Context, method, path string, body io.Re
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if c.auth {
|
||||
req.SetBasicAuth(c.username, c.password)
|
||||
}
|
||||
req = req.WithContext(ctx)
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func (c *Client) Do(r *http.Request, v interface{}) (resp *http.Response, err error) {
|
||||
func (c *Client) Do(r *http.Request, v any) (resp *http.Response, err error) {
|
||||
if c.debugWriter != nil {
|
||||
dumpReq, err := httputil.DumpRequestOut(r, true)
|
||||
if err != nil {
|
||||
|
|
@ -211,3 +163,19 @@ func (c *Client) Do(r *http.Request, v interface{}) (resp *http.Response, err er
|
|||
err = decodeResp(resp, v)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Client) FollowRedirect(r *http.Request, v any, delay time.Duration) (resp *http.Response, err error) {
|
||||
rr := r
|
||||
for {
|
||||
resp, err = c.Do(rr, v)
|
||||
if !errors.Is(ErrRedirect, err) {
|
||||
return
|
||||
}
|
||||
rdr := err.(Redirect)
|
||||
rr, err = c.NewRequest(r.Context(), "GET", rdr.Location, nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
time.Sleep(delay)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,8 +8,8 @@ import (
|
|||
)
|
||||
|
||||
type Error struct {
|
||||
Err string `json:"error"`
|
||||
Status int `json:"status"`
|
||||
Err string `json:"error"`
|
||||
Code int `json:"code"`
|
||||
}
|
||||
|
||||
func (e Error) Error() string {
|
||||
|
|
@ -20,7 +20,18 @@ func (e Error) Is(target error) bool {
|
|||
return e.Err == target.Error()
|
||||
}
|
||||
|
||||
func decodeResp(resp *http.Response, v interface{}) error {
|
||||
type Redirect struct {
|
||||
Location string
|
||||
Code int
|
||||
}
|
||||
|
||||
func (r Redirect) Error() string {
|
||||
return "redirect: " + r.Location
|
||||
}
|
||||
|
||||
var ErrRedirect = Redirect{}
|
||||
|
||||
func decodeResp(resp *http.Response, v any) error {
|
||||
if resp.Body == nil {
|
||||
return nil
|
||||
}
|
||||
|
|
@ -31,12 +42,16 @@ func decodeResp(resp *http.Response, v interface{}) error {
|
|||
}
|
||||
return json.NewDecoder(resp.Body).Decode(&v)
|
||||
}
|
||||
if resp.StatusCode >= 300 && resp.StatusCode < 400 {
|
||||
loc := resp.Header.Get("Location")
|
||||
return Redirect{loc, resp.StatusCode}
|
||||
}
|
||||
var e Error
|
||||
err := json.NewDecoder(resp.Body).Decode(&e)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if resp.StatusCode >= 400 && resp.StatusCode <= 599 {
|
||||
if resp.StatusCode >= 400 && resp.StatusCode < 600 {
|
||||
return e
|
||||
}
|
||||
return nil
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue