Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cold-bin/wire-example
wire的示例
https://github.com/cold-bin/wire-example
Last synced: 3 days ago
JSON representation
wire的示例
- Host: GitHub
- URL: https://github.com/cold-bin/wire-example
- Owner: cold-bin
- Created: 2024-01-20T11:24:07.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-01-20T11:39:21.000Z (10 months ago)
- Last Synced: 2024-01-20T12:34:54.310Z (10 months ago)
- Language: Go
- Size: 17.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
## introduce
本项目仅仅只是一个示例
使用wire实现依赖注入,避免层间依赖,既方便mock和测试,也方便维护
## 项目结构
```
.
├── cmd
│ └── di # injector
├── conf
├── dal
├── entity
├── handler
├── provider # di所需的provider
│ ├── kmq
│ ├── mysql
│ ├── redis
│ └── zap
├── router
└── service
```## 食用指南
- 每一层都需要将依赖外部组件和下一层抽象到一个结构体里,然后每层(router,handler,service,dal)都按照业务拆分成实体并封装实体方法
- 这样所有的依赖都可以通过`wire`注入,而不是直接引用外部包的变量
- `wire`提供了一些很方便的api,帮助我们省掉工厂方法的创建,诸如`wire.Struct`等
- 但是有一些组件的对象,只有我们自己提供工厂方法来创建了,例如数据库句柄等### injector
```go
//go:build wireinject
// +build wireinjectpackage di
import (
"github.com/google/wire"
"wire-example/dal"
"wire-example/handler"
"wire-example/provider/kmq"
"wire-example/provider/mysql"
"wire-example/provider/redis"
"wire-example/provider/zap"
"wire-example/router"
"wire-example/service"
)func BuildItemRouter() *router.ItemRouter {
panic(
wire.Build(
wire.Struct(new(router.ItemRouter), "*"),
wire.Struct(new(handler.ItemHandler), "*"),
wire.Struct(new(service.ItemSvc), "*"),
redis.RClient,
kmq.KmqClient,
zap.Logger,
wire.Struct(new(dal.ItemDal), "*"),
wire.Struct(new(dal.Base), "*"),
mysql.DBClient,
))
}
```### provider
自提供工厂方法的provider示例:
```go
var (
gdb *gorm.DB
once = &sync.Once{}
)// 单例模式
func DBClient() *gorm.DB {
if gdb != nil {
return gdb
}once.Do(func() {
source := fmt.Sprintf("%s:%s@(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
conf.MysqlUsername,
conf.MysqlPassword,
conf.MysqlHost,
conf.MysqlPort,
conf.MysqlDbname,
)
db_, err := gorm.Open(mysql.Open(source), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
SingularTable: true,
},
})
if err != nil {
panic(err)
}
db, err := db_.DB()
if err != nil {
panic(err)
}
db.SetMaxOpenConns(conf.MysqlMaxOpenConns)
db.SetMaxIdleConns(conf.MysqlMaxIdleConns)
db.SetConnMaxLifetime(conf.MysqlConnMaxLifeMinutes * time.Minute)
err = db_.Migrator().AutoMigrate()
if err != nil {
panic(err)
}
gdb = db_
})
return gdb
}```