37 lines
		
	
	
		
			943 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			37 lines
		
	
	
		
			943 B
		
	
	
	
		
			Go
		
	
	
	
	
	
| package config
 | |
| 
 | |
| import (
 | |
| 	"time"
 | |
| 
 | |
| 	"github.com/spf13/viper"
 | |
| )
 | |
| 
 | |
| type Config struct {
 | |
| 	DBDriver            string        `mapstructure:"DB_DRIVER"`
 | |
| 	DBSource            string        `mapstructure:"DB_SOURCE"`
 | |
| 	RDSource            string        `mapstructure:"RD_SOURCE"`
 | |
| 	RDPassowrd          string        `mapstructure:"RD_PWSSWORD"`
 | |
| 	RDIndex             int           `mapstructure:"RD_INDEX"`
 | |
| 	ServerAddress       string        `mapstructure:"SERVER_ADDRESS"`
 | |
| 	TokenSymmetricKey   string        `mapstructure:"TOKEN_SYMMETRIC_KEY"`
 | |
| 	AccessTokenDuration time.Duration `mapstructure:"ACCESS_TOKEN_DURATION"`
 | |
| }
 | |
| 
 | |
| func LoadConfig(path string) (*Config, error) {
 | |
| 	viper.AddConfigPath(path)
 | |
| 	viper.SetConfigName("app")
 | |
| 	viper.SetConfigType("env")
 | |
| 
 | |
| 	// 自动识别加载环境变量
 | |
| 	viper.AutomaticEnv()
 | |
| 
 | |
| 	err := viper.ReadInConfig()
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 
 | |
| 	var config Config
 | |
| 	err = viper.Unmarshal(&config)
 | |
| 	return &config, err
 | |
| }
 | 
