mirror of
https://gitea.com/gitea/act_runner.git
synced 2025-06-12 17:47:14 +02:00
chore(client): support gRPC and gRPC web protocol
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
@ -15,13 +15,19 @@ import (
|
||||
)
|
||||
|
||||
// New returns a new runner client.
|
||||
func New(endpoint, secret string, skipverify bool) *HTTPClient {
|
||||
func New(endpoint, secret string, skipverify bool, opts ...Option) *HTTPClient {
|
||||
client := &HTTPClient{
|
||||
Endpoint: endpoint,
|
||||
Secret: secret,
|
||||
SkipVerify: skipverify,
|
||||
}
|
||||
|
||||
// Loop through each option
|
||||
for _, opt := range opts {
|
||||
// Call the option giving the instantiated
|
||||
opt.Apply(client)
|
||||
}
|
||||
|
||||
client.Client = &http.Client{
|
||||
Timeout: 5 * time.Second,
|
||||
CheckRedirect: func(*http.Request, []*http.Request) error {
|
||||
@ -57,6 +63,8 @@ type HTTPClient struct {
|
||||
Endpoint string
|
||||
Secret string
|
||||
SkipVerify bool
|
||||
|
||||
opts []connect.ClientOption
|
||||
}
|
||||
|
||||
// Ping sends a ping message to the server to test connectivity.
|
||||
@ -64,6 +72,7 @@ func (p *HTTPClient) Ping(ctx context.Context, machine string) error {
|
||||
client := v1connect.NewPingServiceClient(
|
||||
p.Client,
|
||||
p.Endpoint,
|
||||
p.opts...,
|
||||
)
|
||||
req := connect.NewRequest(&v1.PingRequest{
|
||||
Data: machine,
|
||||
|
36
client/options.go
Normal file
36
client/options.go
Normal file
@ -0,0 +1,36 @@
|
||||
package client
|
||||
|
||||
import "github.com/bufbuild/connect-go"
|
||||
|
||||
// An Option configures a mutex.
|
||||
type Option interface {
|
||||
Apply(*HTTPClient)
|
||||
}
|
||||
|
||||
// OptionFunc is a function that configure a value.
|
||||
type OptionFunc func(*HTTPClient)
|
||||
|
||||
// Apply calls f(option)
|
||||
func (f OptionFunc) Apply(cli *HTTPClient) {
|
||||
f(cli)
|
||||
}
|
||||
|
||||
// WithGRPC configures clients to use the HTTP/2 gRPC protocol.
|
||||
func WithGRPC(c bool) Option {
|
||||
return OptionFunc(func(cli *HTTPClient) {
|
||||
if !c {
|
||||
return
|
||||
}
|
||||
cli.opts = append(cli.opts, connect.WithGRPC())
|
||||
})
|
||||
}
|
||||
|
||||
// WithGRPCWeb configures clients to use the gRPC-Web protocol.
|
||||
func WithGRPCWeb(c bool) Option {
|
||||
return OptionFunc(func(cli *HTTPClient) {
|
||||
if !c {
|
||||
return
|
||||
}
|
||||
cli.opts = append(cli.opts, connect.WithGRPCWeb())
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user