35 lines
800 B
Go
35 lines
800 B
Go
package models
|
|
|
|
import (
|
|
"log"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
var Viper *viper.Viper
|
|
|
|
func init() {
|
|
viper.SetConfigName("config") // 设置配置文件的名字
|
|
viper.SetConfigType("yaml") // 设置配置文件的格式
|
|
viper.AddConfigPath("./data") // 设置配置文件所在的路径
|
|
if err := viper.ReadInConfig(); err != nil {
|
|
log.Println("读取配置文件失败", err)
|
|
生成配置文件()
|
|
}
|
|
Viper = viper.GetViper()
|
|
}
|
|
|
|
func 生成配置文件() {
|
|
viper.Set("mysql.host", "")
|
|
viper.Set("mysql.port", 3306)
|
|
viper.Set("mysql.user", "")
|
|
viper.Set("mysql.password", "")
|
|
viper.Set("mysql.dbname", "")
|
|
viper.Set("mysql.charset", "utf8mb4")
|
|
viper.Set("mysql.maxOpenConns", 100)
|
|
|
|
viper.Set("oss.host", "")
|
|
viper.Set("oss.accessKeyId", "")
|
|
viper.Set("oss.accessKeySecret", "")
|
|
}
|