-
Notifications
You must be signed in to change notification settings - Fork 37
/
middleware.go
61 lines (48 loc) · 1.56 KB
/
middleware.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
package ginrpc
import (
"context"
"fmt"
"time"
"github.com/xxjwxc/public/message"
"github.com/xxjwxc/public/mylog"
"github.com/gin-gonic/gin"
)
// GinBeforeAfterInfo 对象调用前后执行中间件参数
type GinBeforeAfterInfo struct {
C *gin.Context
FuncName string // 函数名
Req interface{} // 调用前的请求参数
Resp interface{} // 调用后的返回参数
Error error
// Other options for implementations of the interface
// can be stored in a context
Context context.Context // 占位参数,可用于存储其他参数,前后连接可用
}
// GinBeforeAfter 对象调用前后执行中间件(支持总的跟对象单独添加)
type GinBeforeAfter interface {
GinBefore(req *GinBeforeAfterInfo) bool
GinAfter(req *GinBeforeAfterInfo) bool
}
// DefaultGinBeforeAfter 创建一个默认 BeforeAfter Middleware
type DefaultGinBeforeAfter struct {
}
type timeTrace struct{}
// GinBefore call之前调用
func (d *DefaultGinBeforeAfter) GinBefore(req *GinBeforeAfterInfo) bool {
req.Context = context.WithValue(req.Context, timeTrace{}, time.Now())
return true
}
// GinAfter call之后调用
func (d *DefaultGinBeforeAfter) GinAfter(req *GinBeforeAfterInfo) bool {
begin := (req.Context.Value(timeTrace{})).(time.Time)
now := time.Now()
mylog.Info(fmt.Sprintf("[middleware] call[%v] [%v]", req.FuncName, now.Sub(begin)))
msg := message.GetSuccessMsg()
if req.Error != nil {
msg = message.GetErrorStrMsg(req.Error.Error())
}
msg.Data = req.Resp
req.Resp = msg // 设置resp 结果
return true
}
// ----------------end