act_runner/client/http.go

84 lines
1.7 KiB
Go
Raw Normal View History

package client
import (
"crypto/tls"
"net"
"net/http"
2022-11-22 21:42:02 -06:00
"strings"
"time"
"code.gitea.io/bots-proto-go/ping/v1/pingv1connect"
"code.gitea.io/bots-proto-go/runner/v1/runnerv1connect"
"golang.org/x/net/http2"
)
// New returns a new runner client.
func New(endpoint string, opts ...Option) *HTTPClient {
cfg := &config{}
// Loop through each option
for _, opt := range opts {
// Call the option giving the instantiated
opt.apply(cfg)
}
if cfg.httpClient == nil {
cfg.httpClient = &http.Client{
Timeout: 1 * time.Minute,
CheckRedirect: func(*http.Request, []*http.Request) error {
return http.ErrUseLastResponse
},
Transport: &http2.Transport{
AllowHTTP: true,
DialTLS: func(netw, addr string, cfg *tls.Config) (net.Conn, error) {
return net.Dial(netw, addr)
},
},
}
}
if cfg.skipVerify {
cfg.httpClient = &http.Client{
CheckRedirect: func(*http.Request, []*http.Request) error {
return http.ErrUseLastResponse
},
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
}
2022-11-22 21:42:02 -06:00
baseURL := strings.TrimRight(endpoint, "/") + "/api/bots"
return &HTTPClient{
PingServiceClient: pingv1connect.NewPingServiceClient(
cfg.httpClient,
2022-11-22 21:42:02 -06:00
baseURL,
cfg.opts...,
),
RunnerServiceClient: runnerv1connect.NewRunnerServiceClient(
cfg.httpClient,
2022-11-22 21:42:02 -06:00
baseURL,
cfg.opts...,
),
endpoint: endpoint,
}
}
func (c *HTTPClient) Address() string {
return c.endpoint
}
var _ Client = (*HTTPClient)(nil)
// An HTTPClient manages communication with the runner API.
type HTTPClient struct {
pingv1connect.PingServiceClient
runnerv1connect.RunnerServiceClient
endpoint string
}