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