Skip to content

Commit 6ecde1f

Browse files
committed
Replaced fuego.OpenAPI with fuego.Engine
1 parent 5ac66e9 commit 6ecde1f

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

examples/gin-compat/main.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,21 @@ func main() {
3131
}
3232

3333
func server() (*gin.Engine, *fuego.OpenAPI) {
34-
e := gin.Default()
35-
openapi := fuego.NewOpenAPI()
34+
ginRouter := gin.Default()
35+
engine := fuego.NewEngine()
3636

3737
// Register Gin controller
38-
e.GET("/gin", ginController)
38+
ginRouter.GET("/gin", ginController)
3939

4040
// Incrementally add OpenAPI spec
4141
// Level 1: Register Gin controller to Gin router, plugs Fuego OpenAPI route declaration
42-
fuegogin.GetGin(openapi, e, "/gin-with-openapi", ginController)
42+
fuegogin.GetGin(engine, ginRouter, "/gin-with-openapi", ginController)
4343

4444
// Level 2: Register Fuego controller to Gin router. Fuego take care of serialization/deserialization, error handling, content-negotiation, etc.
45-
fuegogin.Get(openapi, e, "/fuego", fuegoControllerGet)
45+
fuegogin.Get(engine, ginRouter, "/fuego", fuegoControllerGet)
4646

4747
// Add some options to the POST endpoint
48-
fuegogin.Post(openapi, e, "/fuego-with-options", fuegoControllerPost,
48+
fuegogin.Post(engine, ginRouter, "/fuego-with-options", fuegoControllerPost,
4949
// OpenAPI options
5050
option.Description("Some description"),
5151
option.OperationID("SomeOperationID"),
@@ -59,14 +59,14 @@ func server() (*gin.Engine, *fuego.OpenAPI) {
5959
)
6060

6161
// Supports groups & path parameters even for gin handlers
62-
group := e.Group("/my-group/:id")
63-
fuegogin.Get(openapi, group, "/fuego", fuegoControllerGet,
62+
group := ginRouter.Group("/my-group/:id")
63+
fuegogin.Get(engine, group, "/fuego", fuegoControllerGet,
6464
option.Summary("Route with group and id"),
6565
)
6666

6767
// Serve the OpenAPI spec
68-
e.GET("/openapi.json", serveOpenApiJSONDescription(openapi))
69-
e.GET("/swagger", DefaultOpenAPIHandler("/openapi.json"))
68+
ginRouter.GET("/openapi.json", serveOpenApiJSONDescription(engine.OpenAPI))
69+
ginRouter.GET("/swagger", DefaultOpenAPIHandler("/openapi.json"))
7070

71-
return e, openapi
71+
return ginRouter, engine.OpenAPI
7272
}

extra/fuegogin/adaptor.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,40 @@ import (
1010
"github.com/go-fuego/fuego/internal"
1111
)
1212

13-
func GetGin(s *fuego.OpenAPI, e gin.IRouter, path string, handler gin.HandlerFunc, options ...func(*fuego.BaseRoute)) *fuego.Route[any, any] {
14-
return handleGin(s, e, http.MethodGet, path, handler, options...)
13+
func GetGin(engine *fuego.Engine, ginRouter gin.IRouter, path string, handler gin.HandlerFunc, options ...func(*fuego.BaseRoute)) *fuego.Route[any, any] {
14+
return handleGin(engine, ginRouter, http.MethodGet, path, handler, options...)
1515
}
1616

17-
func PostGin(s *fuego.OpenAPI, e gin.IRouter, path string, handler gin.HandlerFunc, options ...func(*fuego.BaseRoute)) *fuego.Route[any, any] {
18-
return handleGin(s, e, http.MethodPost, path, handler, options...)
17+
func PostGin(engine *fuego.Engine, ginRouter gin.IRouter, path string, handler gin.HandlerFunc, options ...func(*fuego.BaseRoute)) *fuego.Route[any, any] {
18+
return handleGin(engine, ginRouter, http.MethodPost, path, handler, options...)
1919
}
2020

21-
func Get[T, B any](s *fuego.OpenAPI, e gin.IRouter, path string, handler func(c fuego.ContextWithBody[B]) (T, error), options ...func(*fuego.BaseRoute)) *fuego.Route[T, B] {
22-
return handleFuego(s, e, http.MethodGet, path, handler, options...)
21+
func Get[T, B any](engine *fuego.Engine, ginRouter gin.IRouter, path string, handler func(c fuego.ContextWithBody[B]) (T, error), options ...func(*fuego.BaseRoute)) *fuego.Route[T, B] {
22+
return handleFuego(engine, ginRouter, http.MethodGet, path, handler, options...)
2323
}
2424

25-
func Post[T, B any](s *fuego.OpenAPI, e gin.IRouter, path string, handler func(c fuego.ContextWithBody[B]) (T, error), options ...func(*fuego.BaseRoute)) *fuego.Route[T, B] {
26-
return handleFuego(s, e, http.MethodPost, path, handler, options...)
25+
func Post[T, B any](engine *fuego.Engine, ginRouter gin.IRouter, path string, handler func(c fuego.ContextWithBody[B]) (T, error), options ...func(*fuego.BaseRoute)) *fuego.Route[T, B] {
26+
return handleFuego(engine, ginRouter, http.MethodPost, path, handler, options...)
2727
}
2828

29-
func handleFuego[T, B any](openapi *fuego.OpenAPI, e gin.IRouter, method, path string, fuegoHandler func(c fuego.ContextWithBody[B]) (T, error), options ...func(*fuego.BaseRoute)) *fuego.Route[T, B] {
30-
baseRoute := fuego.NewBaseRoute(method, path, fuegoHandler, openapi, options...)
31-
return handle(openapi, e, &fuego.Route[T, B]{BaseRoute: baseRoute}, GinHandler(fuegoHandler))
29+
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] {
30+
baseRoute := fuego.NewBaseRoute(method, path, fuegoHandler, engine.OpenAPI, options...)
31+
return handle(engine, ginRouter, &fuego.Route[T, B]{BaseRoute: baseRoute}, GinHandler(fuegoHandler))
3232
}
3333

34-
func handleGin(openapi *fuego.OpenAPI, e gin.IRouter, method, path string, ginHandler gin.HandlerFunc, options ...func(*fuego.BaseRoute)) *fuego.Route[any, any] {
35-
baseRoute := fuego.NewBaseRoute(method, path, ginHandler, openapi, options...)
36-
return handle(openapi, e, &fuego.Route[any, any]{BaseRoute: baseRoute}, ginHandler)
34+
func handleGin(engine *fuego.Engine, ginRouter gin.IRouter, method, path string, ginHandler gin.HandlerFunc, options ...func(*fuego.BaseRoute)) *fuego.Route[any, any] {
35+
baseRoute := fuego.NewBaseRoute(method, path, ginHandler, engine.OpenAPI, options...)
36+
return handle(engine, ginRouter, &fuego.Route[any, any]{BaseRoute: baseRoute}, ginHandler)
3737
}
3838

39-
func handle[T, B any](openapi *fuego.OpenAPI, e gin.IRouter, route *fuego.Route[T, B], fuegoHandler gin.HandlerFunc) *fuego.Route[T, B] {
40-
if _, ok := e.(*gin.RouterGroup); ok {
41-
route.Path = e.(*gin.RouterGroup).BasePath() + route.Path
39+
func handle[T, B any](engine *fuego.Engine, ginRouter gin.IRouter, route *fuego.Route[T, B], fuegoHandler gin.HandlerFunc) *fuego.Route[T, B] {
40+
if _, ok := ginRouter.(*gin.RouterGroup); ok {
41+
route.Path = ginRouter.(*gin.RouterGroup).BasePath() + route.Path
4242
}
4343

44-
e.Handle(route.Method, route.Path, fuegoHandler)
44+
ginRouter.Handle(route.Method, route.Path, fuegoHandler)
4545

46-
err := route.RegisterOpenAPIOperation(openapi)
46+
err := route.RegisterOpenAPIOperation(engine.OpenAPI)
4747
if err != nil {
4848
slog.Warn("error documenting openapi operation", "error", err)
4949
}

0 commit comments

Comments
 (0)