暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

loader.go 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package config
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "path/filepath"
  7. "git.x2erp.com/qdy/go-base/config/subconfigs"
  8. "gopkg.in/yaml.v2"
  9. )
  10. // LoadConfig 加载配置到注册表
  11. func LoadConfig() error {
  12. // 1. 设置所有注册配置的默认值
  13. for _, config := range subconfigs.GetAllConfigs() {
  14. config.SetDefaults()
  15. }
  16. // 2. 查找配置文件
  17. configFile, err := findConfigFile()
  18. if err != nil {
  19. return err
  20. }
  21. // 3. 读取文件
  22. data, err := os.ReadFile(configFile)
  23. if err != nil {
  24. return fmt.Errorf("read config file error: %v", err)
  25. }
  26. // 4. 解析为map
  27. var rawConfig map[string]interface{}
  28. err = yaml.Unmarshal(data, &rawConfig)
  29. if err != nil {
  30. return fmt.Errorf("parse yaml error: %v", err)
  31. }
  32. // 5. 循环注册表,为每个配置加载数据
  33. for name, config := range subconfigs.GetAllConfigs() {
  34. if configData, ok := rawConfig[name].(map[interface{}]interface{}); ok {
  35. // 转换为 map[string]interface{}
  36. strMap := convertMap(configData)
  37. if err := config.Load(strMap); err != nil {
  38. return fmt.Errorf("load config %s error: %v", name, err)
  39. }
  40. }
  41. }
  42. // // 6. 验证所有配置
  43. // for name, config := range subconfigs.GetAllConfigs() {
  44. // if err := config.Validate(); err != nil {
  45. // return fmt.Errorf("validate config %s error: %v", name, err)
  46. // }
  47. // }
  48. return nil
  49. }
  50. // convertMap 转换map类型
  51. func convertMap(input map[interface{}]interface{}) map[string]interface{} {
  52. output := make(map[string]interface{})
  53. for k, v := range input {
  54. if strKey, ok := k.(string); ok {
  55. output[strKey] = v
  56. }
  57. }
  58. return output
  59. }
  60. // findConfigFile 查找配置文件
  61. func findConfigFile() (string, error) {
  62. exePath, _ := os.Executable()
  63. exeDir := filepath.Dir(exePath)
  64. possiblePaths := []string{
  65. filepath.Join(exeDir, "db.yaml"),
  66. filepath.Join(exeDir, "config", "db.yaml"),
  67. "db.yaml",
  68. "config/db.yaml",
  69. os.Getenv("DB_CONFIG_PATH"),
  70. }
  71. for _, path := range possiblePaths {
  72. if path == "" {
  73. continue
  74. }
  75. if _, err := os.Stat(path); err == nil {
  76. log.Printf("✅ Using config file: %s\n", path)
  77. return path, nil
  78. }
  79. }
  80. //return "", fmt.Errorf("no configuration file found")
  81. return "", fmt.Errorf(`no configuration file found
  82. Searched locations:
  83. 1. %s
  84. 2. %s
  85. 3. ./db.yaml
  86. 4. ./config/db.yaml
  87. 5. DB_CONFIG_PATH环境变量指定的路径
  88. 请确保配置文件存在,或通过环境变量指定:
  89. export DB_CONFIG_PATH=/your/config/path/db.yaml
  90. set DB_CONFIG_PATH=C:\your\config\path\db.yaml (Windows)`,
  91. filepath.Join(exeDir, "db.yaml"),
  92. filepath.Join(exeDir, "config", "db.yaml"))
  93. }