-
Notifications
You must be signed in to change notification settings - Fork 16
/
types.go
157 lines (141 loc) · 4.81 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
package config
import (
"fmt"
"time"
"github.com/knadh/koanf"
"github.com/knadh/koanf/providers/confmap"
)
// // 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"`
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"`
FileName string `koanf:"fileName"`
TimeFormat string `koanf:"timeFormat"`
Level string `koanf:"level"`
Permission uint32 `koanf:"permission"`
NoColor bool `koanf:"noColor"`
StartupMsg bool `koanf:"startupMsg"`
}
type Pool struct {
Size int `koanf:"size"`
}
type Proxy struct {
Elastic bool `koanf:"elastic"`
ReuseElasticClients bool `koanf:"reuseElasticClients"`
}
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 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"`
}
// 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,
"permission": DefaultLogFilePermission,
},
},
"clients": map[string]interface{}{
"default": map[string]interface{}{
"receiveBufferSize": DefaultBufferSize,
"receiveChunkSize": DefaultChunkSize,
"tcpKeepAlivePeriod": DefaultTCPKeepAlivePeriod,
},
},
"pools": map[string]interface{}{
"default": map[string]interface{}{
"size": DefaultPoolSize,
},
},
"proxy": map[string]interface{}{
"default": map[string]interface{}{
"elastic": false,
"reuseElasticClients": false,
},
},
"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,
},
}, "")
if err := cfg.Load(defaultValues, nil); err != nil {
panic(fmt.Errorf("failed to load default global configuration: %w", err))
}
}
func LoadPluginConfigDefaults(cfg *koanf.Koanf) {
defaultValues := confmap.Provider(map[string]interface{}{
"plugins": map[string]interface{}{
"verificationPolicy": "passdown",
"compatibilityPolicy": "strict",
},
}, "")
if err := cfg.Load(defaultValues, nil); err != nil {
panic(fmt.Errorf("failed to load default plugin configuration: %w", err))
}
}