共用配置文件导入
This commit is contained in:
@@ -1,44 +1,9 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"log"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
import "github.com/spf13/viper"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
var config *viper.Viper
|
||||
|
||||
var (
|
||||
Root string
|
||||
Viper *viper.Viper
|
||||
)
|
||||
|
||||
func init() {
|
||||
_, b, _, _ := runtime.Caller(0)
|
||||
Root = filepath.Join(filepath.Dir(b), "..")
|
||||
config_file := filepath.Join(Root, "data", "config.yaml")
|
||||
viper.SetConfigFile(config_file)
|
||||
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.endpoint", "")
|
||||
viper.Set("oss.accessID", "")
|
||||
viper.Set("oss.accessKey", "")
|
||||
|
||||
viper.Set("video.endpoint", "")
|
||||
viper.Set("video.accessKeyID", "")
|
||||
viper.Set("video.accessKey", "")
|
||||
func InitConfig(cfg *viper.Viper) {
|
||||
config = cfg
|
||||
}
|
||||
|
Reference in New Issue
Block a user