Browse Source

fix:配置文件路径

Roc 17 hours ago
parent
commit
725df799ed
2 changed files with 39 additions and 5 deletions
  1. 15 5
      global/global.go
  2. 24 0
      utils/common.go

+ 15 - 5
global/global.go

@@ -20,16 +20,26 @@ var (
 
 const ConfigFile = "config/config_debug.yaml" //本地(测试)环境下的配置文件地址
 const ProConfigFile = "config/config.yaml"    //生产环境下的配置文件地址
+const DefaultFilePath = "config"
+const ConfigFilePath = "/home/code/config/eta_bridge/config" //生产环境下的配置文件地址
 
 func init() {
 	v := viper.New()
 
-	configFilePath := ConfigFile
+	configFilePath, _ := utils.ExistFiles(map[string][]string{
+		DefaultFilePath: {
+			ConfigFile,
+			ProConfigFile,
+		},
+		ConfigFilePath: {
+			ProConfigFile,
+		},
+	})
 
-	//如果不存在该配置文件,那么应该是线上环境,那么去寻找线上配置文件的路径
-	if !utils.FileIsExist(configFilePath) {
-		configFilePath = ProConfigFile
-	}
+	////如果不存在该配置文件,那么应该是线上环境,那么去寻找线上配置文件的路径
+	//if !utils.FileIsExist(configFilePath) {
+	//	configFilePath = ProConfigFile
+	//}
 
 	fmt.Println("configFilePath->", configFilePath)
 

+ 24 - 0
utils/common.go

@@ -1109,3 +1109,27 @@ func IsErrNoRow(err error) bool {
 	}
 	return errors.Is(err, gorm.ErrRecordNotFound)
 }
+
+func ExistFiles(configPathMap map[string][]string) (FullFileName string, err error) {
+	for filePath, fileNames := range configPathMap {
+		if FullFileName != "" {
+			return
+		}
+		files, pathErr := os.ReadDir(filePath)
+		if pathErr != nil {
+			continue
+		}
+		for _, file := range files {
+			if file.IsDir() {
+				continue
+			}
+			for _, fileName := range fileNames {
+				if file.Name() == fileName {
+					FullFileName = fmt.Sprintf("%s/%s", filePath, file.Name())
+					break
+				}
+			}
+		}
+	}
+	return
+}