-
Notifications
You must be signed in to change notification settings - Fork 0
/
kameleoon_client_config.go
150 lines (136 loc) · 4.31 KB
/
kameleoon_client_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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package kameleoon
import (
"time"
"github.com/Kameleoon/client-go/v3/errs"
"github.com/Kameleoon/client-go/v3/logging"
"github.com/cristalhq/aconfig"
"github.com/cristalhq/aconfig/aconfigyaml"
)
const (
DefaultConfigPath = "/etc/kameleoon/client-go.yaml"
DefaultRefreshInterval = time.Hour
DefaultRequestTimeout = 10 * time.Second
DefaultSessionDuration = 30 * time.Minute
DefaultEnvironment = ""
)
type KameleoonClientConfig struct {
defaultsApplied bool
Network NetworkConfig
Logger logging.Logger `yml:"-" yaml:"-"`
ProxyURL string `yml:"proxy_url" yaml:"proxy_url"`
ClientID string `yml:"client_id" yaml:"client_id"`
ClientSecret string `yml:"client_secret" yaml:"client_secret"`
RefreshInterval time.Duration `yml:"refresh_interval" yaml:"refresh_interval" default:"1h"`
DefaultTimeout time.Duration `yml:"default_timeout" yaml:"default_timeout" default:"10s"`
VerboseMode bool `yml:"verbose_mode" yaml:"verbose_mode"`
SessionDuration time.Duration `yml:"session_duration" yaml:"session_duration" default:"30m"`
TopLevelDomain string `yml:"top_level_domain" yaml:"top_level_domain"`
Environment string `yml:"environment" yaml:"environment"`
}
func LoadConfig(path string) (*KameleoonClientConfig, error) {
c := &KameleoonClientConfig{}
return c, c.Load(path)
}
func (c *KameleoonClientConfig) defaults() error {
if c.defaultsApplied {
return nil
}
c.defaultsApplied = true
if len(c.ClientID) == 0 {
return errs.NewConfigCredentialsInvalid("Client ID is not specified")
}
if len(c.ClientSecret) == 0 {
return errs.NewConfigCredentialsInvalid("Client secret is not specified")
}
if c.Logger == nil {
c.defaultLogger()
}
if c.RefreshInterval < time.Minute {
if c.RefreshInterval != 0 {
c.Logger.Printf("Kameleoon SDK: Config update interval must not be less than a minute."+
"Default config update interval (%d minutes) is applied", int(DefaultRefreshInterval.Minutes()))
}
c.RefreshInterval = DefaultRefreshInterval
}
if c.DefaultTimeout <= 0 {
if c.DefaultTimeout != 0 {
c.Logger.Printf("Kameleoon SDK: Default timeout must have positive value."+
"Default default timeout (%d ms) is applied", DefaultRequestTimeout.Milliseconds())
}
c.DefaultTimeout = DefaultRequestTimeout
}
if c.SessionDuration <= 0 {
if c.SessionDuration != 0 {
c.Logger.Printf("Kameleoon SDK: Session duration must have positive value."+
"Default session duration (%d minutes) is applied", int(DefaultSessionDuration.Minutes()))
}
c.SessionDuration = DefaultSessionDuration
}
if len(c.TopLevelDomain) == 0 {
c.Logger.Printf("Kameleoon SDK: Setting top level domain is strictly recommended, " +
"otherwise you may have problems when using subdomains.")
}
if len(c.Environment) == 0 {
c.Environment = DefaultEnvironment
}
return c.Network.defaults()
}
func (c *KameleoonClientConfig) defaultLogger() {
loggerMode := logging.Silent
if c.VerboseMode {
loggerMode = logging.Verbose
}
c.Logger = logging.NewLogger(loggerMode, logging.DefaultLogger)
}
func (c *KameleoonClientConfig) Load(path string) error {
if len(path) == 0 {
path = DefaultConfigPath
}
if err := c.loadFile(path); err != nil {
return err
}
return c.defaults()
}
func (c *KameleoonClientConfig) loadFile(configPath string) error {
yml := aconfigyaml.New()
loader := aconfig.LoaderFor(c, aconfig.Config{
SkipFlags: true,
SkipEnv: true,
FailOnFileNotFound: true,
AllowUnknownFields: true,
Files: []string{configPath},
FileDecoders: map[string]aconfig.FileDecoder{
".yaml": yml,
".yml": yml,
},
})
return loader.Load()
}
const (
DefaultReadTimeout = 5 * time.Second
DefaultWriteTimeout = 5 * time.Second
DefaultDoTimeout = 10 * time.Second
DefaultMaxConnsPerHost = 10000
)
type NetworkConfig struct {
ProxyURL string
DoTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
MaxConnsPerHost int
}
func (c *NetworkConfig) defaults() error {
if c.ReadTimeout == 0 {
c.ReadTimeout = DefaultReadTimeout
}
if c.WriteTimeout == 0 {
c.WriteTimeout = DefaultWriteTimeout
}
if c.DoTimeout == 0 {
c.DoTimeout = DefaultDoTimeout
}
if c.MaxConnsPerHost == 0 {
c.MaxConnsPerHost = DefaultMaxConnsPerHost
}
return nil
}