-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathecho.go
112 lines (94 loc) · 3.63 KB
/
echo.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
107
108
109
110
111
112
package utils
import (
"errors"
"net/http"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
// Echo utility instance
var Echo EchoUtil
// EchoBinder utility instance
var EchoBinder EchoBinderWithValidation
// EchoUtil is a utility struct for working with Echo instances
type EchoUtil struct{}
// EchoValidator is a struct that implements the echo.Validator interface.
type EchoValidator struct{}
// Validate is a method that validates the given struct using the ValidateStruct
// function and returns an error if validation fails.
func (EchoValidator) Validate(i any) error {
return ValidateStruct(i)
}
// EchoBinderWithValidation is a struct that implements the echo.Binder interface
// with added validation functionality.
type EchoBinderWithValidation struct {
echo.DefaultBinder
}
// validateWithErrorHandling validates a given struct and error handler
func (b *EchoBinderWithValidation) validateWithErrorHandling(i any, err error) error {
if err != nil {
return errors.New(err.(*echo.HTTPError).Message.(string))
}
return ValidateStruct(i)
}
// Bind binds request data, validates it using ValidateStruct(),
// and returns an error if binding or validation fails.
func (b *EchoBinderWithValidation) Bind(i any, c echo.Context) error {
err := b.DefaultBinder.Bind(i, c)
return b.validateWithErrorHandling(i, err)
}
// BindBody binds body data, validates it using ValidateStruct(),
// and returns an error if binding or validation fails.
func (b *EchoBinderWithValidation) BindBody(c echo.Context, i any) error {
err := b.DefaultBinder.BindBody(c, i)
return b.validateWithErrorHandling(i, err)
}
// BindHeaders binds headers data, validates it using ValidateStruct(),
// and returns an error if binding or validation fails.
func (b *EchoBinderWithValidation) BindHeaders(c echo.Context, i any) error {
err := b.DefaultBinder.BindHeaders(c, i)
return b.validateWithErrorHandling(i, err)
}
// BindPathParams binds path params, validates them using ValidateStruct(),
// and returns an error if binding or validation fails.
func (b *EchoBinderWithValidation) BindPathParams(c echo.Context, i any) error {
err := b.DefaultBinder.BindPathParams(c, i)
return b.validateWithErrorHandling(i, err)
}
// BindQueryParams binds query params, validates them using ValidateStruct(),
// and returns an error if binding or validation fails.
func (b *EchoBinderWithValidation) BindQueryParams(c echo.Context, i any) error {
err := b.DefaultBinder.BindQueryParams(c, i)
return b.validateWithErrorHandling(i, err)
}
// BindAll binds all request data, validates it using ValidateStruct(),
func (b *EchoBinderWithValidation) BindAll(i any, c echo.Context) error {
if err := b.DefaultBinder.BindPathParams(c, i); err != nil {
return b.validateWithErrorHandling(i, err)
}
if err := b.DefaultBinder.BindQueryParams(c, i); err != nil {
return b.validateWithErrorHandling(i, err)
}
err := b.DefaultBinder.BindBody(c, i)
return b.validateWithErrorHandling(i, err)
}
// DefaultRootHandler handles requests to the root endpoint
func (EchoUtil) DefaultRootHandler(c echo.Context) error {
return c.JSON(http.StatusOK, echo.Map{"message": "200 OK"})
}
// NoContentHandler handles return no content endpoint
func (EchoUtil) NoContentHandler(c echo.Context) error {
return c.NoContent(http.StatusNoContent)
}
// New creates a new instance of the Echo framework
func (EchoUtil) New() *echo.Echo {
e := echo.New()
e.HidePort = true
e.HideBanner = true
e.Validator = new(EchoValidator)
e.Binder = &EchoBinder
e.Pre(middleware.RemoveTrailingSlash())
e.Use(middleware.Gzip())
e.GET("/", Echo.DefaultRootHandler)
e.GET("/favicon.ico", Echo.NoContentHandler)
return e
}