-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
218 lines (175 loc) · 5.34 KB
/
config.go
File metadata and controls
218 lines (175 loc) · 5.34 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package ipgeolocation
import (
"encoding/json"
"net/url"
"strings"
"time"
)
const (
DefaultBaseURL = "https://api.ipgeolocation.io"
DefaultMaxResponseBodyBytes = 134217728
)
var (
DefaultConnectTimeout = 10 * time.Second
DefaultReadTimeout = 30 * time.Second
)
type Config struct {
APIKey string `json:"-"`
RequestOrigin string `json:"requestOrigin,omitempty"`
BaseURL string `json:"baseUrl,omitempty"`
ConnectTimeout time.Duration `json:"connectTimeout,omitempty"`
ReadTimeout time.Duration `json:"readTimeout,omitempty"`
}
type normalizedConfig struct {
APIKey string
RequestOrigin string
BaseURL string
ConnectTimeout time.Duration
ReadTimeout time.Duration
}
func DefaultConfig() *Config {
return &Config{
BaseURL: DefaultBaseURL,
ConnectTimeout: DefaultConnectTimeout,
ReadTimeout: DefaultReadTimeout,
}
}
func (c *Config) Validate() error {
_, err := normalizeConfig(c)
return err
}
func (c *Config) SafeMap() map[string]interface{} {
cfg := c
if cfg == nil {
cfg = DefaultConfig()
}
result := map[string]interface{}{
"apiKey": nil,
"requestOrigin": strings.TrimSpace(cfg.RequestOrigin),
"baseUrl": strings.TrimSpace(cfg.BaseURL),
"connectTimeout": cfg.ConnectTimeout.Seconds(),
"readTimeout": cfg.ReadTimeout.Seconds(),
}
if strings.TrimSpace(cfg.APIKey) != "" {
result["apiKey"] = "[REDACTED]"
}
if normalized, err := normalizeConfig(cfg); err == nil {
result["requestOrigin"] = emptyStringAsNil(normalized.RequestOrigin)
result["baseUrl"] = normalized.BaseURL
result["connectTimeout"] = normalized.ConnectTimeout.Seconds()
result["readTimeout"] = normalized.ReadTimeout.Seconds()
}
return result
}
func (c *Config) MarshalJSON() ([]byte, error) {
return json.Marshal(c.SafeMap())
}
func normalizeConfig(config *Config) (normalizedConfig, error) {
if config == nil {
config = DefaultConfig()
}
baseURL := config.BaseURL
if strings.TrimSpace(baseURL) == "" {
baseURL = DefaultBaseURL
}
connectTimeout := config.ConnectTimeout
if connectTimeout == 0 {
connectTimeout = DefaultConnectTimeout
}
readTimeout := config.ReadTimeout
if readTimeout == 0 {
readTimeout = DefaultReadTimeout
}
apiKey, err := normalizeAPIKey(config.APIKey)
if err != nil {
return normalizedConfig{}, err
}
requestOrigin, err := normalizeRequestOrigin(config.RequestOrigin)
if err != nil {
return normalizedConfig{}, err
}
normalizedBaseURL, err := normalizeBaseURL(baseURL)
if err != nil {
return normalizedConfig{}, err
}
normalizedConnectTimeout, err := normalizeTimeout(connectTimeout, "connectTimeout")
if err != nil {
return normalizedConfig{}, err
}
normalizedReadTimeout, err := normalizeTimeout(readTimeout, "readTimeout")
if err != nil {
return normalizedConfig{}, err
}
return normalizedConfig{
APIKey: apiKey,
RequestOrigin: requestOrigin,
BaseURL: normalizedBaseURL,
ConnectTimeout: normalizedConnectTimeout,
ReadTimeout: normalizedReadTimeout,
}, nil
}
func normalizeAPIKey(value string) (string, error) {
normalized := strings.TrimSpace(value)
if value != "" && normalized == "" {
return "", &ValidationError{Message: "apiKey must not be blank"}
}
if normalized == "" {
return "", nil
}
return normalized, nil
}
func normalizeRequestOrigin(value string) (string, error) {
normalized := strings.TrimSpace(value)
if value != "" && normalized == "" {
return "", &ValidationError{Message: "requestOrigin must not be blank"}
}
if normalized == "" {
return "", nil
}
if strings.ContainsAny(normalized, "\r\n") {
return "", &ValidationError{Message: "requestOrigin must not contain CR or LF"}
}
parsed, err := url.Parse(normalized)
if err != nil || !parsed.IsAbs() || (parsed.Scheme != "http" && parsed.Scheme != "https") {
return "", &ValidationError{Message: "requestOrigin must be an absolute http or https origin"}
}
if parsed.User != nil {
return "", &ValidationError{Message: "requestOrigin must not include userinfo"}
}
if parsed.RawQuery != "" || parsed.Fragment != "" {
return "", &ValidationError{Message: "requestOrigin must not include query or fragment"}
}
if parsed.Path != "" && parsed.Path != "/" {
return "", &ValidationError{Message: "requestOrigin must not include a path"}
}
return parsed.Scheme + "://" + parsed.Host, nil
}
func normalizeBaseURL(value string) (string, error) {
normalized := strings.TrimRight(strings.TrimSpace(value), "/")
if normalized == "" {
return "", &ValidationError{Message: "baseUrl must not be blank"}
}
parsed, err := url.Parse(normalized)
if err != nil || !parsed.IsAbs() || (parsed.Scheme != "http" && parsed.Scheme != "https") {
return "", &ValidationError{Message: "baseUrl must be an absolute http or https URL"}
}
if parsed.User != nil {
return "", &ValidationError{Message: "baseUrl must not include userinfo"}
}
if parsed.RawQuery != "" || parsed.Fragment != "" {
return "", &ValidationError{Message: "baseUrl must not include query or fragment"}
}
return normalized, nil
}
func normalizeTimeout(value time.Duration, fieldName string) (time.Duration, error) {
if value <= 0 {
return 0, &ValidationError{Message: fieldName + " must be greater than zero"}
}
return value, nil
}
func emptyStringAsNil(value string) interface{} {
if value == "" {
return nil
}
return value
}