Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package config
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "path/filepath"
  7. "git.x2erp.com/qdy/go-base/config/core"
  8. "gopkg.in/yaml.v2"
  9. )
  10. // LoadConfig 从文件加载配置到注册表(保持原有接口不变)
  11. func LoadConfig() {
  12. // 1. 查找配置文件
  13. configFile, err := findConfigFile()
  14. if err != nil {
  15. log.Fatalf("查找配置文件错误:%v", err)
  16. }
  17. // 2. 读取并解析文件
  18. data, err := os.ReadFile(configFile)
  19. if err != nil {
  20. log.Fatalf("read config file error: %v", err)
  21. }
  22. var rawConfig map[string]interface{}
  23. err = yaml.Unmarshal(data, &rawConfig)
  24. if err != nil {
  25. log.Fatalf("parse yaml error: %v", err)
  26. }
  27. // 3. 从map加载配置
  28. LoadConfigFromMap(rawConfig)
  29. }
  30. // LoadConfigFromMap 从map[string]interface{}加载配置到注册表
  31. // 新增方法,供外部调用(比如从数据库加载后使用)
  32. func LoadConfigFromMap(rawConfig map[string]interface{}) {
  33. // 1. 设置所有注册配置的默认值
  34. for _, config := range core.GetAllConfigs() {
  35. config.SetDefaults()
  36. }
  37. // 2. 循环注册表,为每个配置加载数据
  38. for name, config := range core.GetAllConfigs() {
  39. if configData, ok := rawConfig[name].(map[interface{}]interface{}); ok {
  40. // 转换为 map[string]interface{}
  41. strMap := convertMap(configData)
  42. if err := config.Load(strMap); err != nil {
  43. log.Fatalf("load config %s error: %v", name, err)
  44. }
  45. }
  46. }
  47. }
  48. // convertMap 转换map类型(内部函数保持不变)
  49. func convertMap(input map[interface{}]interface{}) map[string]interface{} {
  50. output := make(map[string]interface{})
  51. for k, v := range input {
  52. if strKey, ok := k.(string); ok {
  53. output[strKey] = v
  54. }
  55. }
  56. return output
  57. }
  58. // LoadConfigFromMeta 从ConfigMeta集合加载配置到注册表
  59. func LoadConfigFromMeta(metaMap map[string]*core.ConfigMeta) error {
  60. // 1. 转换元数据为配置数据
  61. rawConfig := convertMetaToConfig(metaMap)
  62. // 2. 使用现有的LoadConfigFromMap方法加载
  63. LoadConfigFromMap(rawConfig)
  64. return nil
  65. }
  66. // convertMetaToConfig 将元数据转换为配置数据
  67. func convertMetaToConfig(metaMap map[string]*core.ConfigMeta) map[string]interface{} {
  68. rawConfig := make(map[string]interface{})
  69. for configName, configMeta := range metaMap {
  70. configData := make(map[string]interface{})
  71. // 遍历每个字段,设置默认值
  72. for fieldName, fieldMeta := range configMeta.Fields {
  73. // 根据字段类型设置合理的默认值
  74. defaultValue := getDefaultValueByType(fieldMeta.Type)
  75. configData[fieldName] = defaultValue
  76. }
  77. rawConfig[configName] = configData
  78. }
  79. return rawConfig
  80. }
  81. // getDefaultValueByType 根据类型返回默认值
  82. func getDefaultValueByType(typeStr string) interface{} {
  83. switch typeStr {
  84. case "string":
  85. return ""
  86. case "int", "int8", "int16", "int32", "int64", "uint", "uint8", "uint16", "uint32", "uint64":
  87. return 0
  88. case "float32", "float64":
  89. return 0.0
  90. case "bool":
  91. return false
  92. case "[]string":
  93. return []string{}
  94. case "[]int":
  95. return []int{}
  96. case "map[string]string":
  97. return make(map[string]string)
  98. case "map[string]interface{}":
  99. return make(map[string]interface{})
  100. default:
  101. // 如果是其他结构体或指针类型,返回nil
  102. return nil
  103. }
  104. }
  105. // findConfigFile 查找配置文件
  106. func findConfigFile() (string, error) {
  107. exePath, _ := os.Executable()
  108. exeDir := filepath.Dir(exePath)
  109. // 获取当前工作目录
  110. currentDir, err := os.Getwd()
  111. if err != nil {
  112. currentDir = "."
  113. }
  114. appName := filepath.Base(currentDir)
  115. // 构建可能的配置文件路径
  116. possiblePaths := []string{
  117. // 1. 当前目录的 appName.yaml
  118. filepath.Join(currentDir, appName+".yaml"),
  119. // 2. 当前目录的上级目录的 appName.yaml
  120. filepath.Join(filepath.Dir(currentDir), appName+".yaml"),
  121. // 3. 可执行文件所在目录的 appName.yaml
  122. filepath.Join(exeDir, appName+".yaml"),
  123. // 4. 可执行文件所在目录的上级目录的 appName.yaml
  124. filepath.Join(filepath.Dir(exeDir), appName+".yaml"),
  125. // 5. 原始逻辑中的 db.yaml 路径
  126. filepath.Join(exeDir, "db.yaml"),
  127. filepath.Join(exeDir, "config", "db.yaml"),
  128. "db.yaml",
  129. "config/db.yaml",
  130. // 6. 环境变量指定的路径
  131. os.Getenv("DB_CONFIG_PATH"),
  132. }
  133. for _, path := range possiblePaths {
  134. if path == "" {
  135. continue
  136. }
  137. log.Printf("find config file: %s", path)
  138. // 清理路径
  139. cleanPath := filepath.Clean(path)
  140. if _, err := os.Stat(cleanPath); err == nil {
  141. log.Printf("✅ Using config file: %s\n", cleanPath)
  142. return cleanPath, nil
  143. }
  144. }
  145. return "", fmt.Errorf(`no configuration file found
  146. Searched locations:
  147. 1. %s (当前目录)
  148. 2. %s (上级目录)
  149. 3. %s (可执行文件目录)
  150. 4. %s (可执行文件上级目录)
  151. 5. %s
  152. 6. %s
  153. 7. ./db.yaml
  154. 8. ./config/db.yaml
  155. 9. DB_CONFIG_PATH环境变量指定的路径
  156. 请确保配置文件存在,或通过环境变量指定:
  157. export DB_CONFIG_PATH=/your/config/path/db.yaml
  158. set DB_CONFIG_PATH=C:\your\config\path\db.yaml (Windows)`,
  159. filepath.Join(currentDir, appName+".yaml"),
  160. filepath.Join(filepath.Dir(currentDir), appName+".yaml"),
  161. filepath.Join(exeDir, appName+".yaml"),
  162. filepath.Join(filepath.Dir(exeDir), appName+".yaml"),
  163. filepath.Join(exeDir, "db.yaml"),
  164. filepath.Join(exeDir, "config", "db.yaml"))
  165. }