/
echoutils.go
119 lines (96 loc) · 2.62 KB
/
echoutils.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
113
114
115
116
117
118
119
package echoutils
import (
"context"
"encoding/json"
"fmt"
"net/http"
"reflect"
"time"
mycontext "github.com/studygolang/studygolang/context"
"github.com/labstack/echo/v4"
"github.com/polaris1119/goutils"
"github.com/polaris1119/logger"
"github.com/polaris1119/nosql"
)
const logKey = "logger"
// GetLogger 由调用者确保 ctx 中存在 logger.Logger 对象
func GetLogger(ctx context.Context) *logger.Logger {
return ctx.Value(logKey).(*logger.Logger)
}
// 是否异步处理
func IsAsync(ctx echo.Context) bool {
return goutils.MustBool(ctx.FormValue("async"), false)
}
// WrapContext 返回一个 context.Context 实例
func WrapEchoContext(ctx echo.Context) context.Context {
return mycontext.EchoContext(ctx)
}
// WrapContext 返回一个 context.Context 实例。如果 ctx == nil,需要确保 调用 logger.PutLogger()
func WrapContext(ctx context.Context) context.Context {
var objLogger *logger.Logger
if ctx == nil {
ctx = context.Background()
objLogger = logger.GetLogger()
} else {
objLogger = GetLogger(ctx)
}
return context.WithValue(ctx, logKey, objLogger)
}
func LogFlush(ctx context.Context) {
objLogger := GetLogger(ctx)
objLogger.Flush()
logger.PutLogger(objLogger)
}
func Success(ctx echo.Context, data interface{}) error {
result := map[string]interface{}{
"code": 0,
"msg": "ok",
"data": data,
}
b, err := json.Marshal(result)
if err != nil {
return err
}
go func(b []byte) {
if cacheKey := ctx.Get(nosql.CacheKey); cacheKey != nil {
logger.Debugln("cache save:", cacheKey, "now:", time.Now())
nosql.DefaultLRUCache.CompressAndAdd(cacheKey, b, nosql.NewCacheData())
}
}(b)
if ctx.Response().Committed {
LogFlush(WrapEchoContext(ctx))
return nil
}
return ctx.JSONBlob(http.StatusOK, b)
}
func Fail(ctx echo.Context, code int, msg string) error {
if ctx.Response().Committed {
LogFlush(WrapEchoContext(ctx))
return nil
}
result := map[string]interface{}{
"code": code,
"msg": msg,
}
GetLogger(WrapEchoContext(ctx)).Errorln("operate fail:", result)
return ctx.JSON(http.StatusOK, result)
}
func AsyncResponse(ctx echo.Context, logicInstance interface{}, methodName string, args ...interface{}) error {
wrapCtx := mycontext.EchoContext(ctx)
go func() {
defer func() {
if err := recover(); err != nil {
fmt.Println("async response panic:", err)
}
}()
defer LogFlush(wrapCtx)
instance := reflect.ValueOf(logicInstance)
in := make([]reflect.Value, len(args)+1)
in[0] = reflect.ValueOf(wrapCtx)
for i, arg := range args {
in[i+1] = reflect.ValueOf(arg)
}
instance.MethodByName(methodName).Call(in)
}()
return Success(ctx, nil)
}