forked from rai-project/registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
120 lines (99 loc) · 2.04 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package registry
import (
"context"
"crypto/tls"
"crypto/x509"
"time"
"github.com/c3sr/libkv/store"
)
type Options struct {
Provider store.Backend
Username string
Password string
Endpoints []string
Timeout time.Duration
Bucket string
TLSConfig *tls.Config
PersistConnection bool
Context context.Context
}
type Option func(*Options)
type RegisterOptions struct {
TTL time.Duration
Context context.Context
}
type RegisterOption func(*RegisterOptions)
func Bucket(s string) Option {
return func(o *Options) {
o.Bucket = s
}
}
func Provider(s string) Option {
return func(o *Options) {
o.Provider = getProvider(s)
}
}
func Username(s string) Option {
return func(o *Options) {
o.Username = s
}
}
func Password(s string) Option {
return func(o *Options) {
o.Password = s
}
}
func UsernamePassword(u string, p string) Option {
return func(o *Options) {
o.Username = u
o.Password = p
}
}
func Endpoint(addr string) Option {
return Endpoints([]string{addr})
}
func Endpoints(addrs []string) Option {
return func(o *Options) {
o.Endpoints = cleanupEndpoints(addrs)
}
}
func TLSCertificate(s string) Option {
return func(o *Options) {
var roots *x509.CertPool
if o.TLSConfig != nil && o.TLSConfig.RootCAs != nil {
roots = o.TLSConfig.RootCAs
} else {
roots = x509.NewCertPool()
}
cert := []byte(decrypt(s))
roots.AppendCertsFromPEM(cert)
o.TLSConfig = &tls.Config{
RootCAs: roots,
}
}
}
func TLSConfig(t *tls.Config) Option {
return func(o *Options) {
o.TLSConfig = t
}
}
func Timeout(t time.Duration) Option {
return func(o *Options) {
o.Timeout = t
}
}
func PersistConnection(b bool) Option {
return func(o *Options) {
o.PersistConnection = b
}
}
func HeaderTimeoutPerRequest(t time.Duration) Option {
return func(o *Options) {
o.Context = context.WithValue(o.Context, "HeaderTimeoutPerRequest", t)
}
}
func AutoSync(v bool) Option {
return func(o *Options) {
o.Context = context.WithValue(o.Context, "AutoSync", v)
}
}