Skip to content

Commit 5b373d1

Browse files
committed
router group
1 parent 454bc15 commit 5b373d1

File tree

11 files changed

+91
-32
lines changed

11 files changed

+91
-32
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ clean:
176176
@rm -vrf cover.out
177177
@rm -vrf main.go serverNameExample_mixExample.gv
178178
@rm -vrf internal/ecode/*go.gen.*
179+
@rm -vrf internal/routers/*go.gen.*
179180
@rm -vrf internal/handller/*go.gen.*
180181
@rm -vrf internal/service/*go.gen.*
181182
@rm -rf serverNameExample-binary.tar.gz

api/serverNameExample/v1/userExample_router.pb.go

Lines changed: 26 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/readme-cn.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ make run
7070

7171
<br>
7272

73-
💡 如果不想使用ui界面,可以使用sponge命令行生成代码,命令行帮组信息里面有丰富的示例,有一些生成代码命令比使用UI界面更便捷。
73+
💡 如果不想使用ui界面,可以使用sponge命令行生成代码,命令行帮助信息里面有丰富的示例,有一些生成代码命令比使用UI界面更便捷。
7474

7575
<br>
7676

cmd/protoc-gen-go-gin/internal/generate/handler/template.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,17 @@ import (
9090
)
9191
9292
func init() {
93-
rootRouterFns = append(rootRouterFns, func(r *gin.Engine) {
93+
apiV1RouterFns = append(apiV1RouterFns, func(prePath string, group *gin.RouterGroup) {
9494
{{- range .PbServices}}
95-
{{.LowerName}}Router(r, handler.New{{.Name}}Handler())
95+
{{.LowerName}}Router(prePath, group, handler.New{{.Name}}Handler())
9696
{{- end}}
9797
})
9898
}
9999
100100
{{- range .PbServices}}
101101
102-
func {{.LowerName}}Router(r *gin.Engine, iService serverNameExampleV1.{{.Name}}Logicer) {
103-
serverNameExampleV1.Register{{.Name}}Router(r, iService,
102+
func {{.LowerName}}Router(prePath string, group *gin.RouterGroup, iService serverNameExampleV1.{{.Name}}Logicer) {
103+
serverNameExampleV1.Register{{.Name}}Router(prePath, group, iService,
104104
serverNameExampleV1.With{{.Name}}RPCResponse(),
105105
serverNameExampleV1.With{{.Name}}Logger(logger.Get()),
106106
)

cmd/protoc-gen-go-gin/internal/generate/router/template.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func With{{$.Name}}Logger(zapLog *zap.Logger) {{$.Name}}Option {
5858
}
5959
}
6060
61-
func Register{{$.Name}}Router(iRouter gin.IRouter, iLogic {{$.Name}}Logicer, opts ...{{$.Name}}Option) {
61+
func Register{{$.Name}}Router(prePath string, iRouter gin.IRouter, iLogic {{$.Name}}Logicer, opts ...{{$.Name}}Option) {
6262
o := &{{$.LowerName}}Options{}
6363
o.apply(opts...)
6464
@@ -70,6 +70,7 @@ func Register{{$.Name}}Router(iRouter gin.IRouter, iLogic {{$.Name}}Logicer, opt
7070
}
7171
7272
r := &{{$.LowerName}}Router {
73+
prePath: prePath,
7374
iRouter: iRouter,
7475
iLogic: iLogic,
7576
iResponse: o.responser,
@@ -79,17 +80,36 @@ func Register{{$.Name}}Router(iRouter gin.IRouter, iLogic {{$.Name}}Logicer, opt
7980
}
8081
8182
type {{$.LowerName}}Router struct {
83+
prePath string
8284
iRouter gin.IRouter
8385
iLogic {{$.Name}}Logicer
8486
iResponse errcode.Responser
8587
zapLog *zap.Logger
8688
}
8789
8890
func (r *{{$.LowerName}}Router) register() {
89-
{{range .Methods}}r.iRouter.Handle("{{.Method}}", "{{.Path}}", r.{{ .HandlerName }})
91+
{{range .Methods}}r.iRouter.Handle("{{.Method}}", r.cutPrePath("{{.Path}}"), r.{{ .HandlerName }})
9092
{{end}}
9193
}
9294
95+
func (r *{{$.LowerName}}Router) cutPrePath(path string)string{
96+
if r.prePath==""||r.prePath=="/"{
97+
return path
98+
}
99+
size:=len(r.prePath)
100+
if len(path)<=size{
101+
return path
102+
}
103+
if r.prePath==path[:size]{
104+
cutPath:=path[size:]
105+
if cutPath[0]!='/'{
106+
cutPath="/"+cutPath
107+
}
108+
return cutPath
109+
}
110+
return path
111+
}
112+
93113
{{range .Methods}}
94114
func (r *{{$.LowerName}}Router) {{ .HandlerName }} (c *gin.Context) {
95115
req := &{{.Request}}{}

cmd/protoc-gen-go-gin/internal/generate/service/template.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,17 @@ import (
8383
)
8484
8585
func init() {
86-
rootRouterFns = append(rootRouterFns, func(r *gin.Engine) {
86+
apiV1RouterFns = append(apiV1RouterFns, func(prePath string, group *gin.RouterGroup) {
8787
{{- range .PbServices}}
88-
{{.LowerName}}Router(r, service.New{{.Name}}Client())
88+
{{.LowerName}}Router(prePath, group, service.New{{.Name}}Client())
8989
{{- end}}
9090
})
9191
}
9292
9393
{{- range .PbServices}}
9494
95-
func {{.LowerName}}Router(r *gin.Engine, iService serverNameExampleV1.{{.Name}}Logicer) {
96-
serverNameExampleV1.Register{{.Name}}Router(r, iService,
95+
func {{.LowerName}}Router(prePath string, group *gin.RouterGroup, iService serverNameExampleV1.{{.Name}}Logicer) {
96+
serverNameExampleV1.Register{{.Name}}Router(prePath, group, iService,
9797
serverNameExampleV1.With{{.Name}}RPCResponse(),
9898
serverNameExampleV1.With{{.Name}}Logger(logger.Get()),
9999
)

cmd/protoc-gen-go-gin/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func saveHandlerAndRouterFiles(f *protogen.File, moduleName string, serverName s
134134
}
135135

136136
filePath = filenamePrefix + "_handler.pb.go"
137-
err = saveFile(moduleName, serverName, routerOut, filePath, routerContent, true, handlerPlugin)
137+
err = saveFile(moduleName, serverName, routerOut, filePath, routerContent, false, handlerPlugin)
138138
if err != nil {
139139
return err
140140
}
@@ -160,7 +160,7 @@ func saveServiceAndRouterFiles(gen *protogen.Plugin, f *protogen.File, moduleNam
160160
}
161161

162162
filePath = filenamePrefix + "_service.pb.go"
163-
err = saveFile(moduleName, serverName, routerOut, filePath, routerContent, true, servicePlugin)
163+
err = saveFile(moduleName, serverName, routerOut, filePath, routerContent, false, servicePlugin)
164164
if err != nil {
165165
return err
166166
}

internal/routers/routers.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ import (
2020
)
2121

2222
var (
23-
apiV1RouterFns []func(*gin.RouterGroup) // router functions
23+
apiV1RouterFns []func(*gin.RouterGroup) // group router functions
2424
// if you have other group routes you can define them here
25+
// example:
26+
// myPrePathRouterFns []func(prePath string, engine *gin.RouterGroup)
2527
)
2628

2729
// NewRouter create a new router
@@ -80,9 +82,11 @@ func NewRouter() *gin.Engine {
8082
// access path /swagger/index.html
8183
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
8284

83-
// register routers
85+
// register routers, middleware support
8486
registerRouters(r, "/api/v1", apiV1RouterFns)
8587
// if you have other group routes you can add them here
88+
// example:
89+
// registerRouters(r, "/myGroupPath", myGroupPathRouterFns, middleware.Auth())
8690

8791
return r
8892
}

internal/routers/routers_pbExample.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ import (
1818
"github.com/gin-gonic/gin/binding"
1919
)
2020

21+
// nolint
2122
var (
22-
rootRouterFns []func(engine *gin.Engine) // root routing group, used by rpc gateway
23+
apiV1RouterFns_pbExample []func(prePath string, engine *gin.RouterGroup) // group router functions
24+
// if you have other group routes you can define them here
25+
// example:
26+
// myPrePathRouterFns []func(prePath string, engine *gin.RouterGroup)
2327
)
2428

2529
// NewRouter_pbExample create a new router
@@ -76,10 +80,20 @@ func NewRouter_pbExample() *gin.Engine { //nolint
7680
// access path /apis/swagger/index.html
7781
swagger.CustomRouter(r, "apis", docs.ApiDocs)
7882

79-
// registration/Prefix Routing Groups
80-
for _, fn := range rootRouterFns {
81-
fn(r)
82-
}
83+
// register routers, middleware support
84+
registerRouters_pbExample(r, "/api/v1", apiV1RouterFns_pbExample)
85+
// if you have other group routes you can add them here
86+
// example:
87+
// registerRouters(r, "/myPrePath", myPrePathRouterFns, middleware.Auth())
8388

8489
return r
8590
}
91+
92+
// nolint
93+
func registerRouters_pbExample(r *gin.Engine, prePath string,
94+
routerFns []func(prePath string, engine *gin.RouterGroup), handlers ...gin.HandlerFunc) {
95+
rg := r.Group(prePath, handlers...)
96+
for _, fn := range routerFns {
97+
fn(prePath, rg)
98+
}
99+
}

internal/routers/routers_pbExample_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ func TestNewRouter_pbExample(t *testing.T) {
3838
func Test_userExampleServiceRouter(t *testing.T) {
3939
gin.SetMode(gin.ReleaseMode)
4040
r := gin.Default()
41-
userExampleServiceRouter(r, &mockGw{})
41+
rg := r.Group("")
42+
userExampleServiceRouter("", rg, &mockGw{})
4243
}
4344

4445
type mockGw struct{}

0 commit comments

Comments
 (0)