/
context.go
111 lines (96 loc) · 3.36 KB
/
context.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
package echop
import (
"github.com/labstack/echo/v4"
"go.uber.org/zap"
"net/http"
)
type context struct {
echo.Context
}
// Context is a wrapper of echo.Context
type Context interface {
echo.Context
Json(httpCode int, resp JSONResponse) error
// JsonSuccess is a shortcut of JsonSuccessWithCode with DefaultResponseSuccessCode
JsonSuccess(i any, msg string) error
// JsonFail is a shortcut of JsonFailWithCode with DefaultResponseFailCode
JsonFail(i any, msg string) error
// JsonSuccessWithCode is a shortcut of Json with http code is http.StatusOK and default msg is DefaultResponseSuccessMsg
JsonSuccessWithCode(code any, data any, msg string) error
// JsonFailWithCode is a shortcut of Json with http code is http.StatusOK and default msg is DefaultResponseFailMsg
JsonFailWithCode(code any, data any, msg string) error
// LogInfo is a shortcut of LogInfoWithContext
LogInfo(msg string, fields ...zap.Field)
// LogError is a shortcut of LogErrorWithContext
LogError(msg string, fields ...zap.Field)
// LogWarn is a shortcut of LogWarnWithContext
LogWarn(msg string, fields ...zap.Field)
// BindAndValidate is a shortcut of echo context Bind and Validate
BindAndValidate(payload any) error
}
// JSONResponse is a standard response format
type JSONResponse struct {
Code any `json:"code"`
Message string `json:"message"`
Data any `json:"data,omitempty"`
}
// WrapHandlerFunc wraps HandlerFunc to echo.HandlerFunc
func WrapHandlerFunc(h HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
return h(c.(Context))
}
}
func (c *context) Json(httpCode int, resp JSONResponse) error {
return c.Context.JSON(httpCode, resp)
}
// JsonSuccess is a shortcut of JsonSuccessWithCode with DefaultResponseSuccessCode
func (c *context) JsonSuccess(data any, msg string) error {
return c.JsonSuccessWithCode(DefaultResponseSuccessCode, data, msg)
}
// JsonSuccessWithCode is a shortcut of Json with http code is http.StatusOK and default msg is DefaultResponseSuccessMsg
func (c *context) JsonSuccessWithCode(code any, data any, msg string) error {
if msg == "" {
msg = DefaultResponseSuccessMsg
}
return c.Json(http.StatusOK, JSONResponse{
Code: code,
Message: msg,
Data: data,
})
}
func (c *context) JsonFail(data any, msg string) error {
return c.JsonFailWithCode(DefaultResponseFailCode, data, msg)
}
// JsonFailWithCode is a shortcut of Json with http code is http.StatusOK and default msg is DefaultResponseFailMsg
func (c *context) JsonFailWithCode(code any, data any, msg string) error {
if msg == "" {
msg = DefaultResponseFailMsg
}
return c.Json(http.StatusOK, JSONResponse{
Code: code,
Message: msg,
Data: data,
})
}
// LogInfo is a shortcut of LogInfoWithContext
func (c *context) LogInfo(msg string, fields ...zap.Field) {
LogInfoWithContext(c, msg, fields...)
}
// LogError is a shortcut of LogErrorWithContext
func (c *context) LogError(msg string, fields ...zap.Field) {
LogErrorWithContext(c, msg, fields...)
}
// LogWarn is a shortcut of LogWarnWithContext
func (c *context) LogWarn(msg string, fields ...zap.Field) {
LogWarnWithContext(c, msg, fields...)
}
// BindAndValidate is a shortcut of echo context Bind and Validate
func (c *context) BindAndValidate(payload any) error {
if err := c.Context.Bind(payload); err != nil {
return err
}
if err := c.Context.Validate(payload); err != nil {
return err
}
return nil
}