Skip to content

Commit

Permalink
add a new function router to pass gin context (OpenAPITools#17785)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfatihercik authored and kota65535 committed Feb 23, 2024
1 parent b1cdc2a commit 8b8687f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,30 @@ type Route struct {

// NewRouter returns a new router.
func NewRouter(handleFunctions ApiHandleFunctions) *gin.Engine {
router := gin.Default()
for _, route := range getRoutes(handleFunctions) {
if route.HandlerFunc == nil {
route.HandlerFunc = DefaultHandleFunc
}
switch route.Method {
case http.MethodGet:
router.GET(route.Pattern, route.HandlerFunc)
case http.MethodPost:
router.POST(route.Pattern, route.HandlerFunc)
case http.MethodPut:
router.PUT(route.Pattern, route.HandlerFunc)
case http.MethodPatch:
router.PATCH(route.Pattern, route.HandlerFunc)
case http.MethodDelete:
router.DELETE(route.Pattern, route.HandlerFunc)
}
}
return NewRouterWithGinEngine(gin.Default(), handleFunctions)
}

// NewRouter add routes to existing gin engine.
func NewRouterWithGinEngine(router *gin.Engine, handleFunctions ApiHandleFunctions) *gin.Engine {
for _, route := range getRoutes(handleFunctions) {
if route.HandlerFunc == nil {
route.HandlerFunc = DefaultHandleFunc
}
switch route.Method {
case http.MethodGet:
router.GET(route.Pattern, route.HandlerFunc)
case http.MethodPost:
router.POST(route.Pattern, route.HandlerFunc)
case http.MethodPut:
router.PUT(route.Pattern, route.HandlerFunc)
case http.MethodPatch:
router.PATCH(route.Pattern, route.HandlerFunc)
case http.MethodDelete:
router.DELETE(route.Pattern, route.HandlerFunc)
}
}

return router
return router
}

// Default handler for not yet implemented routes
Expand Down
42 changes: 23 additions & 19 deletions samples/server/petstore/go-gin-api-server/go/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,30 @@ type Route struct {

// NewRouter returns a new router.
func NewRouter(handleFunctions ApiHandleFunctions) *gin.Engine {
router := gin.Default()
for _, route := range getRoutes(handleFunctions) {
if route.HandlerFunc == nil {
route.HandlerFunc = DefaultHandleFunc
}
switch route.Method {
case http.MethodGet:
router.GET(route.Pattern, route.HandlerFunc)
case http.MethodPost:
router.POST(route.Pattern, route.HandlerFunc)
case http.MethodPut:
router.PUT(route.Pattern, route.HandlerFunc)
case http.MethodPatch:
router.PATCH(route.Pattern, route.HandlerFunc)
case http.MethodDelete:
router.DELETE(route.Pattern, route.HandlerFunc)
}
}
return NewRouterWithGinEngine(gin.Default(), handleFunctions)
}

// NewRouter add routes to existing gin engine.
func NewRouterWithGinEngine(router *gin.Engine, handleFunctions ApiHandleFunctions) *gin.Engine {
for _, route := range getRoutes(handleFunctions) {
if route.HandlerFunc == nil {
route.HandlerFunc = DefaultHandleFunc
}
switch route.Method {
case http.MethodGet:
router.GET(route.Pattern, route.HandlerFunc)
case http.MethodPost:
router.POST(route.Pattern, route.HandlerFunc)
case http.MethodPut:
router.PUT(route.Pattern, route.HandlerFunc)
case http.MethodPatch:
router.PATCH(route.Pattern, route.HandlerFunc)
case http.MethodDelete:
router.DELETE(route.Pattern, route.HandlerFunc)
}
}

return router
return router
}

// Default handler for not yet implemented routes
Expand Down

0 comments on commit 8b8687f

Please sign in to comment.