/
mw.go
43 lines (36 loc) · 1.34 KB
/
mw.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
package server
import (
"fmt"
"github.com/Trinoooo/eggie_kv/consts"
"github.com/Trinoooo/eggie_kv/errs"
"github.com/Trinoooo/eggie_kv/storage/core/ragdoll/logs"
"github.com/luci/go-render/render"
"go.uber.org/zap"
)
type HandleFunc func(request *consts.KvRequest) (*consts.KvResponse, error)
type MiddlewareFunc func(handleFn HandleFunc) HandleFunc
func LogMw(handleFn HandleFunc) HandleFunc {
return func(req *consts.KvRequest) (*consts.KvResponse, error) {
logs.Info(fmt.Sprintf("req: %#v", render.Render(req)))
resp, err := handleFn(req)
logs.Info(fmt.Sprintf("resp: %#v, errs: %#v", render.Render(resp), err))
return resp, err
}
}
func ParamsValidateMw(handleFn HandleFunc) HandleFunc {
return func(req *consts.KvRequest) (*consts.KvResponse, error) {
reqKeyLength := len(req.Key)
if reqKeyLength <= 0 || reqKeyLength > consts.KB {
e := errs.NewInvalidParamErr()
logs.Error(e.Error(), zap.String(consts.LogFieldParams, "reqKeyLength"), zap.Int(consts.LogFieldValue, reqKeyLength))
return newExceptionResp(e), e
}
reqValueLength := len(req.Value)
if reqValueLength < 0 || reqValueLength > consts.MB {
e := errs.NewInvalidParamErr()
logs.Error(e.Error(), zap.String(consts.LogFieldParams, "reqValueLength"), zap.Int(consts.LogFieldValue, reqValueLength))
return newExceptionResp(e), e
}
return handleFn(req)
}
}