2022-08-14 03:59:09 +01:00
|
|
|
package client
|
|
|
|
|
2022-09-25 11:54:00 +01:00
|
|
|
import (
|
2022-10-15 13:03:33 +01:00
|
|
|
"context"
|
2022-09-25 11:54:00 +01:00
|
|
|
"net/http"
|
|
|
|
|
2022-10-15 13:03:33 +01:00
|
|
|
"gitea.com/gitea/act_runner/core"
|
|
|
|
|
2022-09-25 11:54:00 +01:00
|
|
|
"github.com/bufbuild/connect-go"
|
|
|
|
)
|
|
|
|
|
|
|
|
type config struct {
|
|
|
|
httpClient *http.Client
|
|
|
|
skipVerify bool
|
|
|
|
opts []connect.ClientOption
|
|
|
|
}
|
2022-08-14 03:59:09 +01:00
|
|
|
|
|
|
|
// An Option configures a mutex.
|
|
|
|
type Option interface {
|
2022-09-25 11:54:00 +01:00
|
|
|
apply(*config)
|
2022-08-14 03:59:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// OptionFunc is a function that configure a value.
|
2022-09-25 11:54:00 +01:00
|
|
|
type OptionFunc func(*config)
|
2022-08-14 03:59:09 +01:00
|
|
|
|
|
|
|
// Apply calls f(option)
|
2022-09-25 11:54:00 +01:00
|
|
|
func (f OptionFunc) apply(cfg *config) {
|
|
|
|
f(cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithSkipVerify(c bool) Option {
|
|
|
|
return OptionFunc(func(cfg *config) {
|
|
|
|
cfg.skipVerify = c
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithClientOptions(opts ...connect.ClientOption) Option {
|
|
|
|
return OptionFunc(func(cfg *config) {
|
|
|
|
cfg.opts = append(cfg.opts, opts...)
|
|
|
|
})
|
2022-08-14 03:59:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithGRPC configures clients to use the HTTP/2 gRPC protocol.
|
|
|
|
func WithGRPC(c bool) Option {
|
2022-09-25 11:54:00 +01:00
|
|
|
return OptionFunc(func(cfg *config) {
|
2022-08-14 03:59:09 +01:00
|
|
|
if !c {
|
|
|
|
return
|
|
|
|
}
|
2022-09-25 11:54:00 +01:00
|
|
|
cfg.opts = append(cfg.opts, connect.WithGRPC())
|
2022-08-14 03:59:09 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithGRPCWeb configures clients to use the gRPC-Web protocol.
|
|
|
|
func WithGRPCWeb(c bool) Option {
|
2022-09-25 11:54:00 +01:00
|
|
|
return OptionFunc(func(cfg *config) {
|
2022-08-14 03:59:09 +01:00
|
|
|
if !c {
|
|
|
|
return
|
|
|
|
}
|
2022-09-25 11:54:00 +01:00
|
|
|
cfg.opts = append(cfg.opts, connect.WithGRPCWeb())
|
2022-08-14 03:59:09 +01:00
|
|
|
})
|
|
|
|
}
|
2022-10-15 13:03:33 +01:00
|
|
|
|
|
|
|
// WithUUIDHeader add runner uuid in header
|
|
|
|
func WithUUIDHeader(uuid string) Option {
|
|
|
|
return OptionFunc(func(cfg *config) {
|
|
|
|
if uuid == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cfg.opts = append(
|
|
|
|
cfg.opts,
|
|
|
|
connect.WithInterceptors(connect.UnaryInterceptorFunc(func(next connect.UnaryFunc) connect.UnaryFunc {
|
|
|
|
return func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {
|
|
|
|
req.Header().Set(core.UUIDHeader, uuid)
|
|
|
|
return next(ctx, req)
|
|
|
|
}
|
|
|
|
})),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|