Skip to content

Commit

Permalink
wire.go实现调整
Browse files Browse the repository at this point in the history
  • Loading branch information
xmgtony committed Mar 4, 2023
1 parent cefe2e8 commit a831df3
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 126 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
## 下一步计划
将本项目打造成快速开发框架,目前Java方向的快速开发框架非常多,包含完成度很高的管理后台、代码生成、组件集成等。但是Go方向相对较少,要么有但是很多按照PHP的玩法在做,因此后续规划为快速开发框架,会新起一个项目或者分支,在当前代码的基础上拓展。

### apiserver-gin

基于gin的api服务端脚手架。 gin在Go web开发中是相当受欢迎的,但是gin也是一个轻量级web框架,并不能像其他语言比如java的spring框架具有丰富的生态和标准,在实际开发中需要自己设计和添加一些额外的能力,来完善应用,比如:request_id透传,依赖注入,日志打印分割,session管理,全局错误处理,编译打包等等。
Expand All @@ -9,14 +6,18 @@

布局参考[project-layout](https://github.com/golang-standards/project-layout),该项目非Go官方标准,但是已经是行业主流。


### 理论基础

#### 清洁架构 [(Robert C. Martin)](https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html)

![image](https://user-images.githubusercontent.com/8643542/159397149-17f58fba-a3c0-4874-b49a-ae724989af59.png)

按照依赖注入,面向对象编程思想,开闭原则,可拓展,可测性等原则来规划项目。

### 更新日志

*2023-03-04:调整依赖注入wire.go实现,wire解决复杂依赖较为困难,每次对代码有破坏性更改,改为使用传参解决。*

*2022-05-27:添加新的演示功能,用户记账,用于演示脚手架多个router,handler,service的使用,详情见使用文档,升级viper版本,解决依赖安全问题。*

*2022-04-01:拆分中间件和路由,不放在一起,便于团队开发时多人维护,比如用户模块的开发人员维护user_router.go,商品模块人员维护goods_router.go互相不影响,便于拓展,详情参考cmd/wire.go*
Expand Down
5 changes: 3 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"apiserver-gin/internal/middleware"
"apiserver-gin/pkg/config"
"apiserver-gin/pkg/db"
"apiserver-gin/pkg/log"
Expand All @@ -26,6 +27,6 @@ func main() {
ds.Close()
}
})
routers := getRouters(ds)
srv.Run(routers...)
router := initRouter(ds)
srv.Run(middleware.NewMiddleware(), router)
}
30 changes: 4 additions & 26 deletions cmd/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ package main

import (
handlerV1 "apiserver-gin/internal/handler/v1"
"apiserver-gin/internal/middleware"
"apiserver-gin/internal/repo/mysql"
"apiserver-gin/internal/router"
"apiserver-gin/internal/service"
Expand All @@ -19,34 +18,13 @@ import (
"github.com/google/wire"
)

// getRouters 获取所有的router
func getRouters(ds db.IDataSource) []server.Router {
rts := make([]server.Router, 0)
// 加载中间件及路由, 注意需要先加载中间件
rts = append(rts,
middleware.NewMiddleware(),
buildUserRouter(ds),
buildAccountBillRouter(ds),
)
return rts
}

var providerSet = wire.NewSet(mysql.ProviderSet, service.ProviderSet, handlerV1.ProviderSet)

// buildUserRouter 组装UserRouter
func buildUserRouter(ds db.IDataSource) server.Router {
// initRouter 初始化router
func initRouter(ds db.IDataSource) server.Router {
wire.Build(
providerSet,
router.UserRouterProviderSet,
router.ApiRouterProviderSet,
)
return nil
}

// buildAccountBillRouter 组装AccountBillRouter
func buildAccountBillRouter(ds db.IDataSource) server.Router {
wire.Build(
providerSet,
router.AccountBillRouterProviderSet,
)
return nil
}
var providerSet = wire.NewSet(mysql.ProviderSet, service.ProviderSet, handlerV1.ProviderSet)
27 changes: 6 additions & 21 deletions cmd/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 0 additions & 34 deletions internal/router/accountbill_router.go

This file was deleted.

46 changes: 46 additions & 0 deletions internal/router/api_router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Created on 2023/3/4.
// @author tony
// email [email protected]
// description

package router

import (
"apiserver-gin/internal/handler/v1/accountbill"
"apiserver-gin/internal/handler/v1/user"
"apiserver-gin/internal/middleware"
"github.com/gin-gonic/gin"
)

type ApiRouter struct {
userHandler *user.Handler
accountBillHandler *accountbill.Handler
}

func NewApiRouter(
userHandler *user.Handler,
accountBillHandler *accountbill.Handler) *ApiRouter {
return &ApiRouter{
userHandler: userHandler,
accountBillHandler: accountBillHandler,
}
}

// Load 实现了server/http.go:40
func (ar *ApiRouter) Load(g *gin.Engine) {
// login
g.POST("/login", ar.userHandler.Login())
// user group
ug := g.Group("/v1/user", middleware.AuthToken())
{
ug.GET("", ar.userHandler.GetUserInfo())
}

// accountBill group
abg := g.Group("/v1/accountBill", middleware.AuthToken())
{
abg.GET("/list", ar.accountBillHandler.GetAccountBillList())
// login
abg.POST("", ar.accountBillHandler.AddAccountBill())
}
}
11 changes: 3 additions & 8 deletions internal/router/provider_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ import (
"github.com/google/wire"
)

var UserRouterProviderSet = wire.NewSet(
NewUserRouter,
wire.Bind(new(server.Router), new(*userRouter)),
)

var AccountBillRouterProviderSet = wire.NewSet(
NewAccountBillRouter,
wire.Bind(new(server.Router), new(*accountBillRouter)),
var ApiRouterProviderSet = wire.NewSet(
NewApiRouter,
wire.Bind(new(server.Router), new(*ApiRouter)),
)
31 changes: 0 additions & 31 deletions internal/router/user_router.go

This file was deleted.

0 comments on commit a831df3

Please sign in to comment.