-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
99 lines (83 loc) · 2.49 KB
/
main.go
File metadata and controls
99 lines (83 loc) · 2.49 KB
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
package main
import (
"fmt"
"net/http"
"github.com/go-playground/validator/v10"
"github.com/labstack/echo/v4"
"github.com/richjyoung/echopen"
v310 "github.com/richjyoung/echopen/openapi/v3.1.0"
)
const Description = `Validation example`
type Request struct {
StringLen string `json:"string_len,omitempty" validate:"max=10,min=1"`
NumRange int `json:"num_range,omitempty" validate:"lte=10,gte=1"`
}
type Response struct {
StringLen string `json:"string_len,omitempty"`
NumRange int `json:"num_range,omitempty"`
}
type Error struct {
Code string `json:"code,omitempty"`
Message string `json:"message,omitempty"`
}
func main() {
// Create a new echOpen wrapper
api := echopen.New(
"Validation",
"1.0.0",
echopen.WithSpecDescription(Description),
echopen.WithSpecLicense(&v310.License{Name: "MIT", URL: "https://example.com/license"}),
)
// Add a global error handler to catch validation errors
api.SetErrorHandler(onError)
// Validate body route
api.POST(
"/validate",
validate,
echopen.WithRequestBodyStruct(echo.MIMEApplicationJSON, "Request parameters", Request{}),
echopen.WithResponseStruct(fmt.Sprint(http.StatusOK), "Successful response", Response{}),
echopen.WithResponseStruct("default", "Error response", Error{}),
)
// Serve the generated schema
api.ServeYAMLSpec("/openapi.yml")
api.ServeSwaggerUI("/", "/openapi.yml", "5.10.3")
// Write the full generated spec
api.WriteYAMLSpec("openapi_out.yml")
// Start the server
api.Start("localhost:3000")
}
func validate(c echo.Context) error {
body := c.Get("body").(*Request)
return c.JSON(http.StatusOK, &Response{
StringLen: body.StringLen,
NumRange: body.NumRange,
})
}
func onError(err error, c echo.Context) {
var err2 error
if ve, ok := err.(validator.ValidationErrors); ok {
// Validation error - send a 400 with the first error
err2 = c.JSON(http.StatusBadRequest, Error{
Code: "bad_request",
Message: ve[0].Error(),
})
} else if he, ok := err.(*echo.HTTPError); ok {
// Echo builtin HTTP error - send the code with the provided message
err2 = c.JSON(he.Code, struct {
Message interface{} `json:"message,omitempty"`
Stack string `json:"stack,omitempty"`
}{
Message: he.Message,
})
} else {
// Unknown error - send a 500 with the message
err2 = c.JSON(http.StatusInternalServerError, Error{
Code: "internal_server_error",
Message: err.Error(),
})
}
if err2 != nil {
// Something went wrong handling the error, all we can do is panic
panic(err2)
}
}