Ingen beskrivning
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() {
  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 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. 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. // findConfigFile 查找配置文件(内部函数保持不变)
  59. func findConfigFile() (string, error) {
  60. exePath, _ := os.Executable()
  61. exeDir := filepath.Dir(exePath)
  62. possiblePaths := []string{
  63. filepath.Join(exeDir, "db.yaml"),
  64. filepath.Join(exeDir, "config", "db.yaml"),
  65. "db.yaml",
  66. "config/db.yaml",
  67. os.Getenv("DB_CONFIG_PATH"),
  68. }
  69. for _, path := range possiblePaths {
  70. if path == "" {
  71. continue
  72. }
  73. if _, err := os.Stat(path); err == nil {
  74. log.Printf("✅ Using config file: %s\n", path)
  75. return path, nil
  76. }
  77. }
  78. return "", fmt.Errorf(`no configuration file found
  79. Searched locations:
  80. 1. %s
  81. 2. %s
  82. 3. ./db.yaml
  83. 4. ./config/db.yaml
  84. 5. DB_CONFIG_PATH环境变量指定的路径
  85. 请确保配置文件存在,或通过环境变量指定:
  86. export DB_CONFIG_PATH=/your/config/path/db.yaml
  87. set DB_CONFIG_PATH=C:\your\config\path\db.yaml (Windows)`,
  88. filepath.Join(exeDir, "db.yaml"),
  89. filepath.Join(exeDir, "config", "db.yaml"))
  90. }