add config.yml

This commit is contained in:
zhangchao
2024-07-24 04:19:01 +08:00
parent 82eadb8921
commit 1860e3666d
5 changed files with 120 additions and 6 deletions

View File

@@ -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)
}

26
conf/config.yml Normal file
View File

@@ -0,0 +1,26 @@
mysql:
Type: mysql
Host: 127.0.0.1
Port: 3306
User: root
Password: root
DBName: blog
Charset: utf8mb4
Prefix: gin_
# Prefix: gin_ # This line is commented out in the original .ini file
jwt:
SecretKey: "\x13\xbf\xd2 1\xce\x8b\xc1\t\xc1=\xec\x07\x93\xd4\x9e\xbco\xb0Z"
project:
StaticUrlMapPath:
- assets/static/: static/
- assets/docs/: docs/
- media/: media/
TemplateGlob: "templates/**/*"
MediaFilePath: "media/upload/"
server:
Port: 7890
ReadTimeout: 60
WriteTimeout: 60