-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
56 lines (47 loc) · 1.3 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
package gors
import (
"context"
"net/http"
"github.com/gin-gonic/gin"
)
type key struct{}
// NewContext 向context注入gin.Contenxt
func NewContext(c *gin.Context) context.Context {
ctx := c.Request.Context()
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
}
// SetCodeToContext 向context设置status code
func SetCodeToContext(ctx context.Context, code int) {
c, _ := ctx.Value(key{}).(*gin.Context)
c.Set("HTTP_STATUS_CODE", code)
}
// GetCodeFromContext 从context获取status code
func GetCodeFromContext(ctx context.Context) int {
c, _ := ctx.Value(key{}).(*gin.Context)
code, exists := c.Get("HTTP_STATUS_CODE")
if !exists {
return http.StatusOK
}
return code.(int)
}
// InterruptHandle 向context设置中断处理
func InterruptHandle(ctx context.Context) {
c, _ := ctx.Value(key{}).(*gin.Context)
c.Set("INTERRUPT_HANDLE", true)
}
// IsInterrupted 从context获取是否中断处理
func IsInterrupted(ctx context.Context) bool {
c, _ := ctx.Value(key{}).(*gin.Context)
isInterrupted, exists := c.Get("INTERRUPT_HANDLE")
if !exists {
return false
}
return isInterrupted.(bool)
}