-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.go
277 lines (234 loc) · 6.85 KB
/
application.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package runtime
import (
"github.com/gin-gonic/gin"
"net/http"
"sync"
"github.com/casbin/casbin/v2"
"github.com/go-admin-team/go-admin-core/logger"
"github.com/go-admin-team/go-admin-core/storage"
"github.com/go-admin-team/go-admin-core/storage/queue"
"github.com/robfig/cron/v3"
"gorm.io/gorm"
)
type Application struct {
dbs map[string]*gorm.DB
casbins map[string]*casbin.SyncedEnforcer
engine http.Handler
crontab map[string]*cron.Cron
mux sync.RWMutex
middlewares map[string]interface{}
cache storage.AdapterCache
queue storage.AdapterQueue
locker storage.AdapterLocker
memoryQueue storage.AdapterQueue
handler map[string][]func(r *gin.RouterGroup, hand ...*gin.HandlerFunc)
routers []Router
configs map[string]interface{} // 系统参数
appRouters []func() // app路由
}
type Router struct {
HttpMethod, RelativePath, Handler string
}
type Routers struct {
List []Router
}
// SetDb 设置对应key的db
func (e *Application) SetDb(key string, db *gorm.DB) {
e.mux.Lock()
defer e.mux.Unlock()
e.dbs[key] = db
}
// GetDb 获取所有map里的db数据
func (e *Application) GetDb() map[string]*gorm.DB {
e.mux.Lock()
defer e.mux.Unlock()
return e.dbs
}
// GetDbByKey 根据key获取db
func (e *Application) GetDbByKey(key string) *gorm.DB {
e.mux.Lock()
defer e.mux.Unlock()
if db, ok := e.dbs["*"]; ok {
return db
}
return e.dbs[key]
}
func (e *Application) SetCasbin(key string, enforcer *casbin.SyncedEnforcer) {
e.mux.Lock()
defer e.mux.Unlock()
e.casbins[key] = enforcer
}
func (e *Application) GetCasbin() map[string]*casbin.SyncedEnforcer {
return e.casbins
}
// GetCasbinKey 根据key获取casbin
func (e *Application) GetCasbinKey(key string) *casbin.SyncedEnforcer {
e.mux.Lock()
defer e.mux.Unlock()
if e, ok := e.casbins["*"]; ok {
return e
}
return e.casbins[key]
}
// SetEngine 设置路由引擎
func (e *Application) SetEngine(engine http.Handler) {
e.engine = engine
}
// GetEngine 获取路由引擎
func (e *Application) GetEngine() http.Handler {
return e.engine
}
// GetRouter 获取路由表
func (e *Application) GetRouter() []Router {
return e.setRouter()
}
// setRouter 设置路由表
func (e *Application) setRouter() []Router {
switch e.engine.(type) {
case *gin.Engine:
routers := e.engine.(*gin.Engine).Routes()
for _, router := range routers {
e.routers = append(e.routers, Router{RelativePath: router.Path, Handler: router.Handler, HttpMethod: router.Method})
}
}
return e.routers
}
// SetLogger 设置日志组件
func (e *Application) SetLogger(l logger.Logger) {
logger.DefaultLogger = l
}
// GetLogger 获取日志组件
func (e *Application) GetLogger() logger.Logger {
return logger.DefaultLogger
}
// NewConfig 默认值
func NewConfig() *Application {
return &Application{
dbs: make(map[string]*gorm.DB),
casbins: make(map[string]*casbin.SyncedEnforcer),
crontab: make(map[string]*cron.Cron),
middlewares: make(map[string]interface{}),
memoryQueue: queue.NewMemory(10000),
handler: make(map[string][]func(r *gin.RouterGroup, hand ...*gin.HandlerFunc)),
routers: make([]Router, 0),
configs: make(map[string]interface{}),
}
}
// SetCrontab 设置对应key的crontab
func (e *Application) SetCrontab(key string, crontab *cron.Cron) {
e.mux.Lock()
defer e.mux.Unlock()
e.crontab[key] = crontab
}
// GetCrontab 获取所有map里的crontab数据
func (e *Application) GetCrontab() map[string]*cron.Cron {
e.mux.Lock()
defer e.mux.Unlock()
return e.crontab
}
// GetCrontabKey 根据key获取crontab
func (e *Application) GetCrontabKey(key string) *cron.Cron {
e.mux.Lock()
defer e.mux.Unlock()
if e, ok := e.crontab["*"]; ok {
return e
}
return e.crontab[key]
}
// SetMiddleware 设置中间件
func (e *Application) SetMiddleware(key string, middleware interface{}) {
e.mux.Lock()
defer e.mux.Unlock()
e.middlewares[key] = middleware
}
// GetMiddleware 获取所有中间件
func (e *Application) GetMiddleware() map[string]interface{} {
return e.middlewares
}
// GetMiddlewareKey 获取对应key的中间件
func (e *Application) GetMiddlewareKey(key string) interface{} {
e.mux.Lock()
defer e.mux.Unlock()
return e.middlewares[key]
}
// SetCacheAdapter 设置缓存
func (e *Application) SetCacheAdapter(c storage.AdapterCache) {
e.cache = c
}
// GetCacheAdapter 获取缓存
func (e *Application) GetCacheAdapter() storage.AdapterCache {
return NewCache("", e.cache, "")
}
// GetCachePrefix 获取带租户标记的cache
func (e *Application) GetCachePrefix(key string) storage.AdapterCache {
return NewCache(key, e.cache, "")
}
// SetQueueAdapter 设置队列适配器
func (e *Application) SetQueueAdapter(c storage.AdapterQueue) {
e.queue = c
}
// GetQueueAdapter 获取队列适配器
func (e *Application) GetQueueAdapter() storage.AdapterQueue {
return NewQueue("", e.queue)
}
// GetQueuePrefix 获取带租户标记的queue
func (e *Application) GetQueuePrefix(key string) storage.AdapterQueue {
return NewQueue(key, e.queue)
}
// SetLockerAdapter 设置分布式锁
func (e *Application) SetLockerAdapter(c storage.AdapterLocker) {
e.locker = c
}
// GetLockerAdapter 获取分布式锁
func (e *Application) GetLockerAdapter() storage.AdapterLocker {
return NewLocker("", e.locker)
}
func (e *Application) GetLockerPrefix(key string) storage.AdapterLocker {
return NewLocker(key, e.locker)
}
func (e *Application) SetHandler(key string, routerGroup func(r *gin.RouterGroup, hand ...*gin.HandlerFunc)) {
e.mux.Lock()
defer e.mux.Unlock()
e.handler[key] = append(e.handler[key], routerGroup)
}
func (e *Application) GetHandler() map[string][]func(r *gin.RouterGroup, hand ...*gin.HandlerFunc) {
e.mux.Lock()
defer e.mux.Unlock()
return e.handler
}
func (e *Application) GetHandlerPrefix(key string) []func(r *gin.RouterGroup, hand ...*gin.HandlerFunc) {
e.mux.Lock()
defer e.mux.Unlock()
return e.handler[key]
}
// GetStreamMessage 获取队列需要用的message
func (e *Application) GetStreamMessage(id, stream string, value map[string]interface{}) (storage.Messager, error) {
message := &queue.Message{}
message.SetID(id)
message.SetStream(stream)
message.SetValues(value)
return message, nil
}
func (e *Application) GetMemoryQueue(prefix string) storage.AdapterQueue {
return NewQueue(prefix, e.memoryQueue)
}
// SetConfig 设置对应key的config
func (e *Application) SetConfig(key string, value interface{}) {
e.mux.Lock()
defer e.mux.Unlock()
e.configs[key] = value
}
// GetConfig 获取对应key的config
func (e *Application) GetConfig(key string) interface{} {
e.mux.Lock()
defer e.mux.Unlock()
return e.configs[key]
}
// SetAppRouters 设置app的路由
func (e *Application) SetAppRouters(appRouters func()) {
e.appRouters = append(e.appRouters, appRouters)
}
// GetAppRouters 获取app的路由
func (e *Application) GetAppRouters() []func() {
return e.appRouters
}