Skip to content

Commit 04a5c59

Browse files
committed
Add more examples of what Fuego can do with Gin
1 parent f3b4ecb commit 04a5c59

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

examples/gin-compat/main.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
6+
"strings"
57

68
"github.com/gin-gonic/gin"
79

@@ -12,9 +14,11 @@ import (
1214
)
1315

1416
type HelloRequest struct {
15-
Word string `json:"word"`
17+
Word string `json:"word" validate:"required,min=2"`
1618
}
1719

20+
var _ fuego.InTransformer = &HelloRequest{}
21+
1822
type HelloResponse struct {
1923
Message string `json:"message"`
2024
}
@@ -38,13 +42,22 @@ func server() (*gin.Engine, *fuego.OpenAPI) {
3842
ginRouter.GET("/gin", ginController)
3943

4044
// Incrementally add OpenAPI spec
41-
// Level 1: Register Gin controller to Gin router, plugs Fuego OpenAPI route declaration
45+
// 1️⃣ Level 1: Register Gin controller to Gin router, plugs Fuego OpenAPI route declaration
4246
fuegogin.GetGin(engine, ginRouter, "/gin-with-openapi", ginController)
4347

44-
// Level 2: Register Fuego controller to Gin router. Fuego take care of serialization/deserialization, error handling, content-negotiation, etc.
48+
// 2️⃣ Level 2: Register Gin controller to Gin router, manually add options (not checked inside the Gin controller)
49+
fuegogin.GetGin(engine, ginRouter, "/gin-with-openapi-and-options", ginController,
50+
// OpenAPI options
51+
option.Summary("Gin controller with options"),
52+
option.Description("Some description"),
53+
option.OperationID("MyCustomOperationID"),
54+
option.Tags("Gin"),
55+
)
56+
57+
// 3️⃣ Level 3: Register Fuego controller to Gin router. Fuego take care of serialization/deserialization, error handling, content-negotiation, etc.
4558
fuegogin.Get(engine, ginRouter, "/fuego", fuegoControllerGet)
4659

47-
// Add some options to the POST endpoint
60+
// 4️⃣ Level 4: Add some options to the POST endpoint (checks at start-time + validations at request time)
4861
fuegogin.Post(engine, ginRouter, "/fuego-with-options", fuegoControllerPost,
4962
// OpenAPI options
5063
option.Description("Some description"),
@@ -63,6 +76,7 @@ func server() (*gin.Engine, *fuego.OpenAPI) {
6376
group := ginRouter.Group("/my-group/:id")
6477
fuegogin.Get(engine, group, "/fuego", fuegoControllerGet,
6578
option.Summary("Route with group and id"),
79+
option.Tags("Fuego"),
6680
)
6781

6882
// Serve the OpenAPI spec
@@ -71,3 +85,20 @@ func server() (*gin.Engine, *fuego.OpenAPI) {
7185

7286
return ginRouter, engine.OpenAPI
7387
}
88+
89+
func (h *HelloRequest) InTransform(ctx context.Context) error {
90+
// Transformation
91+
h.Word = strings.ToLower(h.Word)
92+
93+
// Custom validation
94+
if h.Word == "apple" {
95+
return fuego.BadRequestError{Title: "Please don't use the word 'apple'"}
96+
}
97+
98+
// Context-based transformation
99+
if user := ctx.Value("user"); user == "secret agent" {
100+
h.Word = "*****"
101+
}
102+
103+
return nil
104+
}

0 commit comments

Comments
 (0)