-
Notifications
You must be signed in to change notification settings - Fork 1
/
context.go
79 lines (67 loc) · 1.95 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
package gors
import (
"context"
"github.com/gin-gonic/gin"
"net/http"
)
type key struct{}
// NewContext 向context注入gin.Context
func NewContext(c *gin.Context, rpcMethodName string) context.Context {
ctx := c.Request.Context()
ctx = context.WithValue(ctx, rpcMethodKey{}, rpcMethodName)
ctx = context.WithValue(ctx, key{}, c)
c.Request = c.Request.WithContext(ctx)
return c.Request.Context()
}
// FromContext 从context获取gin.Contenxt
func FromContext(ctx context.Context) *gin.Context {
v, _ := ctx.Value(key{}).(*gin.Context)
return v
}
type rpcMethodKey struct{}
// RPCMethod returns the method string for the server context. The returned
// string is in the format of "/package.Service/method".
func RPCMethod(ctx context.Context) (string, bool) {
method, ok := ctx.Value(rpcMethodKey{}).(string)
return method, ok
}
// SetStatusCode 向context设置status code
func SetStatusCode(ctx context.Context, code int) {
FromContext(ctx).Set("HTTP_STATUS_CODE", code)
}
// StatusCode 从context获取status code
func StatusCode(ctx context.Context) int {
code, exists := FromContext(ctx).Get("HTTP_STATUS_CODE")
if !exists {
return http.StatusOK
}
return code.(int)
}
// SetHeader 向context设置header
func SetHeader(ctx context.Context, header http.Header) {
FromContext(ctx).Set("HTTP_HEADER", header)
}
// Header 从context获取header
func Header(ctx context.Context) http.Header {
c := FromContext(ctx)
header, exists := c.Get("HTTP_HEADER")
if !exists {
header = http.Header{}
c.Set("HTTP_HEADER", header)
}
return header.(http.Header)
}
// SetTrailer 向context设置trailer
func SetTrailer(ctx context.Context, trailer http.Header) {
FromContext(ctx).Set("HTTP_TRAILER", trailer)
}
// Trailer 从context获取trailer
func Trailer(ctx context.Context) http.Header {
c := FromContext(ctx)
trailer, exists := c.Get("HTTP_TRAILER")
if !exists {
trailer = http.Header{}
c.Set("HTTP_TRAILER", trailer)
}
return trailer.(http.Header)
}