config_test
This commit is contained in:
		@@ -2,6 +2,7 @@ package models
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"log"
 | 
						"log"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/spf13/viper"
 | 
						"github.com/spf13/viper"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
@@ -9,9 +10,13 @@ import (
 | 
				
			|||||||
var Viper *viper.Viper
 | 
					var Viper *viper.Viper
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func init() {
 | 
					func init() {
 | 
				
			||||||
	viper.SetConfigName("config") // 设置配置文件的名字
 | 
						//如果命令行参数中有test,则使用测试环境的配置
 | 
				
			||||||
	viper.SetConfigType("yaml")   // 设置配置文件的格式
 | 
						var config_file string = "./data/config.yaml"
 | 
				
			||||||
	viper.AddConfigPath("./data") // 设置配置文件所在的路径
 | 
						if len(os.Args) > 1 && os.Args[1] == "test" {
 | 
				
			||||||
 | 
							log.Println("使用测试环境配置")
 | 
				
			||||||
 | 
							config_file = "./data/config_test.yaml"
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						viper.SetConfigFile(config_file)
 | 
				
			||||||
	if err := viper.ReadInConfig(); err != nil {
 | 
						if err := viper.ReadInConfig(); err != nil {
 | 
				
			||||||
		log.Println("读取配置文件失败", err)
 | 
							log.Println("读取配置文件失败", err)
 | 
				
			||||||
		生成配置文件()
 | 
							生成配置文件()
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user