2022-08-14 10:59:09 +08:00
|
|
|
package client
|
|
|
|
|
2022-09-25 18:54:00 +08:00
|
|
|
import (
|
2022-10-15 20:03:33 +08:00
|
|
|
"context"
|
2022-09-25 18:54:00 +08:00
|
|
|
"net/http"
|
|
|
|
|
2022-10-15 20:03:33 +08:00
|
|
|
"gitea.com/gitea/act_runner/core"
|
|
|
|
|
2022-09-25 18:54:00 +08:00
|
|
|
"github.com/bufbuild/connect-go"
|
|
|
|
)
|
|
|
|
|
|
|
|
type config struct {
|
|
|
|
httpClient *http.Client
|
|
|
|
skipVerify bool
|
|
|
|
opts []connect.ClientOption
|
|
|
|
}
|
2022-08-14 10:59:09 +08:00
|
|
|
|
|
|
|
// An Option configures a mutex.
|
|
|
|
type Option interface {
|
2022-09-25 18:54:00 +08:00
|
|
|
apply(*config)
|
2022-08-14 10:59:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// OptionFunc is a function that configure a value.
|
2022-09-25 18:54:00 +08:00
|
|
|
type OptionFunc func(*config)
|
2022-08-14 10:59:09 +08:00
|
|
|
|
|
|
|
// Apply calls f(option)
|
2022-09-25 18:54:00 +08: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 10:59:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithGRPC configures clients to use the HTTP/2 gRPC protocol.
|
|
|
|
func WithGRPC(c bool) Option {
|
2022-09-25 18:54:00 +08:00
|
|
|
return OptionFunc(func(cfg *config) {
|
2022-08-14 10:59:09 +08:00
|
|
|
if !c {
|
|
|
|
return
|
|
|
|
}
|
2022-09-25 18:54:00 +08:00
|
|
|
cfg.opts = append(cfg.opts, connect.WithGRPC())
|
2022-08-14 10:59:09 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithGRPCWeb configures clients to use the gRPC-Web protocol.
|
|
|
|
func WithGRPCWeb(c bool) Option {
|
2022-09-25 18:54:00 +08:00
|
|
|
return OptionFunc(func(cfg *config) {
|
2022-08-14 10:59:09 +08:00
|
|
|
if !c {
|
|
|
|
return
|
|
|
|
}
|
2022-09-25 18:54:00 +08:00
|
|
|
cfg.opts = append(cfg.opts, connect.WithGRPCWeb())
|
2022-08-14 10:59:09 +08:00
|
|
|
})
|
|
|
|
}
|
2022-10-15 20:03:33 +08: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)
|
|
|
|
}
|
|
|
|
})),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
2022-10-28 23:23:19 +08:00
|
|
|
|
|
|
|
// WithTokenHeader add runner token in header
|
|
|
|
func WithTokenHeader(token string) Option {
|
|
|
|
return OptionFunc(func(cfg *config) {
|
|
|
|
if token == "" {
|
|
|
|
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.TokenHeader, token)
|
|
|
|
return next(ctx, req)
|
|
|
|
}
|
|
|
|
})),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|