-
Notifications
You must be signed in to change notification settings - Fork 315
Expand file tree
/
Copy pathconfig.go
More file actions
322 lines (292 loc) · 8.17 KB
/
config.go
File metadata and controls
322 lines (292 loc) · 8.17 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
package config
import (
"bytes"
"os"
"regexp"
"strings"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"
)
type AppCfg struct {
Name string
Env string
Host string
Port int
}
type RootCfg struct {
ApiBearerToken string
ProjectBearerTokenPrefix string
SecretPepper string
EnableArgon2Verification bool
}
type LogCfg struct {
Level string
}
type DBCfg struct {
DSN string
MaxOpen int
MaxIdle int
MaxIdleTimeSec int
AutoMigrate bool
EnableTLS bool
}
type RedisCfg struct {
Addr string
Password string
DB int
PoolSize int
EnableTLS bool
}
type MQExchangeName struct {
SessionMessage string
LearningSkill string
}
type MQRoutingKey struct {
SessionMessageInsert string
LearningSkillDistill string
}
type MQCfg struct {
URL string
Queue string
Prefetch int
EnableTLS bool
ExchangeName MQExchangeName
RoutingKey MQRoutingKey
}
type S3Cfg struct {
Endpoint string
InternalEndpoint string
Region string
AccessKey string
SecretKey string
Bucket string
UsePathStyle bool
PresignExpireSec int
SSE string
}
type CoreCfg struct {
BaseURL string
}
type TelemetryCfg struct {
OtlpEndpoint string
Enabled bool
SampleRatio float64 // Sampling ratio, range 0.0-1.0, default 1.0 (100%)
}
type ArtifactCfg struct {
MaxUploadSizeBytes int64 // Maximum file upload size in bytes
}
type EncryptionCfg struct {
MasterKey string // Admin master key for envelope encryption (env: APP_ENCRYPTION_MASTERKEY)
Enabled bool // Enable S3 envelope encryption (default: false)
}
type Config struct {
App AppCfg
Root RootCfg
Log LogCfg
Database DBCfg
Redis RedisCfg
RabbitMQ MQCfg
S3 S3Cfg
Core CoreCfg
Telemetry TelemetryCfg
Artifact ArtifactCfg
Encryption EncryptionCfg
}
func setDefaults(v *viper.Viper) {
v.SetDefault("app.env", "debug")
v.SetDefault("app.port", 8029)
v.SetDefault("root.apiBearerToken", "your-root-api-bearer-token")
v.SetDefault("root.projectBearerTokenPrefix", "sk-ac-")
v.SetDefault("root.enableArgon2Verification", true)
v.SetDefault("database.dsn", "host=127.0.0.1 user=acontext password=helloworld dbname=acontext port=15432 sslmode=disable TimeZone=UTC")
v.SetDefault("database.enableTLS", false)
v.SetDefault("redis.addr", "127.0.0.1:16379")
v.SetDefault("redis.password", "helloworld")
v.SetDefault("redis.db", 0)
v.SetDefault("redis.poolSize", 10)
v.SetDefault("redis.enableTLS", false)
v.SetDefault("s3.endpoint", "http://127.0.0.1:19000")
v.SetDefault("s3.internalEndpoint", "http://127.0.0.1:19000")
v.SetDefault("s3.region", "auto")
v.SetDefault("s3.accessKey", "acontext")
v.SetDefault("s3.secretKey", "helloworld")
v.SetDefault("s3.bucket", "acontext-assets")
v.SetDefault("rabbitmq.url", "amqp://acontext:helloworld@127.0.0.1:15672/%2F")
v.SetDefault("rabbitmq.enableTLS", false)
v.SetDefault("rabbitmq.exchangeName.sessionMessage", "session.message")
v.SetDefault("rabbitmq.exchangeName.learningSkill", "learning.skill")
v.SetDefault("rabbitmq.routingKey.sessionMessageInsert", "session.message.insert")
v.SetDefault("rabbitmq.routingKey.learningSkillDistill", "learning.skill.distill")
v.SetDefault("core.baseURL", "http://127.0.0.1:8019")
v.SetDefault("telemetry.otlpEndpoint", "http://127.0.0.1:4317")
v.SetDefault("telemetry.enabled", true)
v.SetDefault("telemetry.sampleRatio", 1.0) // Default 100% sampling
v.SetDefault("artifact.maxUploadSizeBytes", 16777216) // Default 16MB (16 * 1024 * 1024 bytes)
v.SetDefault("encryption.enabled", false)
v.SetDefault("encryption.masterKey", "")
}
func Load() (*Config, error) {
base := viper.New()
base.SetConfigName("config")
base.SetConfigType("yaml")
base.AddConfigPath("./configs")
base.AddConfigPath(".")
base.AutomaticEnv()
base.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
base.SetEnvPrefix("APP") // e.g. APP_APP_PORT -> app.port
// First assign a default value (effective regardless of whether there is a file or not)
setDefaults(base)
// Read the file (if any)
if err := base.ReadInConfig(); err == nil {
// After finding the file, manually perform one expansion of ${ENV}, and then parse it.
path := base.ConfigFileUsed()
raw, err := os.ReadFile(path)
if err != nil {
return nil, err
}
// Parse YAML to find and remove keys with undefined environment variables
var yamlData interface{}
if err := yaml.Unmarshal(raw, &yamlData); err == nil {
keysToRemove := findKeysWithUndefinedEnvVars(yamlData, "")
if len(keysToRemove) > 0 {
removeKeys(yamlData, keysToRemove)
// Re-marshal to YAML bytes
if cleanedYaml, err := yaml.Marshal(yamlData); err == nil {
raw = cleanedYaml
}
}
}
expanded := os.ExpandEnv(string(raw))
// Load the expanded content with a new viper and copy the env settings.
v := viper.New()
v.SetConfigType("yaml")
if err := v.ReadConfig(bytes.NewBufferString(expanded)); err != nil {
return nil, err
}
v.AutomaticEnv()
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
v.SetEnvPrefix("APP")
setDefaults(v)
cfg := new(Config)
if err := v.Unmarshal(&cfg); err != nil {
return nil, err
}
return cfg, nil
}
// No files are also allowed, using only env + default values
cfg := new(Config)
if err := base.Unmarshal(&cfg); err != nil {
return nil, err
}
return cfg, nil
}
// removeKeys removes keys from the YAML data based on dot-separated paths
func removeKeys(data interface{}, keysToRemove []string) {
for _, keyPath := range keysToRemove {
parts := strings.Split(keyPath, ".")
if len(parts) == 0 {
continue
}
removeKeyRecursive(data, parts, 0)
}
}
// removeKeyRecursive recursively removes a key from nested maps
func removeKeyRecursive(data interface{}, parts []string, index int) bool {
if index >= len(parts) {
return false
}
currentKey := parts[index]
isLast := index == len(parts)-1
switch m := data.(type) {
case map[string]interface{}:
if isLast {
if _, ok := m[currentKey]; ok {
delete(m, currentKey)
return true
}
return false
}
if next, ok := m[currentKey]; ok {
if removeKeyRecursive(next, parts, index+1) {
// Remove parent key if nested map is now empty
if isEmptyMap(next) {
delete(m, currentKey)
}
return true
}
}
case map[interface{}]interface{}:
for k, v := range m {
if strKey, ok := k.(string); ok && strKey == currentKey {
if isLast {
delete(m, k)
return true
}
if removeKeyRecursive(v, parts, index+1) {
if isEmptyMap(v) {
delete(m, k)
}
return true
}
break
}
}
}
return false
}
// isEmptyMap checks if a value is an empty map
func isEmptyMap(v interface{}) bool {
if m, ok := v.(map[string]interface{}); ok {
return len(m) == 0
}
if m, ok := v.(map[interface{}]interface{}); ok {
return len(m) == 0
}
return false
}
// findKeysWithUndefinedEnvVars recursively finds keys that contain undefined environment variables
func findKeysWithUndefinedEnvVars(data interface{}, prefix string) []string {
var keysToRemove []string
envVarPattern := regexp.MustCompile(`\$\{([^}]+)\}`)
switch v := data.(type) {
case map[string]interface{}:
for key, value := range v {
fullKey := key
if prefix != "" {
fullKey = prefix + "." + key
}
keysToRemove = append(keysToRemove, findKeysWithUndefinedEnvVars(value, fullKey)...)
}
case map[interface{}]interface{}:
for key, value := range v {
if keyStr, ok := key.(string); ok {
fullKey := keyStr
if prefix != "" {
fullKey = prefix + "." + keyStr
}
keysToRemove = append(keysToRemove, findKeysWithUndefinedEnvVars(value, fullKey)...)
}
}
case []interface{}:
for i, item := range v {
fullKey := prefix
if prefix != "" {
fullKey = prefix + "[" + string(rune(i+'0')) + "]"
}
keysToRemove = append(keysToRemove, findKeysWithUndefinedEnvVars(item, fullKey)...)
}
case string:
matches := envVarPattern.FindAllStringSubmatch(v, -1)
for _, match := range matches {
if len(match) > 1 {
if _, exists := os.LookupEnv(match[1]); !exists {
if prefix != "" {
keysToRemove = append(keysToRemove, prefix)
}
break
}
}
}
}
return keysToRemove
}