You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
848 B
56 lines
848 B
package config |
|
|
|
import ( |
|
"log" |
|
|
|
"github.com/spf13/viper" |
|
) |
|
|
|
type Config struct { |
|
DB DBConfig |
|
Server ServerConfig |
|
Redis RedisConfig |
|
JWT JWTConfig |
|
} |
|
|
|
type DBConfig struct { |
|
Host string |
|
Port string |
|
User string |
|
Password string |
|
Name string |
|
} |
|
|
|
type ServerConfig struct { |
|
Port string |
|
} |
|
|
|
type RedisConfig struct { |
|
Host string |
|
Port string |
|
Password string |
|
DB int |
|
} |
|
|
|
type JWTConfig struct { |
|
Secret string |
|
Expire int // 小时 |
|
} |
|
|
|
func LoadConfig() *Config { |
|
viper.SetConfigName("config") |
|
viper.SetConfigType("yaml") |
|
viper.AddConfigPath(".") |
|
viper.AutomaticEnv() |
|
|
|
if err := viper.ReadInConfig(); err != nil { |
|
log.Fatalf("Error reading config file: %v", err) |
|
} |
|
|
|
var cfg Config |
|
if err := viper.Unmarshal(&cfg); err != nil { |
|
log.Fatalf("Unable to decode into struct: %v", err) |
|
} |
|
|
|
return &cfg |
|
}
|
|
|