| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- package config
-
- import (
- "fmt"
- "log"
- "os"
- "path/filepath"
-
- "git.x2erp.com/qdy/go-base/config/core"
- "gopkg.in/yaml.v2"
- )
-
- // LoadConfig 从文件加载配置到注册表(保持原有接口不变)
- func LoadConfig() {
- // 1. 查找配置文件
- configFile, err := findConfigFile()
- if err != nil {
- log.Fatalf("查找配置文件错误:%v", err)
-
- }
-
- // 2. 读取并解析文件
- data, err := os.ReadFile(configFile)
- if err != nil {
- log.Fatalf("read config file error: %v", err)
- }
-
- var rawConfig map[string]interface{}
- err = yaml.Unmarshal(data, &rawConfig)
- if err != nil {
- log.Fatalf("parse yaml error: %v", err)
-
- }
-
- // 3. 从map加载配置
- LoadConfigFromMap(rawConfig)
- }
-
- // LoadConfigFromMap 从map[string]interface{}加载配置到注册表
- // 新增方法,供外部调用(比如从数据库加载后使用)
- func LoadConfigFromMap(rawConfig map[string]interface{}) {
- // 1. 设置所有注册配置的默认值
- for _, config := range core.GetAllConfigs() {
- config.SetDefaults()
- }
-
- // 2. 循环注册表,为每个配置加载数据
- for name, config := range core.GetAllConfigs() {
- if configData, ok := rawConfig[name].(map[interface{}]interface{}); ok {
- // 转换为 map[string]interface{}
- strMap := convertMap(configData)
- if err := config.Load(strMap); err != nil {
- log.Fatalf("load config %s error: %v", name, err)
- }
- }
- }
- }
-
- // convertMap 转换map类型(内部函数保持不变)
- func convertMap(input map[interface{}]interface{}) map[string]interface{} {
- output := make(map[string]interface{})
- for k, v := range input {
- if strKey, ok := k.(string); ok {
- output[strKey] = v
- }
- }
- return output
- }
-
- // LoadConfigFromMeta 从ConfigMeta集合加载配置到注册表
- func LoadConfigFromMeta(metaMap map[string]*core.ConfigMeta) error {
- // 1. 转换元数据为配置数据
- rawConfig := convertMetaToConfig(metaMap)
-
- // 2. 使用现有的LoadConfigFromMap方法加载
- LoadConfigFromMap(rawConfig)
-
- return nil
- }
-
- // convertMetaToConfig 将元数据转换为配置数据
- func convertMetaToConfig(metaMap map[string]*core.ConfigMeta) map[string]interface{} {
- rawConfig := make(map[string]interface{})
-
- for configName, configMeta := range metaMap {
- configData := make(map[string]interface{})
-
- // 遍历每个字段,设置默认值
- for fieldName, fieldMeta := range configMeta.Fields {
- // 根据字段类型设置合理的默认值
- defaultValue := getDefaultValueByType(fieldMeta.Type)
- configData[fieldName] = defaultValue
- }
-
- rawConfig[configName] = configData
- }
-
- return rawConfig
- }
-
- // getDefaultValueByType 根据类型返回默认值
- func getDefaultValueByType(typeStr string) interface{} {
- switch typeStr {
- case "string":
- return ""
- case "int", "int8", "int16", "int32", "int64", "uint", "uint8", "uint16", "uint32", "uint64":
- return 0
- case "float32", "float64":
- return 0.0
- case "bool":
- return false
- case "[]string":
- return []string{}
- case "[]int":
- return []int{}
- case "map[string]string":
- return make(map[string]string)
- case "map[string]interface{}":
- return make(map[string]interface{})
- default:
- // 如果是其他结构体或指针类型,返回nil
- return nil
- }
- }
-
- // findConfigFile 查找配置文件
- func findConfigFile() (string, error) {
- exePath, _ := os.Executable()
- exeDir := filepath.Dir(exePath)
-
- // 获取当前工作目录
- currentDir, err := os.Getwd()
- if err != nil {
- currentDir = "."
- }
-
- appName := filepath.Base(currentDir)
- // 构建可能的配置文件路径
- possiblePaths := []string{
-
- // 1. 当前目录的 appName.yaml
- filepath.Join(currentDir, appName+".yaml"),
-
- // 2. 当前目录的上级目录的 appName.yaml
- filepath.Join(filepath.Dir(currentDir), appName+".yaml"),
-
- // 3. 可执行文件所在目录的 appName.yaml
- filepath.Join(exeDir, appName+".yaml"),
-
- // 4. 可执行文件所在目录的上级目录的 appName.yaml
- filepath.Join(filepath.Dir(exeDir), appName+".yaml"),
-
- // 5. 原始逻辑中的 db.yaml 路径
- filepath.Join(exeDir, "db.yaml"),
- filepath.Join(exeDir, "config", "db.yaml"),
- "db.yaml",
- "config/db.yaml",
-
- // 6. 环境变量指定的路径
- os.Getenv("DB_CONFIG_PATH"),
- }
-
- for _, path := range possiblePaths {
- if path == "" {
- continue
- }
- log.Printf("find config file: %s", path)
- // 清理路径
- cleanPath := filepath.Clean(path)
- if _, err := os.Stat(cleanPath); err == nil {
- log.Printf("✅ Using config file: %s\n", cleanPath)
- return cleanPath, nil
- }
- }
-
- return "", fmt.Errorf(`no configuration file found
-
- Searched locations:
- 1. %s (当前目录)
- 2. %s (上级目录)
- 3. %s (可执行文件目录)
- 4. %s (可执行文件上级目录)
- 5. %s
- 6. %s
- 7. ./db.yaml
- 8. ./config/db.yaml
- 9. DB_CONFIG_PATH环境变量指定的路径
-
- 请确保配置文件存在,或通过环境变量指定:
- export DB_CONFIG_PATH=/your/config/path/db.yaml
- set DB_CONFIG_PATH=C:\your\config\path\db.yaml (Windows)`,
- filepath.Join(currentDir, appName+".yaml"),
- filepath.Join(filepath.Dir(currentDir), appName+".yaml"),
- filepath.Join(exeDir, appName+".yaml"),
- filepath.Join(filepath.Dir(exeDir), appName+".yaml"),
- filepath.Join(exeDir, "db.yaml"),
- filepath.Join(exeDir, "config", "db.yaml"))
- }
|