一,安装viper
1,viper的代码地址:
https://github.com/spf13/viper
查看viper版本:
https://github.com/spf13/viper/releases
2,安装:
root@ku:~# go get -u github.com/spf13/viper@v1.7.1
说明:刘宏缔的go森林是一个专注golang的博客,
地址:https://blog.csdn.net/weixin_43881017
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,演示项目的相关信息
1,项目地址:
https://github.com/liuhongdi/digv04
2,功能说明:
能从配置文件中读取数据库等相关的配置
3,项目结构:如图:
三,go代码说明
1,config/config.yaml
-
Database:
-
DBType: mysql
-
UserName: root
-
Password: password
-
Host: 127.0.0.1:3306
-
DBName: dig
-
Charset: utf8
-
ParseTime: True
-
MaxIdleConns: 10
-
MaxOpenConns: 30
-
Server:
-
RunMode: debug
-
HttpPort: 8000
-
ReadTimeout: 60
-
WriteTimeout: 60
2,global/setting.go
-
package global
-
-
import (
-
"fmt"
-
"github.com/liuhongdi/digv04/pkg/setting"
-
"time"
-
)
-
//服务器配置
-
type ServerSettingS struct {
-
RunMode string
-
HttpPort string
-
ReadTimeout time.Duration
-
WriteTimeout time.Duration
-
}
-
//数据库配置
-
type DatabaseSettingS struct {
-
DBType string
-
UserName string
-
Password string
-
Host string
-
DBName string
-
Charset string
-
ParseTime bool
-
MaxIdleConns int
-
MaxOpenConns int
-
}
-
//定义全局变量
-
var (
-
ServerSetting *ServerSettingS
-
DatabaseSetting *DatabaseSettingS
-
)
-
-
//读取配置到全局变量
-
func SetupSetting() error {
-
s, err := setting.NewSetting()
-
if err != nil {
-
return err
-
}
-
err = s.ReadSection("Database", &DatabaseSetting)
-
if err != nil {
-
return err
-
}
-
-
err = s.ReadSection("Server", &ServerSetting)
-
if err != nil {
-
return err
-
}
-
-
fmt.Println("setting:")
-
fmt.Println(ServerSetting)
-
fmt.Println(DatabaseSetting)
-
return nil
-
}
3,global/db.go
-
package global
-
-
import (
-
"fmt"
-
"github.com/jinzhu/gorm"
-
)
-
-
var (
-
DBLink *gorm.DB
-
)
-
//建立数据库连接,配置从配置文件中获取
-
func SetupDBLink() (error) {
-
var err 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.Host,
-
DatabaseSetting.DBName,
-
DatabaseSetting.Charset,
-
DatabaseSetting.ParseTime,
-
))
-
if err != nil {
-
return err
-
}
-
-
if ServerSetting.RunMode == "debug" {
-
DBLink.LogMode(true)
-
}
-
DBLink.SingularTable(true)
-
DBLink.DB().SetMaxIdleConns(DatabaseSetting.MaxIdleConns)
-
DBLink.DB().SetMaxOpenConns(DatabaseSetting.MaxOpenConns)
-
return nil
-
}
4,pkg/setting/setting.go
-
package setting
-
-
import (
-
"github.com/spf13/viper"
-
)
-
-
type Setting struct {
-
vp *viper.Viper
-
}
-
-
var sections = make(map[string]interface{})
-
-
//读取配置
-
func NewSetting() (*Setting, error) {
-
vp := viper.New()
-
vp.SetConfigName("config")
-
vp.AddConfigPath("config")
-
vp.SetConfigType("yaml")
-
err := vp.ReadInConfig()
-
if err != nil {
-
return nil, err
-
}
-
-
s := &Setting{vp}
-
return s, nil
-
}
-
-
//读取指定的一段
-
func (s *Setting) ReadSection(k string, v interface{}) error {
-
err := s.vp.UnmarshalKey(k, v)
-
if err != nil {
-
return err
-
}
-
-
if _, ok := sections[k]; !ok {
-
sections[k] = v
-
}
-
return nil
-
}
-
-
//重新加载
-
func (s *Setting) ReloadAllSection() error {
-
for k, v := range sections {
-
err := s.ReadSection(k, v)
-
if err != nil {
-
return err
-
}
-
}
-
-
return nil
-
}
5,main.go
-
package main
-
-
import (
-
"github.com/gin-gonic/gin"
-
_ "github.com/jinzhu/gorm/dialects/mysql"
-
"github.com/liuhongdi/digv04/global"
-
"github.com/liuhongdi/digv04/router"
-
"log"
-
)
-
-
//init
-
func init() {
-
err := global.SetupSetting()
-
if err != nil {
-
log.Fatalf("init.setupSetting err: %v", err)
-
}
-
-
err = global.SetupDBLink()
-
if err != nil {
-
log.Fatalf("init.setupDBEngine err: %v", err)
-
}
-
}
-
-
func main() {
-
//设置运行模式
-
gin.SetMode(global.ServerSetting.RunMode)
-
//引入路由
-
r := router.Router()
-
//run
-
r.Run(":"+global.ServerSetting.HttpPort)
-
}
6,其他代码可访问github获取
四,测试效果
1,访问:
http://127.0.0.1:8000/article/getone/1
返回:
说明数据库可以正常连接,端口号也因为配置文件的设置改变成了8000
五,查看库的版本:
-
module github.com/liuhongdi/digv04
-
-
go 1.15
-
-
require (
-
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 // indirect
-
github.com/mitchellh/mapstructure v1.3.3 // indirect
-
github.com/pelletier/go-toml v1.8.1 // indirect
-
github.com/spf13/afero v1.4.1 // indirect
-
github.com/spf13/cast v1.3.1 // indirect
-
github.com/spf13/jwalterweatherman v1.1.0 // indirect
-
github.com/spf13/pflag v1.0.5 // indirect
-
github.com/spf13/viper v1.7.1
-
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
-
golang.org/x/text v0.3.4 // indirect
-
gopkg.in/ini.v1 v1.62.0 // indirect
-
gopkg.in/yaml.v2 v2.3.0 // indirect
-
)