Açıklama Yok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

loader.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. configFile, err := findConfigFile()
  14. if err != nil {
  15. return err
  16. }
  17. // 2. 读取并解析文件
  18. data, err := os.ReadFile(configFile)
  19. if err != nil {
  20. return fmt.Errorf("read config file error: %v", err)
  21. }
  22. var rawConfig map[string]interface{}
  23. err = yaml.Unmarshal(data, &rawConfig)
  24. if err != nil {
  25. return fmt.Errorf("parse yaml error: %v", err)
  26. }
  27. // 3. 从map加载配置
  28. return LoadConfigFromMap(rawConfig)
  29. }
  30. // LoadConfigFromMap 从map[string]interface{}加载配置到注册表
  31. // 新增方法,供外部调用(比如从数据库加载后使用)
  32. func LoadConfigFromMap(rawConfig map[string]interface{}) error {
  33. // 1. 设置所有注册配置的默认值
  34. for _, config := range subconfigs.GetAllConfigs() {
  35. config.SetDefaults()
  36. }
  37. // 2. 循环注册表,为每个配置加载数据
  38. for name, config := range subconfigs.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. return fmt.Errorf("load config %s error: %v", name, err)
  44. }
  45. }
  46. }
  47. return nil
  48. }
  49. // convertMap 转换map类型(内部函数保持不变)
  50. func convertMap(input map[interface{}]interface{}) map[string]interface{} {
  51. output := make(map[string]interface{})
  52. for k, v := range input {
  53. if strKey, ok := k.(string); ok {
  54. output[strKey] = v
  55. }
  56. }
  57. return output
  58. }
  59. // findConfigFile 查找配置文件(内部函数保持不变)
  60. func findConfigFile() (string, error) {
  61. exePath, _ := os.Executable()
  62. exeDir := filepath.Dir(exePath)
  63. possiblePaths := []string{
  64. filepath.Join(exeDir, "db.yaml"),
  65. filepath.Join(exeDir, "config", "db.yaml"),
  66. "db.yaml",
  67. "config/db.yaml",
  68. os.Getenv("DB_CONFIG_PATH"),
  69. }
  70. for _, path := range possiblePaths {
  71. if path == "" {
  72. continue
  73. }
  74. if _, err := os.Stat(path); err == nil {
  75. log.Printf("✅ Using config file: %s\n", path)
  76. return path, nil
  77. }
  78. }
  79. return "", fmt.Errorf(`no configuration file found
  80. Searched locations:
  81. 1. %s
  82. 2. %s
  83. 3. ./db.yaml
  84. 4. ./config/db.yaml
  85. 5. DB_CONFIG_PATH环境变量指定的路径
  86. 请确保配置文件存在,或通过环境变量指定:
  87. export DB_CONFIG_PATH=/your/config/path/db.yaml
  88. set DB_CONFIG_PATH=C:\your\config\path\db.yaml (Windows)`,
  89. filepath.Join(exeDir, "db.yaml"),
  90. filepath.Join(exeDir, "config", "db.yaml"))
  91. }