add config.yml
This commit is contained in:
@@ -7,9 +7,12 @@
|
||||
package conf
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/go-ini/ini"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type SqlDataBase struct {
|
||||
@@ -64,3 +67,27 @@ func SetUp() {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func SetUp1() {
|
||||
// 设置配置文件的名称(不包括扩展名)
|
||||
viper.SetConfigName("config")
|
||||
// 设置配置文件所在的目录
|
||||
viper.AddConfigPath(".")
|
||||
// 设置配置文件的类型为YAML
|
||||
viper.SetConfigType("yaml")
|
||||
|
||||
// 读取配置文件
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
log.Fatalf("Error reading config file, %s", err)
|
||||
}
|
||||
|
||||
// 获取配置值
|
||||
mysqlHost := viper.GetString("mysql.Host")
|
||||
jwtSecretKey := viper.GetString("jwt.SecretKey")
|
||||
serverPort := viper.GetInt("server.Port")
|
||||
|
||||
// 打印获取到的配置值
|
||||
fmt.Printf("MySQL Host: %s\n", mysqlHost)
|
||||
fmt.Printf("JWT Secret Key: %s\n", jwtSecretKey)
|
||||
fmt.Printf("Server Port: %d\n", serverPort)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user