forked from PolarPanda611/trinity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.go
More file actions
119 lines (100 loc) · 2.79 KB
/
Copy pathcontext.go
File metadata and controls
119 lines (100 loc) · 2.79 KB
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 trinity
import (
"context"
"github.com/jinzhu/gorm"
"google.golang.org/grpc/metadata"
)
// GRPCMethod grpc method
type GRPCMethod string
// ReqUserName request user name
type ReqUserName string
// TraceID current request trace id
type TraceID string
// UserRequestsCtx handle user ctx from context
type UserRequestsCtx interface {
GetGRPCMethod() GRPCMethod
GetTraceID() TraceID
GetReqUserName() ReqUserName
}
// UserRequestsCtxImpl UserRequestsCtxImpl handler request ctx , extrace data from ctx
type UserRequestsCtxImpl struct {
ctx context.Context
method GRPCMethod
reqUserName ReqUserName
traceID TraceID
}
// GetGRPCMethod get grpc method
func (u *UserRequestsCtxImpl) GetGRPCMethod() GRPCMethod { return u.method }
// GetTraceID get trace id
func (u *UserRequestsCtxImpl) GetTraceID() TraceID { return u.traceID }
// GetReqUserName get request user
func (u *UserRequestsCtxImpl) GetReqUserName() ReqUserName { return u.reqUserName }
// NewUserRequestsCtx new user ctx
func NewUserRequestsCtx(ctx context.Context) UserRequestsCtx {
if ctx != nil {
md, ok := metadata.FromIncomingContext(ctx)
if ok {
method := md[GRPCMethodKey][0]
traceID := md[TraceIDKey][0]
userName := md[ReqUserNameKey][0]
return &UserRequestsCtxImpl{
method: GRPCMethod(method),
reqUserName: ReqUserName(userName),
traceID: TraceID(traceID),
}
}
}
// no data carries in ctx
return &UserRequestsCtxImpl{
method: "",
reqUserName: "",
traceID: "",
}
}
// Context interface to get Req Context
type Context interface {
GetDB() *gorm.DB
GetTXDB() *gorm.DB
GetLogger() Logger
GetRequest() interface{}
GetResponse() interface{}
}
// ContextImpl context impl
type ContextImpl struct {
userRequestsCtx UserRequestsCtx
db *gorm.DB
logger Logger
setting ISetting
request interface{}
response interface{}
}
// NewContext new ctx
func NewContext(db *gorm.DB, setting ISetting, userRequestsCtx UserRequestsCtx, logger Logger) Context {
newDB := db.New()
newDB.SetLogger(logger)
newDB.Set("UserID", userRequestsCtx.GetReqUserName())
newContext := &ContextImpl{
userRequestsCtx: userRequestsCtx,
logger: logger,
db: newDB,
setting: setting,
}
return newContext
}
// GetDB get db instance
func (c *ContextImpl) GetDB() *gorm.DB {
return c.db
}
// GetTXDB get db instance with transaction
func (c *ContextImpl) GetTXDB() *gorm.DB {
c.db = c.db.Begin()
return c.db
}
// GetLogger get current user name
func (c *ContextImpl) GetLogger() Logger {
return c.logger
}
// GetRequest get user request data
func (c *ContextImpl) GetRequest() interface{} { return c.request }
// GetResponse get user response data
func (c *ContextImpl) GetResponse() interface{} { return c.response }