|
|
@@ -10,33 +10,39 @@ import (
|
|
10
|
10
|
"gopkg.in/yaml.v2"
|
|
11
|
11
|
)
|
|
12
|
12
|
|
|
13
|
|
-// LoadConfig 加载配置到注册表
|
|
|
13
|
+// LoadConfig 从文件加载配置到注册表(保持原有接口不变)
|
|
14
|
14
|
func LoadConfig() error {
|
|
15
|
|
- // 1. 设置所有注册配置的默认值
|
|
16
|
|
- for _, config := range subconfigs.GetAllConfigs() {
|
|
17
|
|
- config.SetDefaults()
|
|
18
|
|
- }
|
|
19
|
|
-
|
|
20
|
|
- // 2. 查找配置文件
|
|
|
15
|
+ // 1. 查找配置文件
|
|
21
|
16
|
configFile, err := findConfigFile()
|
|
22
|
17
|
if err != nil {
|
|
23
|
18
|
return err
|
|
24
|
19
|
}
|
|
25
|
20
|
|
|
26
|
|
- // 3. 读取文件
|
|
|
21
|
+ // 2. 读取并解析文件
|
|
27
|
22
|
data, err := os.ReadFile(configFile)
|
|
28
|
23
|
if err != nil {
|
|
29
|
24
|
return fmt.Errorf("read config file error: %v", err)
|
|
30
|
25
|
}
|
|
31
|
26
|
|
|
32
|
|
- // 4. 解析为map
|
|
33
|
27
|
var rawConfig map[string]interface{}
|
|
34
|
28
|
err = yaml.Unmarshal(data, &rawConfig)
|
|
35
|
29
|
if err != nil {
|
|
36
|
30
|
return fmt.Errorf("parse yaml error: %v", err)
|
|
37
|
31
|
}
|
|
38
|
32
|
|
|
39
|
|
- // 5. 循环注册表,为每个配置加载数据
|
|
|
33
|
+ // 3. 从map加载配置
|
|
|
34
|
+ return LoadConfigFromMap(rawConfig)
|
|
|
35
|
+}
|
|
|
36
|
+
|
|
|
37
|
+// LoadConfigFromMap 从map[string]interface{}加载配置到注册表
|
|
|
38
|
+// 新增方法,供外部调用(比如从数据库加载后使用)
|
|
|
39
|
+func LoadConfigFromMap(rawConfig map[string]interface{}) error {
|
|
|
40
|
+ // 1. 设置所有注册配置的默认值
|
|
|
41
|
+ for _, config := range subconfigs.GetAllConfigs() {
|
|
|
42
|
+ config.SetDefaults()
|
|
|
43
|
+ }
|
|
|
44
|
+
|
|
|
45
|
+ // 2. 循环注册表,为每个配置加载数据
|
|
40
|
46
|
for name, config := range subconfigs.GetAllConfigs() {
|
|
41
|
47
|
if configData, ok := rawConfig[name].(map[interface{}]interface{}); ok {
|
|
42
|
48
|
// 转换为 map[string]interface{}
|
|
|
@@ -47,17 +53,10 @@ func LoadConfig() error {
|
|
47
|
53
|
}
|
|
48
|
54
|
}
|
|
49
|
55
|
|
|
50
|
|
- // // 6. 验证所有配置
|
|
51
|
|
- // for name, config := range subconfigs.GetAllConfigs() {
|
|
52
|
|
- // if err := config.Validate(); err != nil {
|
|
53
|
|
- // return fmt.Errorf("validate config %s error: %v", name, err)
|
|
54
|
|
- // }
|
|
55
|
|
- // }
|
|
56
|
|
-
|
|
57
|
56
|
return nil
|
|
58
|
57
|
}
|
|
59
|
58
|
|
|
60
|
|
-// convertMap 转换map类型
|
|
|
59
|
+// convertMap 转换map类型(内部函数保持不变)
|
|
61
|
60
|
func convertMap(input map[interface{}]interface{}) map[string]interface{} {
|
|
62
|
61
|
output := make(map[string]interface{})
|
|
63
|
62
|
for k, v := range input {
|
|
|
@@ -68,7 +67,7 @@ func convertMap(input map[interface{}]interface{}) map[string]interface{} {
|
|
68
|
67
|
return output
|
|
69
|
68
|
}
|
|
70
|
69
|
|
|
71
|
|
-// findConfigFile 查找配置文件
|
|
|
70
|
+// findConfigFile 查找配置文件(内部函数保持不变)
|
|
72
|
71
|
func findConfigFile() (string, error) {
|
|
73
|
72
|
exePath, _ := os.Executable()
|
|
74
|
73
|
exeDir := filepath.Dir(exePath)
|
|
|
@@ -91,8 +90,6 @@ func findConfigFile() (string, error) {
|
|
91
|
90
|
}
|
|
92
|
91
|
}
|
|
93
|
92
|
|
|
94
|
|
- //return "", fmt.Errorf("no configuration file found")
|
|
95
|
|
-
|
|
96
|
93
|
return "", fmt.Errorf(`no configuration file found
|
|
97
|
94
|
|
|
98
|
95
|
Searched locations:
|