补充配置包
This commit is contained in:
parent
868c41caa5
commit
699b3b498b
@ -7,7 +7,6 @@ import (
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/infraboard/mcube/v2/types"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@ -66,7 +65,7 @@ func (h *BookApiHandler) ListBook(ctx *gin.Context) {
|
||||
|
||||
// List<*Book>
|
||||
// *Set[T]
|
||||
types.New[*Book]()
|
||||
// types.New[*Book]()
|
||||
|
||||
// 给默认值
|
||||
pn, ps := 1, 20
|
||||
@ -96,7 +95,7 @@ func (h *BookApiHandler) ListBook(ctx *gin.Context) {
|
||||
kws := ctx.Query("keywords")
|
||||
if kws != "" {
|
||||
// where title like %kws%
|
||||
query = query.Where("title LIKE ?", "%"+kws+"%x")
|
||||
query = query.Where("title LIKE ?", "%"+kws+"%")
|
||||
}
|
||||
|
||||
// 其他过滤条件
|
||||
|
10
book/v2/application.yaml
Normal file
10
book/v2/application.yaml
Normal file
@ -0,0 +1,10 @@
|
||||
app:
|
||||
host: 127.0.0.1
|
||||
port: 8080
|
||||
mysql:
|
||||
host: 127.0.0.1
|
||||
port: 3306
|
||||
database: test
|
||||
username: "root"
|
||||
password: "123456"
|
||||
debug: true
|
20
book/v2/config/README.md
Normal file
20
book/v2/config/README.md
Normal file
@ -0,0 +1,20 @@
|
||||
# 程序的配置管理
|
||||
|
||||
|
||||
## 配置的加载
|
||||
```go
|
||||
// 用于加载配置
|
||||
config.LoadConfigFromYaml(yamlConfigFilePath)
|
||||
```
|
||||
|
||||
## 程序内部如何使用配置
|
||||
```go
|
||||
// Get Config --> ConfigObject
|
||||
config.C().MySQL.Host
|
||||
// config.ConfigObjectInstance
|
||||
```
|
||||
|
||||
## 为你的包添加单元测试
|
||||
|
||||
如何验证我们这个包的 业务逻辑是正确
|
||||
|
41
book/v2/config/config.go
Normal file
41
book/v2/config/config.go
Normal file
@ -0,0 +1,41 @@
|
||||
package config
|
||||
|
||||
func Default() *Config {
|
||||
return &Config{
|
||||
Application: &application{
|
||||
Host: "127.0.0.1",
|
||||
Port: 8080,
|
||||
},
|
||||
MySQL: &mySQL{
|
||||
Host: "127.0.0.1",
|
||||
Port: 3306,
|
||||
DB: "test",
|
||||
Username: "root",
|
||||
Password: "123456",
|
||||
Debug: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// 这歌对象就是程序配置
|
||||
// yaml, toml
|
||||
type Config struct {
|
||||
Application *application `toml:"app" yaml:"app" json:"app"`
|
||||
MySQL *mySQL `toml:"mysql" yaml:"mysql" json:"mysql"`
|
||||
}
|
||||
|
||||
// 应用服务
|
||||
type application struct {
|
||||
Host string `toml:"host" yaml:"host" json:"host" env:"HOST"`
|
||||
Port int `toml:"port" yaml:"port" json:"port" env:"PORT"`
|
||||
}
|
||||
|
||||
// db对象也是一个单列模式
|
||||
type mySQL struct {
|
||||
Host string `json:"host" yaml:"host" toml:"host" env:"DATASOURCE_HOST"`
|
||||
Port int `json:"port" yaml:"port" toml:"port" env:"DATASOURCE_PORT"`
|
||||
DB string `json:"database" yaml:"database" toml:"database" env:"DATASOURCE_DB"`
|
||||
Username string `json:"username" yaml:"username" toml:"username" env:"DATASOURCE_USERNAME"`
|
||||
Password string `json:"password" yaml:"password" toml:"password" env:"DATASOURCE_PASSWORD"`
|
||||
Debug bool `json:"debug" yaml:"debug" toml:"debug" env:"DATASOURCE_DEBUG"`
|
||||
}
|
1
book/v2/config/config_test.go
Normal file
1
book/v2/config/config_test.go
Normal file
@ -0,0 +1 @@
|
||||
package config_test
|
47
book/v2/config/load.go
Normal file
47
book/v2/config/load.go
Normal file
@ -0,0 +1,47 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/caarlos0/env/v6"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// 配置加载
|
||||
// file/env/... ---> Config
|
||||
// 全局一份
|
||||
|
||||
// config 全局变量, 通过函数对我提供访问
|
||||
var config *Config
|
||||
|
||||
func C() *Config {
|
||||
// 没有配置文件怎么办?
|
||||
// 默认配置, 方便开发者
|
||||
if config == nil {
|
||||
config = Default()
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
// 加载配置 把外部配置读到 config全局变量里面来
|
||||
// yaml 文件yaml --> conf
|
||||
func LoadConfigFromYaml(configPath string) error {
|
||||
content, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 默认值
|
||||
config = C()
|
||||
return yaml.Unmarshal(content, config)
|
||||
}
|
||||
|
||||
// 从环境变量读取配置
|
||||
// "github.com/caarlos0/env/v6"
|
||||
func LoadConfigFromEnv() error {
|
||||
config = C()
|
||||
// MYSQL_DB <---> DB
|
||||
// config.MySQL.DB = os.Getenv("MYSQL_DB")
|
||||
return env.Parse(config)
|
||||
}
|
6
go.mod
6
go.mod
@ -3,8 +3,9 @@ module 122.51.31.227/go-course/go18
|
||||
go 1.24.1
|
||||
|
||||
require (
|
||||
github.com/caarlos0/env/v6 v6.10.1
|
||||
github.com/gin-gonic/gin v1.10.0
|
||||
github.com/infraboard/mcube/v2 v2.0.53
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
gorm.io/driver/mysql v1.5.7
|
||||
gorm.io/gorm v1.26.0
|
||||
)
|
||||
@ -21,6 +22,7 @@ require (
|
||||
github.com/go-playground/validator/v10 v10.20.0 // indirect
|
||||
github.com/go-sql-driver/mysql v1.7.0 // indirect
|
||||
github.com/goccy/go-json v0.10.2 // indirect
|
||||
github.com/google/go-cmp v0.5.9 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
@ -38,6 +40,4 @@ require (
|
||||
golang.org/x/sys v0.28.0 // indirect
|
||||
golang.org/x/text v0.21.0 // indirect
|
||||
google.golang.org/protobuf v1.34.1 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
sigs.k8s.io/yaml v1.4.0 // indirect
|
||||
)
|
||||
|
6
go.sum
6
go.sum
@ -2,6 +2,8 @@ github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc
|
||||
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
|
||||
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
|
||||
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
||||
github.com/caarlos0/env/v6 v6.10.1 h1:t1mPSxNpei6M5yAeu1qtRdPAK29Nbcf/n3G7x+b3/II=
|
||||
github.com/caarlos0/env/v6 v6.10.1/go.mod h1:hvp/ryKXKipEkcuYjs9mI4bBCg+UI0Yhgm5Zu0ddvwc=
|
||||
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
|
||||
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
|
||||
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
|
||||
@ -30,8 +32,6 @@ github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MG
|
||||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
||||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/infraboard/mcube/v2 v2.0.53 h1:BXqIB7zhiB/T9v/KCQGLTcpqdQAqkD5StQLl6EGBHXM=
|
||||
github.com/infraboard/mcube/v2 v2.0.53/go.mod h1:gnr0xPPDPHvCS6JAzvdjqJ62J2+vUZTkobomjTXKsx0=
|
||||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
||||
@ -98,5 +98,3 @@ gorm.io/gorm v1.26.0 h1:9lqQVPG5aNNS6AyHdRiwScAVnXHg/L/Srzx55G5fOgs=
|
||||
gorm.io/gorm v1.26.0/go.mod h1:8Z33v652h4//uMA76KjeDH8mJXPm1QNCYrMeatR0DOE=
|
||||
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
|
||||
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
|
||||
sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=
|
||||
sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY=
|
||||
|
Loading…
x
Reference in New Issue
Block a user