-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
openapi_description.go
106 lines (80 loc) · 2.65 KB
/
openapi_description.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package fuego
const openapiDescription = `
This is the autogenerated OpenAPI documentation for your [Fuego](https://github.com/go-fuego/fuego) API.
Below is a Fuego Cheatsheet to help you get started. Don't hesitate to check the [Fuego documentation](https://go-fuego.github.io/fuego) for more details.
Happy coding! 🔥
## Usage
### Route registration
` + "```go" + `
func main() {
// Create a new server
s := fuego.NewServer()
// Register some routes
fuego.Post(s, "/hello", myController)
fuego.Get(s, "/myPath", otherController)
fuego.Put(s, "/hello", thirdController)
adminRoutes := fuego.Group(s, "/admin")
fuego.Use(adminRoutes, myMiddleware) // This middleware (for authentication, etc...) will be available for routes starting by /admin/*,
fuego.Get(adminRoutes, "/hello", groupController) // This route will be available at /admin/hello
// Start the server
s.Start()
}
` + "```" + `
### Basic controller
` + "```go" + `
type MyBody struct {
Name string ` + "`json:\"name\" validate:\"required,max=30\"`" + `
}
type MyResponse struct {
Answer string ` + "`json:\"answer\"`" + `
}
func hello(ctx fuego.ContextWithBody[MyBody]) (*MyResponse, error) {
body, err := ctx.Body()
if err != nil {
return nil, err
}
return &MyResponse{Answer: "Hello " + body.Name}, nil
}
` + "```" + `
### Add openAPI information to the route
` + "```go" + `
import (
"github.com/go-fuego/fuego"
"github.com/go-fuego/fuego/option"
"github.com/go-fuego/fuego/param"
)
func main() {
s := fuego.NewServer()
// Custom OpenAPI options
fuego.Post(s, "/", myController
option.Description("This route does something..."),
option.Summary("This is my summary"),
option.Tags("MyTag"), // A tag is set by default according to the return type (can be deactivated)
option.Deprecated(), // Marks the route as deprecated in the OpenAPI spec
option.Query("name", "Declares a query parameter with default value", param.Default("Carmack")),
option.Header("Authorization", "Bearer token", param.Required()),
optionPagination,
optionCustomBehavior,
)
s.Run()
}
var optionPagination = option.Group(
option.QueryInt("page", "Page number", param.Default(1), param.Example("1st page", 1), param.Example("42nd page", 42)),
option.QueryInt("perPage", "Number of items per page"),
)
var optionCustomBehavior = func(r *fuego.BaseRoute) {
r.XXX = "YYY"
}
` + "```" + `
Then, in the controller
` + "```go" + `
type MyResponse struct {
Answer string ` + "`json:\"answer\"`" + `
}
func getAllPets(ctx fuego.ContextNoBody) (*MyResponse, error) {
name := ctx.QueryParam("name")
perPage, _ := ctx.QueryParamIntErr("per_page")
return &MyResponse{Answer: "Hello " + name}, nil
}
` + "```" + `
`