Skip to content

Commit fdb001c

Browse files
authored
add a new function router to pass gin context (#17785)
1 parent 7cdbb2a commit fdb001c

2 files changed

Lines changed: 46 additions & 38 deletions

File tree

modules/openapi-generator/src/main/resources/go-gin-server/routers.mustache

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,30 @@ type Route struct {
2121

2222
// NewRouter returns a new router.
2323
func NewRouter(handleFunctions ApiHandleFunctions) *gin.Engine {
24-
router := gin.Default()
25-
for _, route := range getRoutes(handleFunctions) {
26-
if route.HandlerFunc == nil {
27-
route.HandlerFunc = DefaultHandleFunc
28-
}
29-
switch route.Method {
30-
case http.MethodGet:
31-
router.GET(route.Pattern, route.HandlerFunc)
32-
case http.MethodPost:
33-
router.POST(route.Pattern, route.HandlerFunc)
34-
case http.MethodPut:
35-
router.PUT(route.Pattern, route.HandlerFunc)
36-
case http.MethodPatch:
37-
router.PATCH(route.Pattern, route.HandlerFunc)
38-
case http.MethodDelete:
39-
router.DELETE(route.Pattern, route.HandlerFunc)
40-
}
41-
}
24+
return NewRouterWithGinEngine(gin.Default(), handleFunctions)
25+
}
26+
27+
// NewRouter add routes to existing gin engine.
28+
func NewRouterWithGinEngine(router *gin.Engine, handleFunctions ApiHandleFunctions) *gin.Engine {
29+
for _, route := range getRoutes(handleFunctions) {
30+
if route.HandlerFunc == nil {
31+
route.HandlerFunc = DefaultHandleFunc
32+
}
33+
switch route.Method {
34+
case http.MethodGet:
35+
router.GET(route.Pattern, route.HandlerFunc)
36+
case http.MethodPost:
37+
router.POST(route.Pattern, route.HandlerFunc)
38+
case http.MethodPut:
39+
router.PUT(route.Pattern, route.HandlerFunc)
40+
case http.MethodPatch:
41+
router.PATCH(route.Pattern, route.HandlerFunc)
42+
case http.MethodDelete:
43+
router.DELETE(route.Pattern, route.HandlerFunc)
44+
}
45+
}
4246

43-
return router
47+
return router
4448
}
4549

4650
// Default handler for not yet implemented routes

samples/server/petstore/go-gin-api-server/go/routers.go

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,30 @@ type Route struct {
2929

3030
// NewRouter returns a new router.
3131
func NewRouter(handleFunctions ApiHandleFunctions) *gin.Engine {
32-
router := gin.Default()
33-
for _, route := range getRoutes(handleFunctions) {
34-
if route.HandlerFunc == nil {
35-
route.HandlerFunc = DefaultHandleFunc
36-
}
37-
switch route.Method {
38-
case http.MethodGet:
39-
router.GET(route.Pattern, route.HandlerFunc)
40-
case http.MethodPost:
41-
router.POST(route.Pattern, route.HandlerFunc)
42-
case http.MethodPut:
43-
router.PUT(route.Pattern, route.HandlerFunc)
44-
case http.MethodPatch:
45-
router.PATCH(route.Pattern, route.HandlerFunc)
46-
case http.MethodDelete:
47-
router.DELETE(route.Pattern, route.HandlerFunc)
48-
}
49-
}
32+
return NewRouterWithGinEngine(gin.Default(), handleFunctions)
33+
}
34+
35+
// NewRouter add routes to existing gin engine.
36+
func NewRouterWithGinEngine(router *gin.Engine, handleFunctions ApiHandleFunctions) *gin.Engine {
37+
for _, route := range getRoutes(handleFunctions) {
38+
if route.HandlerFunc == nil {
39+
route.HandlerFunc = DefaultHandleFunc
40+
}
41+
switch route.Method {
42+
case http.MethodGet:
43+
router.GET(route.Pattern, route.HandlerFunc)
44+
case http.MethodPost:
45+
router.POST(route.Pattern, route.HandlerFunc)
46+
case http.MethodPut:
47+
router.PUT(route.Pattern, route.HandlerFunc)
48+
case http.MethodPatch:
49+
router.PATCH(route.Pattern, route.HandlerFunc)
50+
case http.MethodDelete:
51+
router.DELETE(route.Pattern, route.HandlerFunc)
52+
}
53+
}
5054

51-
return router
55+
return router
5256
}
5357

5458
// Default handler for not yet implemented routes

0 commit comments

Comments
 (0)