Skip to content

Commit

Permalink
Share route in Gin handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
EwenQuim committed Dec 22, 2024
1 parent 2345352 commit 5715e48
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
12 changes: 6 additions & 6 deletions examples/gin-compat/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@ func fuegoControllerGet(c fuego.ContextNoBody) (HelloResponse, error) {
}, nil
}

func fuegoControllerPost(c fuego.ContextWithBody[HelloRequest]) (HelloResponse, error) {
func fuegoControllerPost(c fuego.ContextWithBody[HelloRequest]) (*HelloResponse, error) {
body, err := c.Body()
if err != nil {
return HelloResponse{}, err
return nil, err
}

if body.Word == "forbidden" {
return HelloResponse{}, fuego.BadRequestError{Title: "Forbidden word"}
return nil, fuego.BadRequestError{Title: "Forbidden word"}
}

ctx := c.Context().(*gin.Context)
fmt.Printf("%#v", ctx)
_ = c.Context().(*gin.Context) // Access to the Gin context

name := c.QueryParam("name")
_ = c.QueryParam("not-exising-param-raises-warning")

Check warning on line 35 in examples/gin-compat/handlers.go

View workflow job for this annotation

GitHub Actions / lint

"exising" should be "existing".

return HelloResponse{
return &HelloResponse{
Message: fmt.Sprintf("Hello %s, %s", body.Word, name),
}, nil
}
Expand Down
6 changes: 3 additions & 3 deletions extra/fuegogin/adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func Post[T, B any](engine *fuego.Engine, ginRouter gin.IRouter, path string, ha

func handleFuego[T, B any](engine *fuego.Engine, ginRouter gin.IRouter, method, path string, fuegoHandler func(c fuego.ContextWithBody[B]) (T, error), options ...func(*fuego.BaseRoute)) *fuego.Route[T, B] {
baseRoute := fuego.NewBaseRoute(method, path, fuegoHandler, engine.OpenAPI, options...)
return handle(engine, ginRouter, &fuego.Route[T, B]{BaseRoute: baseRoute}, GinHandler(engine, fuegoHandler))
return handle(engine, ginRouter, &fuego.Route[T, B]{BaseRoute: baseRoute}, GinHandler(engine, fuegoHandler, baseRoute))
}

func handleGin(engine *fuego.Engine, ginRouter gin.IRouter, method, path string, ginHandler gin.HandlerFunc, options ...func(*fuego.BaseRoute)) *fuego.Route[any, any] {
Expand All @@ -53,13 +53,13 @@ func handle[T, B any](engine *fuego.Engine, ginRouter gin.IRouter, route *fuego.
}

// Convert a Fuego handler to a Gin handler.
func GinHandler[B, T any](engine *fuego.Engine, handler func(c fuego.ContextWithBody[B]) (T, error)) gin.HandlerFunc {
func GinHandler[B, T any](engine *fuego.Engine, handler func(c fuego.ContextWithBody[B]) (T, error), route fuego.BaseRoute) gin.HandlerFunc {
return func(c *gin.Context) {
context := &ginContext[B]{
CommonContext: internal.CommonContext[B]{
CommonCtx: c,
UrlValues: c.Request.URL.Query(),
OpenAPIParams: map[string]fuego.OpenAPIParam{},
OpenAPIParams: route.Params,
},
ginCtx: c,
}
Expand Down

0 comments on commit 5715e48

Please sign in to comment.