-
Notifications
You must be signed in to change notification settings - Fork 2
/
context_func.go
82 lines (71 loc) · 2.27 KB
/
context_func.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
package apihandler
import (
"context"
"net/http"
"github.com/pkg/errors"
)
type contextKey string
type httpRequestAndResponseWriter struct {
req *http.Request
w http.ResponseWriter
}
var (
httpRequestAndResponseWriterKey contextKey = "httpRequestAndResponseWriter"
CONTEXT_NOT_FOUND_KEY = errors.New("not found key")
CONTEXT_NOT_EXCEPT = errors.New("not except type")
)
//SetHttpRequestAndResponseWriter 记录http 请求上下文
func SetHttpRequestAndResponseWriter(api ApiInterface, req *http.Request, w http.ResponseWriter) {
ctx := api.GetContext()
if ctx == nil {
ctx = context.Background()
}
value := &httpRequestAndResponseWriter{
req: req,
w: w,
}
ctx = context.WithValue(ctx, httpRequestAndResponseWriterKey, value)
api.SetContext(ctx)
}
func GetHttpRequestAndResponseWriter(apiInterface ApiInterface) (req *http.Request, w http.ResponseWriter, err error) {
value := apiInterface.GetContext().Value(httpRequestAndResponseWriterKey)
if value == nil {
err = errors.WithMessagef(CONTEXT_NOT_FOUND_KEY, "key:%s", httpRequestAndResponseWriterKey)
return nil, nil, err
}
_httpRequestAndResponseWriter, ok := value.(*httpRequestAndResponseWriter)
if !ok {
err = errors.WithMessagef(CONTEXT_NOT_EXCEPT, "except:*httpRequestAndResponseWriter,got:%T", value)
return nil, nil, err
}
return _httpRequestAndResponseWriter.req, _httpRequestAndResponseWriter.w, nil
}
type ApiType string
const (
API_TYPE_QUERY ApiType = "query"
API_TYPE_COMMAND ApiType = "command"
_ApiTypeContentKey contextKey = "apiType"
)
//SetAPIType 记录 api 类型到上下文
func SetAPIType(api ApiInterface, apiType ApiType) {
ctx := api.GetContext()
if ctx == nil {
ctx = context.Background()
}
ctx = context.WithValue(ctx, _ApiTypeContentKey, apiType)
api.SetContext(ctx)
}
//GetApiType 从上下文中获取 apiType
func GetApiType(apiInterface ApiInterface) (apiType ApiType) {
value := apiInterface.GetContext().Value(_ApiTypeContentKey)
apiType = value.(ApiType)
return apiType
}
//ApiTypeIsQuery 判断是否为查询类型api
func ApiTypeIsQuery(apiType ApiType)bool{
return apiType == API_TYPE_QUERY
}
//ApiTypeIsCommand 判断是否为命令类型api
func ApiTypeIsCommand(apiType ApiType)bool{
return apiType == API_TYPE_COMMAND
}