-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathconfig.go
More file actions
327 lines (274 loc) · 11.2 KB
/
config.go
File metadata and controls
327 lines (274 loc) · 11.2 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
Copyright © 2024 Acronis International GmbH.
Released under MIT license.
*/
package httpserver
import (
"fmt"
"time"
"github.com/acronis/go-appkit/config"
)
const cfgDefaultKeyPrefix = "server"
const (
cfgKeyServerAddress = "address"
cfgKeyServerUnixSocketPath = "unixSocketPath"
cfgKeyServerTLSCert = "tls.cert"
cfgKeyServerTLSKey = "tls.key"
cfgKeyServerTLSEnabled = "tls.enabled"
cfgKeyServerTimeoutsWrite = "timeouts.write"
cfgKeyServerTimeoutsRead = "timeouts.read"
cfgKeyServerTimeoutsReadHeader = "timeouts.readHeader"
cfgKeyServerTimeoutsIdle = "timeouts.idle"
cfgKeyServerTimeoutsShutdown = "timeouts.shutdown"
cfgKeyServerLimitsMaxRequests = "limits.maxRequests"
cfgKeyServerLimitsMaxBodySize = "limits.maxBodySize"
cfgKeyServerLogRequestStart = "log.requestStart"
cfgKeyServerLogRequestHeaders = "log.requestHeaders"
cfgKeyServerLogExcludedEndpoints = "log.excludedEndpoints"
cfgKeyServerLogSecretQueryParams = "log.secretQueryParams" // nolint:gosec // false positive
cfgKeyServerLogAddRequestInfo = "log.addRequestInfo"
cfgKeyServerLogSlowRequestThreshold = "log.slowRequestThreshold"
cfgKeyServerLogTimeSlotsThreshold = "log.timeSlotsThreshold"
)
const (
defaultServerAddress = ":8080"
defaultServerTimeoutsWrite = time.Minute
defaultServerTimeoutsRead = time.Second * 15
defaultServerTimeoutsReadHeader = time.Second * 10
defaultServerTimeoutsIdle = time.Minute
defaultServerTimeoutsShutdown = time.Second * 5
defaultSlowRequestThreshold = time.Second
defaultServerLimitsMaxRequests = 5000
)
// Config represents a set of configuration parameters for HTTPServer.
// Configuration can be loaded in different formats (YAML, JSON) using config.Loader, viper,
// or with json.Unmarshal/yaml.Unmarshal functions directly.
type Config struct {
Address string `mapstructure:"address" yaml:"address" json:"address"`
UnixSocketPath string `mapstructure:"unixSocketPath" yaml:"unixSocketPath" json:"unixSocketPath"`
Timeouts TimeoutsConfig `mapstructure:"timeouts" yaml:"timeouts" json:"timeouts"`
Limits LimitsConfig `mapstructure:"limits" yaml:"limits" json:"limits"`
Log LogConfig `mapstructure:"log" yaml:"log" json:"log"`
TLS TLSConfig `mapstructure:"tls" yaml:"tls" json:"tls"`
keyPrefix string
}
var _ config.Config = (*Config)(nil)
var _ config.KeyPrefixProvider = (*Config)(nil)
// ConfigOption is a type for functional options for the Config.
type ConfigOption func(*configOptions)
type configOptions struct {
keyPrefix string
}
// WithKeyPrefix returns a ConfigOption that sets a key prefix for parsing configuration parameters.
// This prefix will be used by config.Loader.
func WithKeyPrefix(keyPrefix string) ConfigOption {
return func(o *configOptions) {
o.keyPrefix = keyPrefix
}
}
// NewConfig creates a new instance of the Config.
func NewConfig(options ...ConfigOption) *Config {
opts := configOptions{keyPrefix: cfgDefaultKeyPrefix} // cfgDefaultKeyPrefix is used here for backward compatibility
for _, opt := range options {
opt(&opts)
}
return &Config{keyPrefix: opts.keyPrefix}
}
// NewConfigWithKeyPrefix creates a new instance of the Config with a key prefix.
// This prefix will be used by config.Loader.
// Deprecated: use NewConfig with WithKeyPrefix instead.
func NewConfigWithKeyPrefix(keyPrefix string) *Config {
if keyPrefix != "" {
keyPrefix += "."
}
keyPrefix += cfgDefaultKeyPrefix // cfgDefaultKeyPrefix is added here for backward compatibility
return &Config{keyPrefix: keyPrefix}
}
// NewDefaultConfig creates a new instance of the Config with default values.
func NewDefaultConfig(options ...ConfigOption) *Config {
opts := configOptions{keyPrefix: cfgDefaultKeyPrefix}
for _, opt := range options {
opt(&opts)
}
return &Config{
keyPrefix: opts.keyPrefix,
Address: defaultServerAddress,
Timeouts: TimeoutsConfig{
Write: config.TimeDuration(defaultServerTimeoutsWrite),
Read: config.TimeDuration(defaultServerTimeoutsRead),
ReadHeader: config.TimeDuration(defaultServerTimeoutsReadHeader),
Idle: config.TimeDuration(defaultServerTimeoutsIdle),
Shutdown: config.TimeDuration(defaultServerTimeoutsShutdown),
},
Limits: LimitsConfig{
MaxRequests: defaultServerLimitsMaxRequests,
},
Log: LogConfig{
SlowRequestThreshold: config.TimeDuration(defaultSlowRequestThreshold),
},
}
}
// KeyPrefix returns a key prefix with which all configuration parameters should be presented.
// Implements config.KeyPrefixProvider interface.
func (c *Config) KeyPrefix() string {
if c.keyPrefix == "" {
return cfgDefaultKeyPrefix
}
return c.keyPrefix
}
// SetProviderDefaults sets default configuration values for HTTPServer in config.DataProvider.
// Implements config.Config interface.
func (c *Config) SetProviderDefaults(dp config.DataProvider) {
dp.SetDefault(cfgKeyServerAddress, defaultServerAddress)
dp.SetDefault(cfgKeyServerTimeoutsWrite, defaultServerTimeoutsWrite)
dp.SetDefault(cfgKeyServerTimeoutsRead, defaultServerTimeoutsRead)
dp.SetDefault(cfgKeyServerTimeoutsReadHeader, defaultServerTimeoutsReadHeader)
dp.SetDefault(cfgKeyServerTimeoutsIdle, defaultServerTimeoutsIdle)
dp.SetDefault(cfgKeyServerTimeoutsShutdown, defaultServerTimeoutsShutdown)
dp.SetDefault(cfgKeyServerLimitsMaxRequests, defaultServerLimitsMaxRequests)
dp.SetDefault(cfgKeyServerLogRequestStart, false)
dp.SetDefault(cfgKeyServerLogAddRequestInfo, false)
dp.SetDefault(cfgKeyServerLogSlowRequestThreshold, defaultSlowRequestThreshold)
}
// TimeoutsConfig represents a set of configuration parameters for HTTPServer relating to timeouts.
type TimeoutsConfig struct {
Write config.TimeDuration `mapstructure:"write" yaml:"write" json:"write"`
Read config.TimeDuration `mapstructure:"read" yaml:"read" json:"read"`
ReadHeader config.TimeDuration `mapstructure:"readHeader" yaml:"readHeader" json:"readHeader"`
Idle config.TimeDuration `mapstructure:"idle" yaml:"idle" json:"idle"`
Shutdown config.TimeDuration `mapstructure:"shutdown" yaml:"shutdown" json:"shutdown"`
}
// Set sets timeout server configuration values from config.DataProvider.
// Implements config.Config interface.
func (t *TimeoutsConfig) Set(dp config.DataProvider) error {
var err error
var dur time.Duration
if dur, err = dp.GetDuration(cfgKeyServerTimeoutsWrite); err != nil {
return err
}
t.Write = config.TimeDuration(dur)
if dur, err = dp.GetDuration(cfgKeyServerTimeoutsRead); err != nil {
return err
}
t.Read = config.TimeDuration(dur)
if dur, err = dp.GetDuration(cfgKeyServerTimeoutsReadHeader); err != nil {
return err
}
t.ReadHeader = config.TimeDuration(dur)
if dur, err = dp.GetDuration(cfgKeyServerTimeoutsIdle); err != nil {
return err
}
t.Idle = config.TimeDuration(dur)
if dur, err = dp.GetDuration(cfgKeyServerTimeoutsShutdown); err != nil {
return err
}
t.Shutdown = config.TimeDuration(dur)
return nil
}
// LimitsConfig represents a set of configuration parameters for HTTPServer relating to limits.
type LimitsConfig struct {
// MaxRequests is the maximum number of requests that can be processed concurrently.
MaxRequests int `mapstructure:"maxRequests" yaml:"maxRequests" json:"maxRequests"`
// MaxBodySizeBytes is the maximum size of the request body in bytes.
MaxBodySizeBytes config.ByteSize `mapstructure:"maxBodySize" yaml:"maxBodySize" json:"maxBodySize"`
}
// Set sets limit server configuration values from config.DataProvider.
func (l *LimitsConfig) Set(dp config.DataProvider) error {
var err error
if l.MaxRequests, err = dp.GetInt(cfgKeyServerLimitsMaxRequests); err != nil {
return err
}
if l.MaxRequests < 0 {
return dp.WrapKeyErr(cfgKeyServerLimitsMaxRequests, fmt.Errorf("cannot be negative"))
}
if l.MaxBodySizeBytes, err = dp.GetSizeInBytes(cfgKeyServerLimitsMaxBodySize); err != nil {
return dp.WrapKeyErr(cfgKeyServerLimitsMaxBodySize, err)
}
return nil
}
// LogConfig represents a set of configuration parameters for HTTPServer relating to logging.
type LogConfig struct {
RequestStart bool `mapstructure:"requestStart" yaml:"requestStart" json:"requestStart"`
RequestHeaders []string `mapstructure:"requestHeaders" yaml:"requestHeaders" json:"requestHeaders"`
ExcludedEndpoints []string `mapstructure:"excludedEndpoints" yaml:"excludedEndpoints" json:"excludedEndpoints"`
SecretQueryParams []string `mapstructure:"secretQueryParams" yaml:"secretQueryParams"`
AddRequestInfoToLogger bool `mapstructure:"addRequestInfo" yaml:"addRequestInfo" json:"addRequestInfo"`
SlowRequestThreshold config.TimeDuration `mapstructure:"slowRequestThreshold" yaml:"slowRequestThreshold" json:"slowRequestThreshold"`
TimeSlotsThreshold config.TimeDuration `mapstructure:"timeSlotsThreshold" yaml:"timeSlotsThreshold" json:"timeSlotsThreshold"`
}
// Set sets log server configuration values from config.DataProvider.
func (l *LogConfig) Set(dp config.DataProvider) error {
var err error
if l.RequestStart, err = dp.GetBool(cfgKeyServerLogRequestStart); err != nil {
return err
}
if l.RequestHeaders, err = dp.GetStringSlice(cfgKeyServerLogRequestHeaders); err != nil {
return err
}
if l.ExcludedEndpoints, err = dp.GetStringSlice(cfgKeyServerLogExcludedEndpoints); err != nil {
return err
}
if l.SecretQueryParams, err = dp.GetStringSlice(cfgKeyServerLogSecretQueryParams); err != nil {
return err
}
if l.AddRequestInfoToLogger, err = dp.GetBool(cfgKeyServerLogAddRequestInfo); err != nil {
return err
}
var dur time.Duration
if dur, err = dp.GetDuration(cfgKeyServerLogSlowRequestThreshold); err != nil {
return err
}
l.SlowRequestThreshold = config.TimeDuration(dur)
if dur, err = dp.GetDuration(cfgKeyServerLogTimeSlotsThreshold); err != nil {
return err
}
l.TimeSlotsThreshold = config.TimeDuration(dur)
return nil
}
// TLSConfig contains configuration parameters needed to initialize(or not) secure server
type TLSConfig struct {
Enabled bool `mapstructure:"enabled" yaml:"enabled" json:"enabled"`
Certificate string `mapstructure:"cert" yaml:"cert" json:"cert"`
Key string `mapstructure:"key" yaml:"key" json:"key"`
}
// Set sets security server configuration values from config.DataProvider.
func (s *TLSConfig) Set(dp config.DataProvider) error {
var err error
if s.Enabled, err = dp.GetBool(cfgKeyServerTLSEnabled); err != nil {
return err
}
if s.Certificate, err = dp.GetString(cfgKeyServerTLSCert); err != nil {
return err
}
if s.Key, err = dp.GetString(cfgKeyServerTLSKey); err != nil {
return err
}
return nil
}
// Set sets HTTPServer configuration values from config.DataProvider.
func (c *Config) Set(dp config.DataProvider) error {
var err error
if c.Address, err = dp.GetString(cfgKeyServerAddress); err != nil {
return err
}
if c.UnixSocketPath, err = dp.GetString(cfgKeyServerUnixSocketPath); err != nil {
return err
}
err = c.TLS.Set(dp)
if err != nil {
return err
}
err = c.Timeouts.Set(dp)
if err != nil {
return err
}
err = c.Limits.Set(dp)
if err != nil {
return err
}
err = c.Log.Set(dp)
if err != nil {
return err
}
return nil
}