forked from micro-plat/hydra
/
context.go
134 lines (115 loc) · 3.87 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package context
import (
"strings"
"sync"
"github.com/micro-plat/hydra/conf"
"github.com/micro-plat/hydra/registry"
"github.com/micro-plat/lib4go/cache"
"github.com/micro-plat/lib4go/db"
"github.com/micro-plat/lib4go/influxdb"
"github.com/micro-plat/lib4go/logger"
"github.com/micro-plat/lib4go/net"
"github.com/micro-plat/lib4go/queue"
"github.com/micro-plat/lib4go/security/jwt"
"github.com/micro-plat/lib4go/security/md5"
)
type IContainer interface {
RPCInvoker
conf.ISystemConf
conf.IVarConf
conf.IMainConf
GetRegistry() registry.IRegistry
GetTags(name string) []string
GetRegularCache(names ...string) (c cache.ICache)
GetCache(names ...string) (c cache.ICache, err error)
GetCacheBy(tpName string, name string) (c cache.ICache, err error)
SaveCacheObject(tpName string, name string, f func(c conf.IConf) (cache.ICache, error)) (bool, cache.ICache, error)
GetRegularDB(names ...string) (d db.IDB)
GetDB(names ...string) (d db.IDB, err error)
GetDBBy(tpName string, name string) (c db.IDB, err error)
SaveDBObject(tpName string, name string, f func(c conf.IConf) (db.IDB, error)) (bool, db.IDB, error)
GetRegularInflux(names ...string) (c influxdb.IInfluxClient)
GetInflux(names ...string) (d influxdb.IInfluxClient, err error)
GetInfluxBy(tpName string, name string) (c influxdb.IInfluxClient, err error)
SaveInfluxObject(tpName string, name string, f func(c conf.IConf) (influxdb.IInfluxClient, error)) (bool, influxdb.IInfluxClient, error)
GetRegularQueue(names ...string) (c queue.IQueue)
GetQueue(names ...string) (q queue.IQueue, err error)
GetQueueBy(tpName string, name string) (c queue.IQueue, err error)
SaveQueueObject(tpName string, name string, f func(c conf.IConf) (queue.IQueue, error)) (bool, queue.IQueue, error)
GetGlobalObject(tpName string, name string) (c interface{}, err error)
SaveGlobalObject(tpName string, name string, f func(c conf.IConf) (interface{}, error)) (bool, interface{}, error)
Close() error
}
//Context 引擎执行上下文
type Context struct {
Request *Request
Response *Response
RPC *ContextRPC
container IContainer
Component interface{}
Meta *Meta
Log logger.ILogger
Name string
Engine string
Service string
}
//GetContext 从缓存池中获取一个context
func GetContext(component interface{}, name string, engine string, service string, container IContainer, queryString IData, form IData, param IData, setting IData, ext map[string]interface{}, logger *logger.Logger) *Context {
c := contextPool.Get().(*Context)
c.Request.reset(c, queryString, form, param, setting, ext)
c.Log = logger
c.Component = component
c.container = container
c.Name = name
c.Engine = engine
c.Service = formatName(c.Request.Translate(service, false))
c.Meta = NewMeta()
return c
}
//GetContainer 获取当前容器
func (c *Context) GetContainer() IContainer {
return c.container
}
//SetRPC 根据输入的context创建插件的上下文对象
func (c *Context) SetRPC(rpc RPCInvoker) {
c.RPC.reset(c, rpc)
}
//BuildJwt 构建JWT
func (c *Context) BuildJwt(data interface{}) (string, error) {
jwtAuth, err := c.Request.GetJWTConfig()
if err != nil {
return "", err
}
return jwt.Encrypt(jwtAuth.Secret, jwtAuth.Mode, data, jwtAuth.ExpireAt)
}
var contextPool *sync.Pool
func init() {
contextPool = &sync.Pool{
New: func() interface{} {
return &Context{
RPC: &ContextRPC{},
Request: newRequest(),
Response: NewResponse(),
}
},
}
}
//Close 回收context
func (c *Context) Close() {
c.Request.clear()
c.Response.clear()
c.RPC.clear()
c.container = nil
contextPool.Put(c)
}
func formatName(name string) string {
text := "/" + strings.Trim(strings.Trim(name, " "), "/")
return text
}
//MakeSign 检查签名原串
func MakeSign(input map[string]interface{}, key string) string {
values := net.NewValues()
values.SetMap(input)
values.Sort()
return md5.Encrypt(values.Join("", "") + key)
}