一,安装viper
1,viper的代码地址:
https://github.com/spf13/viper
查看viper版本:
https://github.com/spf13/viper/releases
2,安装:
root@ku:~
说明:刘宏缔的go森林是一个专注golang的博客, 地址:https://blog.csdn.net/weixin_43881017
说明:作者:刘宏缔 邮箱: [email protected]
二,演示项目的相关信息
1,项目地址:
https://github.com/liuhongdi/digv04
2,功能说明:
能从配置文件中读取数据库等相关的配置
3,项目结构:如图:
三,go代码说明
1,config/config.yaml
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
2,global/setting.go
-
-
-
-
-
"github.com/liuhongdi/digv04/pkg/setting"
-
-
-
-
type ServerSettingS struct {
-
-
-
ReadTimeout time.Duration
-
WriteTimeout time.Duration
-
-
-
type DatabaseSettingS struct {
-
-
-
-
-
-
-
-
-
-
-
-
-
ServerSetting *ServerSettingS
-
DatabaseSetting *DatabaseSettingS
-
-
-
-
func SetupSetting() error {
-
s, err := setting.NewSetting()
-
-
-
-
err = s.ReadSection("Database", &DatabaseSetting)
-
-
-
-
-
err = s.ReadSection("Server", &ServerSetting)
-
-
-
-
-
-
fmt.Println(ServerSetting)
-
fmt.Println(DatabaseSetting)
-
-
3,global/db.go
-
-
-
-
-
-
-
-
-
-
-
-
func SetupDBLink() (error) {
-
-
DBLink, err = gorm.Open(DatabaseSetting.DBType,
-
fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=%s&parseTime=%t&loc=Local",
-
DatabaseSetting.UserName,
-
DatabaseSetting.Password,
-
-
-
-
DatabaseSetting.ParseTime,
-
-
-
-
-
-
if ServerSetting.RunMode == "debug" {
-
-
-
DBLink.SingularTable(true)
-
DBLink.DB().SetMaxIdleConns(DatabaseSetting.MaxIdleConns)
-
DBLink.DB().SetMaxOpenConns(DatabaseSetting.MaxOpenConns)
-
-
4,pkg/setting/setting.go
-
-
-
-
-
-
-
-
-
-
-
var sections = make(map[string]interface{})
-
-
-
func NewSetting() (*Setting, error) {
-
-
vp.SetConfigName("config")
-
vp.AddConfigPath("config")
-
-
-
-
-
-
-
-
-
-
-
-
func (s *Setting) ReadSection(k string, v interface{}) error {
-
err := s.vp.UnmarshalKey(k, v)
-
-
-
-
-
if _, ok := sections[k]; !ok {
-
-
-
-
-
-
-
func (s *Setting) ReloadAllSection() error {
-
for k, v := range sections {
-
err := s.ReadSection(k, v)
-
-
-
-
-
-
-
5,main.go
-
-
-
-
"github.com/gin-gonic/gin"
-
_ "github.com/jinzhu/gorm/dialects/mysql"
-
"github.com/liuhongdi/digv04/global"
-
"github.com/liuhongdi/digv04/router"
-
-
-
-
-
-
err := global.SetupSetting()
-
-
log.Fatalf("init.setupSetting err: %v", err)
-
-
-
err = global.SetupDBLink()
-
-
log.Fatalf("init.setupDBEngine err: %v", err)
-
-
-
-
-
-
gin.SetMode(global.ServerSetting.RunMode)
-
-
-
-
r.Run(":"+global.ServerSetting.HttpPort)
-
6,其他代码可访问github获取
四,测试效果
1,访问:
http://127.0.0.1:8000/article/getone/1
返回:
说明数据库可以正常连接,端口号也因为配置文件的设置改变成了8000
五,查看库的版本:
-
module github.com/liuhongdi/digv04
-
-
-
-
-
github.com/gin-gonic/gin v1.6.3
-
github.com/go-playground/universal-translator v0.17.0
-
github.com/go-playground/validator/v10 v10.2.0
-
github.com/jinzhu/gorm v1.9.16
-
github.com/magiconair/properties v1.8.4
-
github.com/mitchellh/mapstructure v1.3.3
-
github.com/pelletier/go-toml v1.8.1
-
github.com/spf13/afero v1.4.1
-
github.com/spf13/cast v1.3.1
-
github.com/spf13/jwalterweatherman v1.1.0
-
github.com/spf13/pflag v1.0.5
-
github.com/spf13/viper v1.7.1
-
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68
-
-
-
-
|
请发表评论