-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
68 lines (55 loc) · 1.34 KB
/
config.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
package connector
import (
"fmt"
"net/http"
"net/url"
"time"
"gitlab.com/distributed_lab/figure/v3"
"gitlab.com/distributed_lab/kit/comfig"
"gitlab.com/distributed_lab/kit/kv"
"gitlab.com/distributed_lab/logan/v3"
)
const defaultTimeout = 10 * time.Second
type Pointer interface {
Points() *Client
}
type points struct {
once comfig.Once
getter kv.Getter
}
func NewPointer(getter kv.Getter) Pointer {
return &points{getter: getter}
}
func (p *points) Points() *Client {
return p.once.Do(func() any {
var disabledConfig struct {
Disabled bool `fig:"disabled"`
}
err := figure.Out(&disabledConfig).
From(kv.MustGetStringMap(p.getter, "points")).
Please()
if err != nil {
panic(fmt.Errorf("failed to figure out disabled for points: %s", err))
}
if disabledConfig.Disabled {
return &Client{disabled: true, log: logan.New()}
}
var cfg struct {
Addr *url.URL `fig:"addr,required"`
RequestTimeout time.Duration `fig:"request_timeout"`
}
err = figure.Out(&cfg).
From(kv.MustGetStringMap(p.getter, "points")).
Please()
if err != nil {
panic(fmt.Errorf("failed to figure out points: %s", err))
}
if cfg.RequestTimeout == 0 {
cfg.RequestTimeout = defaultTimeout
}
return NewClient(&client{
base: cfg.Addr,
http: &http.Client{Timeout: cfg.RequestTimeout},
})
}).(*Client)
}