-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathconfigs.go
More file actions
170 lines (152 loc) · 4.29 KB
/
configs.go
File metadata and controls
170 lines (152 loc) · 4.29 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
// Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package configs
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/joho/godotenv"
"gopkg.in/yaml.v3"
)
type VeADKConfig struct {
Volcengine *Volcengine `yaml:"volcengine"`
Model *ModelConfig `yaml:"model"`
Tool *BuiltinToolConfigs `yaml:"tools"`
PromptPilot *PromptPilotConfig `yaml:"prompt_pilot"`
TlsConfig *TLSConfig `yaml:"tls_config"`
Veidentity *VeIdentityConfig `yaml:"veidentity"`
Database *DatabaseConfig `yaml:"database"`
LOGGING *Logging `yaml:"LOGGING"`
}
type EnvConfigMaptoStruct interface {
MapEnvToConfig() // 用于映射环境变量到结构体字段
}
var globalConfig *VeADKConfig
func GetGlobalConfig() *VeADKConfig {
if globalConfig == nil {
if err := SetupVeADKConfig(); err != nil {
panic(err)
}
}
return globalConfig
}
func SetupVeADKConfig() error {
if err := loadConfigFromProjectEnv(); err != nil {
return err
}
if err := loadConfigFromProjectYaml(); err != nil {
return err
}
// 3. 从环境变量构建最终配置
globalConfig = &VeADKConfig{
Volcengine: &Volcengine{},
Model: &ModelConfig{
Agent: &AgentConfig{},
Image: &CommonModelConfig{},
Video: &CommonModelConfig{},
},
Tool: &BuiltinToolConfigs{
MCPRouter: &MCPRouter{},
RunCode: &RunCode{},
},
PromptPilot: &PromptPilotConfig{},
TlsConfig: &TLSConfig{},
Veidentity: &VeIdentityConfig{},
LOGGING: &Logging{},
Database: &DatabaseConfig{
Postgresql: &CommonDatabaseConfig{},
Viking: &VikingConfig{},
TOS: &TosClientConf{},
},
}
globalConfig.Model.MapEnvToConfig()
globalConfig.Tool.MapEnvToConfig()
globalConfig.LOGGING.MapEnvToConfig()
globalConfig.Database.MapEnvToConfig()
globalConfig.Volcengine.MapEnvToConfig()
return nil
}
func loadConfigFromProjectEnv() error {
dir, err := os.Getwd()
if err != nil {
return err
}
envFilePath := filepath.Join(dir, ".env")
if _, err := os.Stat(envFilePath); err == nil {
// godotenv.Load 默认不会覆盖已存在的环境变量
if err := godotenv.Load(envFilePath); err != nil {
return fmt.Errorf("加载 .env 文件失败: %v", err)
}
}
return nil
}
func loadConfigFromProjectYaml() error {
dir, err := os.Getwd()
if err != nil {
return err
}
// 2. 加载 config.yaml(优先级最低)
var yamlConfig map[string]interface{}
configYamlPath := filepath.Join(dir, "config.yaml")
if _, err := os.Stat(configYamlPath); err == nil {
data, err := os.ReadFile(configYamlPath)
if err != nil {
return fmt.Errorf("读取 config.yaml 失败: %v", err)
}
if err := yaml.Unmarshal(data, &yamlConfig); err != nil {
return fmt.Errorf("解析 config.yaml 失败: %v", err)
}
// 将 yaml 配置转换为环境变量格式(如 model.name -> MODEL_NAME),但不覆盖已有变量
setYamlToEnv(yamlConfig, "")
}
return nil
}
func setYamlToEnv(data map[string]interface{}, prefix string) {
for key, val := range data {
fullKey := key
if prefix != "" {
fullKey = fmt.Sprintf("%s_%s", prefix, key)
}
fullKey = strings.ToUpper(fullKey)
switch v := val.(type) {
case map[string]interface{}:
setYamlToEnv(v, fullKey)
case string:
// 仅在环境变量不存在时设置
if os.Getenv(fullKey) == "" {
_ = os.Setenv(fullKey, v)
}
case int:
if os.Getenv(fullKey) == "" {
_ = os.Setenv(fullKey, strconv.Itoa(v))
}
}
}
}
// 获取环境变量(类似 Python 的 getenv 函数)
func getEnv(envName string, defaultValue string, allowFalseValues bool) string {
value := os.Getenv(envName)
if value != "" {
return value
}
if allowFalseValues {
return defaultValue
}
if defaultValue != "" {
return defaultValue
}
return ""
}