act_runner/client/options.go
Bo-Yi.Wu e08495af09 chore(runner): remove client secret and add UUID in runner
Signed-off-by: Bo-Yi.Wu <appleboy.tw@gmail.com>
2022-11-24 15:37:38 +08:00

79 lines
1.6 KiB
Go

package client
import (
"context"
"net/http"
"gitea.com/gitea/act_runner/core"
"github.com/bufbuild/connect-go"
)
type config struct {
httpClient *http.Client
skipVerify bool
opts []connect.ClientOption
}
// An Option configures a mutex.
type Option interface {
apply(*config)
}
// OptionFunc is a function that configure a value.
type OptionFunc func(*config)
// Apply calls f(option)
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...)
})
}
// WithGRPC configures clients to use the HTTP/2 gRPC protocol.
func WithGRPC(c bool) Option {
return OptionFunc(func(cfg *config) {
if !c {
return
}
cfg.opts = append(cfg.opts, connect.WithGRPC())
})
}
// WithGRPCWeb configures clients to use the gRPC-Web protocol.
func WithGRPCWeb(c bool) Option {
return OptionFunc(func(cfg *config) {
if !c {
return
}
cfg.opts = append(cfg.opts, connect.WithGRPCWeb())
})
}
// 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)
}
})),
)
})
}