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.
31 lines
717 B
31 lines
717 B
package utils
|
|
|
|
import (
|
|
"github.com/spf13/viper"
|
|
"log"
|
|
)
|
|
|
|
// 默认配置
|
|
func defaultConfig() {
|
|
viper.SetDefault("app.mode", "debug")
|
|
viper.SetDefault("app.addr", ":8080")
|
|
viper.SetDefault("app.log.path", "./logs")
|
|
viper.SetDefault("db.dsn", "user:password@tcp(host:port)/dbname?charset=utf8mb4&parseTime=True&loc=Local")
|
|
}
|
|
|
|
// ParseConfig 解析配置
|
|
func ParseConfig() {
|
|
// 1. 默认配置
|
|
defaultConfig()
|
|
|
|
// 2. 配置解析参数
|
|
viper.AddConfigPath(".") // 从哪些目录搜索配置文件
|
|
viper.SetConfigName("configs") // 配置文件名字
|
|
viper.SetConfigType("yaml") // 配置类型(格式)
|
|
|
|
// 3. 执行解析
|
|
if err := viper.ReadInConfig(); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|