-
Notifications
You must be signed in to change notification settings - Fork 12
/
config.go
241 lines (200 loc) · 6.99 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
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
/*
Copyright © 2024 Acronis International GmbH.
Released under MIT license.
*/
package httpserver
import (
"time"
"github.com/acronis/go-appkit/config"
)
const (
cfgKeyServerAddress = "server.address"
cfgKeyServerUnixSocketPath = "server.unixSocketPath"
cfgKeyServerTLSCert = "server.tls.cert"
cfgKeyServerTLSKey = "server.tls.key"
cfgKeyServerTLSEnabled = "server.tls.enabled"
cfgKeyServerTimeoutsWrite = "server.timeouts.write"
cfgKeyServerTimeoutsRead = "server.timeouts.read"
cfgKeyServerTimeoutsReadHeader = "server.timeouts.readHeader"
cfgKeyServerTimeoutsIdle = "server.timeouts.idle"
cfgKeyServerTimeoutsShutdown = "server.timeouts.shutdown"
cfgKeyServerLimitsMaxRequests = "server.limits.maxRequests"
cfgKeyServerLimitsMaxBodySize = "server.limits.maxBodySize"
cfgKeyServerLogRequestStart = "server.log.requestStart"
cfgKeyServerLogRequestHeaders = "server.log.requestHeaders"
cfgKeyServerLogExcludedEndpoints = "server.log.excludedEndpoints"
cfgKeyServerLogSecretQueryParams = "server.log.secretQueryParams" // nolint:gosec // false positive
cfgKeyServerLogAddRequestInfo = "server.log.addRequestInfo"
cfgKeyServerLogSlowRequestThreshold = "server.log.slowRequestThreshold"
)
const (
defaultServerAddress = ":8080"
defaultServerTimeoutsWrite = "1m"
defaultServerTimeoutsRead = "15s"
defaultServerTimeoutsReadHeader = "10s"
defaultServerTimeoutsIdle = "1m"
defaultServerTimeoutsShutdown = "5s"
defaultServerLimitsMaxRequests = 5000
defaultSlowRequestThreshold = "1s"
)
// TimeoutsConfig represents a set of configuration parameters for HTTPServer relating to timeouts.
type TimeoutsConfig struct {
Write time.Duration
Read time.Duration
ReadHeader time.Duration
Idle time.Duration
Shutdown time.Duration
}
// Set sets timeout server configuration values from config.DataProvider.
func (t *TimeoutsConfig) Set(dp config.DataProvider) error {
var err error
if t.Write, err = dp.GetDuration(cfgKeyServerTimeoutsWrite); err != nil {
return err
}
if t.Read, err = dp.GetDuration(cfgKeyServerTimeoutsRead); err != nil {
return err
}
if t.ReadHeader, err = dp.GetDuration(cfgKeyServerTimeoutsReadHeader); err != nil {
return err
}
if t.Idle, err = dp.GetDuration(cfgKeyServerTimeoutsIdle); err != nil {
return err
}
if t.Shutdown, err = dp.GetDuration(cfgKeyServerTimeoutsShutdown); err != nil {
return err
}
return nil
}
// LimitsConfig represents a set of configuration parameters for HTTPServer relating to limits.
type LimitsConfig struct {
MaxRequests int
MaxBodySizeBytes uint64
}
// 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.MaxBodySizeBytes, err = dp.GetSizeInBytes(cfgKeyServerLimitsMaxBodySize); err != nil {
return err
}
return nil
}
// LogConfig represents a set of configuration parameters for HTTPServer relating to logging.
type LogConfig struct {
RequestStart bool
RequestHeaders []string
ExcludedEndpoints []string
SecretQueryParams []string
AddRequestInfoToLogger bool
SlowRequestThreshold time.Duration
}
// 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
}
if l.SlowRequestThreshold, err = dp.GetDuration(cfgKeyServerLogSlowRequestThreshold); err != nil {
return err
}
return nil
}
// TLSConfig contains configuration parameters needed to initialize(or not) secure server
type TLSConfig struct {
Enabled bool
Certificate string
Key string
}
// 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
}
// Config represents a set of configuration parameters for HTTPServer.
type Config struct {
Address string
UnixSocketPath string
Timeouts TimeoutsConfig
Limits LimitsConfig
Log LogConfig
TLS TLSConfig
keyPrefix string
}
var _ config.Config = (*Config)(nil)
var _ config.KeyPrefixProvider = (*Config)(nil)
// NewConfig creates a new instance of the Config.
func NewConfig() *Config {
return NewConfigWithKeyPrefix("")
}
// NewConfigWithKeyPrefix creates a new instance of the Config.
// Allows specifying key prefix which will be used for parsing configuration parameters.
func NewConfigWithKeyPrefix(keyPrefix string) *Config {
return &Config{keyPrefix: keyPrefix}
}
// KeyPrefix returns a key prefix with which all configuration parameters should be presented.
func (c *Config) KeyPrefix() string {
return c.keyPrefix
}
// SetProviderDefaults sets default configuration values for HTTPServer in config.DataProvider.
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)
}
// 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
}