From 82513551ad476f7da0d1d5416de3850cc5ec37fb Mon Sep 17 00:00:00 2001 From: EwenQuim Date: Tue, 17 Dec 2024 12:17:00 +0100 Subject: [PATCH] Moved /extra/fuegogin/lib to /examples/gin-compat --- examples/gin-compat/handlers.go | 47 +++++++++++++++++++ .../lib/lib.go => examples/gin-compat/main.go | 42 ++--------------- .../gin-compat/main_test.go | 2 +- extra/fuegogin/serve/main.go | 14 ------ 4 files changed, 52 insertions(+), 53 deletions(-) create mode 100644 examples/gin-compat/handlers.go rename extra/fuegogin/lib/lib.go => examples/gin-compat/main.go (59%) rename extra/fuegogin/lib/lib_test.go => examples/gin-compat/main_test.go (97%) delete mode 100644 extra/fuegogin/serve/main.go diff --git a/examples/gin-compat/handlers.go b/examples/gin-compat/handlers.go new file mode 100644 index 00000000..28de183a --- /dev/null +++ b/examples/gin-compat/handlers.go @@ -0,0 +1,47 @@ +package main + +import ( + "fmt" + "net/http" + + "github.com/gin-gonic/gin" + + "github.com/go-fuego/fuego" + "github.com/go-fuego/fuego/extra/fuegogin" +) + +func ginController(c *gin.Context) { + c.String(200, "pong") +} + +func fuegoControllerGet(c fuegogin.ContextNoBody) (HelloResponse, error) { + return HelloResponse{ + Message: "Hello", + }, nil +} + +func fuegoControllerPost(c fuegogin.ContextWithBody[HelloRequest]) (HelloResponse, error) { + body, err := c.Body() + if err != nil { + return HelloResponse{}, err + } + + name := c.QueryParam("name") + + return HelloResponse{ + Message: fmt.Sprintf("Hello %s, %s", body.Word, name), + }, nil +} + +func serveController(s *fuego.OpenAPI) func(ctx *gin.Context) { + return func(ctx *gin.Context) { + ctx.JSON(http.StatusOK, s.Description()) + } +} + +func DefaultOpenAPIHandler(specURL string) gin.HandlerFunc { + return func(ctx *gin.Context) { + ctx.Header("Content-Type", "text/html; charset=utf-8") + ctx.String(200, fuego.DefaultOpenAPIHTML(specURL)) + } +} diff --git a/extra/fuegogin/lib/lib.go b/examples/gin-compat/main.go similarity index 59% rename from extra/fuegogin/lib/lib.go rename to examples/gin-compat/main.go index 8dcb2639..25ecfb57 100644 --- a/extra/fuegogin/lib/lib.go +++ b/examples/gin-compat/main.go @@ -1,8 +1,7 @@ -package lib +package main import ( "fmt" - "net/http" "github.com/gin-gonic/gin" @@ -20,7 +19,7 @@ type HelloResponse struct { Message string `json:"message"` } -func SetupGin() (*gin.Engine, *fuego.OpenAPI) { +func main() { e := gin.Default() openapi := fuego.NewOpenAPI() @@ -48,41 +47,8 @@ func SetupGin() (*gin.Engine, *fuego.OpenAPI) { fmt.Println("OpenAPI at at http://localhost:8980/swagger") - return e, openapi -} - -func ginController(c *gin.Context) { - c.String(200, "pong") -} - -func fuegoControllerGet(c fuegogin.ContextNoBody) (HelloResponse, error) { - return HelloResponse{ - Message: "Hello", - }, nil -} - -func fuegoControllerPost(c fuegogin.ContextWithBody[HelloRequest]) (HelloResponse, error) { - body, err := c.Body() + err := e.Run(":8980") if err != nil { - return HelloResponse{}, err - } - - name := c.QueryParam("name") - - return HelloResponse{ - Message: fmt.Sprintf("Hello %s, %s", body.Word, name), - }, nil -} - -func serveController(s *fuego.OpenAPI) func(ctx *gin.Context) { - return func(ctx *gin.Context) { - ctx.JSON(http.StatusOK, s.Description()) - } -} - -func DefaultOpenAPIHandler(specURL string) gin.HandlerFunc { - return func(ctx *gin.Context) { - ctx.Header("Content-Type", "text/html; charset=utf-8") - ctx.String(200, fuego.DefaultOpenAPIHTML(specURL)) + panic(err) } } diff --git a/extra/fuegogin/lib/lib_test.go b/examples/gin-compat/main_test.go similarity index 97% rename from extra/fuegogin/lib/lib_test.go rename to examples/gin-compat/main_test.go index 5dd9cfdb..81546806 100644 --- a/extra/fuegogin/lib/lib_test.go +++ b/examples/gin-compat/main_test.go @@ -1,4 +1,4 @@ -package lib +package main import ( "net/url" diff --git a/extra/fuegogin/serve/main.go b/extra/fuegogin/serve/main.go deleted file mode 100644 index 692a8cb6..00000000 --- a/extra/fuegogin/serve/main.go +++ /dev/null @@ -1,14 +0,0 @@ -package main - -import ( - "github.com/go-fuego/fuego/extra/fuegogin/lib" -) - -func main() { - e, _ := lib.SetupGin() - - err := e.Run(":8980") - if err != nil { - panic(err) - } -}