refactor: more code cleanup and reorganization

This commit is contained in:
David Allen 2024-11-21 14:16:03 -07:00
parent 72dd8416aa
commit 69aac3c929
Signed by: towk
GPG key ID: 793B2924A49B3A3F
4 changed files with 138 additions and 140 deletions

66
pkg/client/client.go Normal file
View file

@ -0,0 +1,66 @@
package client
import (
"crypto/tls"
"crypto/x509"
"net"
"net/http"
"os"
"time"
)
type Option func(*Params)
type Params struct {
Host string `yaml:"host"`
AccessToken string `yaml:"access-token"`
Transport *http.Transport
}
func ToParams(opts ...Option) *Params {
params := &Params{}
for _, opt := range opts {
opt(params)
}
return params
}
func WithHost(host string) Option {
return func(c *Params) {
c.Host = host
}
}
func WithAccessToken(token string) Option {
return func(c *Params) {
c.AccessToken = token
}
}
func WithCertPool(certPool *x509.CertPool) Option {
return func(c *Params) {
c.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: certPool,
InsecureSkipVerify: true,
},
DisableKeepAlives: true,
Dial: (&net.Dialer{
Timeout: 120 * time.Second,
KeepAlive: 120 * time.Second,
}).Dial,
TLSHandshakeTimeout: 120 * time.Second,
ResponseHeaderTimeout: 120 * time.Second,
}
}
}
// FIXME: Need to check for errors when reading from a file
func WithCertPoolFile(certPath string) Option {
if certPath == "" {
return func(sc *Params) {}
}
cacert, _ := os.ReadFile(certPath)
certPool := x509.NewCertPool()
certPool.AppendCertsFromPEM(cacert)
return WithCertPool(certPool)
}