forked from deepmap/oapi-codegen
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use gin.IRouter interface instead of *gin.Engine Switch custom middleware code to gin middleware Original changes: deepmap#530
- Loading branch information
mrmelphin
committed
Apr 6, 2022
1 parent
71a6fca
commit 56a2a94
Showing
2 changed files
with
10 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,28 @@ | ||
// GinServerOptions provides options for the Gin server. | ||
type GinServerOptions struct { | ||
BaseURL string | ||
Middlewares []MiddlewareFunc | ||
Middlewares []gin.HandlerFunc | ||
} | ||
|
||
// keeping for backward compatibility | ||
type MiddlewareFunc = gin.HandlerFunc | ||
|
||
// RegisterHandlers creates http.Handler with routing matching OpenAPI spec. | ||
func RegisterHandlers(router *gin.Engine, si ServerInterface) *gin.Engine { | ||
func RegisterHandlers(router gin.IRouter, si ServerInterface) gin.IRouter { | ||
return RegisterHandlersWithOptions(router, si, GinServerOptions{}) | ||
} | ||
|
||
// RegisterHandlersWithOptions creates http.Handler with additional options | ||
func RegisterHandlersWithOptions(router *gin.Engine, si ServerInterface, options GinServerOptions) *gin.Engine { | ||
func RegisterHandlersWithOptions(router gin.IRouter, si ServerInterface, options GinServerOptions) gin.IRouter { | ||
{{if .}}wrapper := ServerInterfaceWrapper{ | ||
Handler: si, | ||
HandlerMiddlewares: options.Middlewares, | ||
} | ||
{{end}} | ||
|
||
router = router.Group(options.BaseURL, options.Middlewares...) | ||
|
||
{{range .}} | ||
router.{{.Method }}(options.BaseURL+"{{.Path | swaggerUriToGinUri }}", wrapper.{{.OperationId}}) | ||
router.{{.Method }}("{{.Path | swaggerUriToGinUri }}", wrapper.{{.OperationId}}) | ||
{{end}} | ||
return router | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters