-
Notifications
You must be signed in to change notification settings - Fork 18
/
types.go
197 lines (179 loc) · 6.3 KB
/
types.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
package config
import (
"fmt"
"strings"
"time"
"github.com/knadh/koanf"
"github.com/knadh/koanf/providers/confmap"
"github.com/knadh/koanf/providers/env"
)
// // getPath returns the path to the referenced config value.
// func getPath(cfg *koanf.Koanf, path string) string {
// ref := cfg.String(path)
// if cfg.Exists(path) && cfg.StringMap(ref) != nil {
// return ref
// }
// return path
// }
type Plugin struct {
Name string `koanf:"name"`
Enabled bool `koanf:"enabled"`
LocalPath string `koanf:"localPath"`
Args []string `koanf:"args"`
Env []string `koanf:"env"`
Checksum string `koanf:"checksum"`
}
type PluginConfig struct {
VerificationPolicy string `koanf:"verificationPolicy"`
CompatibilityPolicy string `koanf:"compatibilityPolicy"`
AcceptancePolicy string `koanf:"acceptancePolicy"`
MetricsMergerPeriod time.Duration `koanf:"metricsMergerPeriod"`
Plugins []Plugin `koanf:"plugins"`
}
type Client struct {
Network string `koanf:"network"`
Address string `koanf:"address"`
TCPKeepAlive bool `koanf:"tcpKeepAlive"`
TCPKeepAlivePeriod time.Duration `koanf:"tcpKeepAlivePeriod"`
ReceiveBufferSize int `koanf:"receiveBufferSize"`
ReceiveChunkSize int `koanf:"receiveChunkSize"`
ReceiveDeadline time.Duration `koanf:"receiveDeadline"`
SendDeadline time.Duration `koanf:"sendDeadline"`
}
type Logger struct {
Output string `koanf:"output"`
TimeFormat string `koanf:"timeFormat"`
Level string `koanf:"level"`
NoColor bool `koanf:"noColor"`
StartupMsg bool `koanf:"startupMsg"`
FileName string `koanf:"fileName"`
MaxSize int `koanf:"maxSize"`
MaxBackups int `koanf:"maxBackups"`
MaxAge int `koanf:"maxAge"`
Compress bool `koanf:"compress"`
}
type Pool struct {
Size int `koanf:"size"`
}
type Proxy struct {
Elastic bool `koanf:"elastic"`
ReuseElasticClients bool `koanf:"reuseElasticClients"`
HealthCheckPeriod time.Duration `koanf:"healthCheckPeriod"`
}
type Server struct {
EnableTicker bool `koanf:"enableTicker"`
MultiCore bool `koanf:"multiCore"`
LockOSThread bool `koanf:"lockOSThread"`
ReuseAddress bool `koanf:"reuseAddress"`
ReusePort bool `koanf:"reusePort"`
TCPNoDelay bool `koanf:"tcpNoDelay"`
ReadBufferCap int `koanf:"readBufferCap"`
WriteBufferCap int `koanf:"writeBufferCap"`
SocketRecvBuffer int `koanf:"socketRecvBuffer"`
SocketSendBuffer int `koanf:"socketSendBuffer"`
SoftLimit uint64 `koanf:"softLimit"`
HardLimit uint64 `koanf:"hardLimit"`
TCPKeepAlive time.Duration `koanf:"tcpKeepAlive"`
TickInterval time.Duration `koanf:"tickInterval"`
Network string `koanf:"network"`
Address string `koanf:"address"`
LoadBalancer string `koanf:"loadBalancer"`
}
type Metrics struct {
Enabled bool `koanf:"enabled"`
Address string `koanf:"address"`
Path string `koanf:"path"`
}
type GlobalConfig struct {
Loggers map[string]Logger `koanf:"loggers"`
Clients map[string]Client `koanf:"clients"`
Pools map[string]Pool `koanf:"pools"`
Proxy map[string]Proxy `koanf:"proxy"`
Server Server `koanf:"server"`
Metrics map[string]Metrics `koanf:"metrics"`
}
// LoadDefaultConfig loads the default configuration before loading the config file.
func LoadGlobalConfigDefaults(cfg *koanf.Koanf) {
defaultValues := confmap.Provider(map[string]interface{}{
"loggers": map[string]interface{}{
"default": map[string]interface{}{
"output": DefaultLogOutput,
"level": DefaultLogLevel,
"fileName": DefaultLogFileName,
"maxSize": DefaultMaxSize,
"maxBackups": DefaultMaxBackups,
"maxAge": DefaultMaxAge,
"compress": DefaultCompress,
},
},
"clients": map[string]interface{}{
"default": map[string]interface{}{
"receiveBufferSize": DefaultBufferSize,
"receiveChunkSize": DefaultChunkSize,
"tcpKeepAlivePeriod": DefaultTCPKeepAlivePeriod.String(),
},
},
"pools": map[string]interface{}{
"default": map[string]interface{}{
"size": DefaultPoolSize,
},
},
"proxy": map[string]interface{}{
"default": map[string]interface{}{
"elastic": false,
"reuseElasticClients": false,
"healthCheckPeriod": DefaultHealthCheckPeriod.String(),
},
},
"server": map[string]interface{}{
"network": DefaultListenNetwork,
"address": DefaultListenAddress,
"softLimit": 0,
"hardLimit": 0,
"enableTicker": false,
"multiCore": true,
"lockOSThread": false,
"reuseAddress": true,
"reusePort": true,
"loadBalancer": DefaultLoadBalancer,
"readBufferCap": DefaultBufferSize,
"writeBufferCap": DefaultBufferSize,
"socketRecvBuffer": DefaultBufferSize,
"socketSendBuffer": DefaultBufferSize,
},
"metrics": map[string]interface{}{
"default": map[string]interface{}{
"enabled": true,
"address": DefaultMetricsAddress,
"path": DefaultMetricsPath,
},
},
}, "")
if err := cfg.Load(defaultValues, nil); err != nil {
panic(fmt.Errorf("failed to load default global configuration: %w", err))
}
}
// LoadPluginConfigDefaults loads the default plugin configuration
// before loading the plugin config file.
func LoadPluginConfigDefaults(cfg *koanf.Koanf) {
defaultValues := confmap.Provider(map[string]interface{}{
"plugins": map[string]interface{}{
"verificationPolicy": "passdown",
"compatibilityPolicy": "strict",
"acceptancePolicy": "accept",
"metricsMergerPeriod": DefaultMetricsMergerPeriod.String(),
},
}, "")
if err := cfg.Load(defaultValues, nil); err != nil {
panic(fmt.Errorf("failed to load default plugin configuration: %w", err))
}
}
// LoadEnvVars loads the environment variables into the configuration with the
// given prefix, "GATEWAYD_".
func LoadEnvVars(cfg *koanf.Koanf) {
if err := cfg.Load(env.Provider(EnvPrefix, ".", func(env string) string {
return strings.ReplaceAll(strings.ToLower(strings.TrimPrefix(env, EnvPrefix)), "_", ".")
}), nil); err != nil {
panic(fmt.Errorf("failed to load environment variables: %w", err))
}
}