forked from go-admin-team/go-admin-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.go
511 lines (435 loc) · 13.5 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
package runtime
import (
"github.com/xuanlingzi/go-admin-core/sdk/pkg/utils"
"net/http"
"sync"
"github.com/casbin/casbin/v2"
"github.com/gin-gonic/gin"
"github.com/robfig/cron/v3"
"github.com/xuanlingzi/go-admin-core/lbs"
"github.com/xuanlingzi/go-admin-core/message"
"github.com/xuanlingzi/go-admin-core/moderation"
"github.com/xuanlingzi/go-admin-core/payment"
"github.com/xuanlingzi/go-admin-core/third_party"
"github.com/xuanlingzi/go-admin-core/logger"
"github.com/xuanlingzi/go-admin-core/storage"
"github.com/xuanlingzi/go-admin-core/storage/queue"
"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
queues map[string]storage.AdapterQueue
locker storage.AdapterLocker
memoryQueue storage.AdapterQueue
fileStores map[string]storage.AdapterFileStore
moderation map[string]moderation.AdapterModeration
sms map[string]message.AdapterSms
mail map[string]message.AdapterMail
amqp map[string]message.AdapterAmqp
thirdParty map[string]third_party.AdapterThirdParty
lbs map[string]lbs.AdapterLocationBasedService
payment map[string]payment.AdapterPaymentService
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{}),
queues: make(map[string]storage.AdapterQueue),
memoryQueue: queue.NewMemory(10000),
fileStores: make(map[string]storage.AdapterFileStore),
moderation: make(map[string]moderation.AdapterModeration),
sms: make(map[string]message.AdapterSms),
mail: make(map[string]message.AdapterMail),
amqp: make(map[string]message.AdapterAmqp),
thirdParty: make(map[string]third_party.AdapterThirdParty),
lbs: make(map[string]lbs.AdapterLocationBasedService),
payment: make(map[string]payment.AdapterPaymentService),
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(key string, c storage.AdapterQueue) {
e.mux.Lock()
defer e.mux.Unlock()
if utils.StringIsEmpty(key) {
key = "*"
}
e.queues[key] = c
}
// GetQueueAdapter 获取队列适配器
func (e *Application) GetQueueAdapter() storage.AdapterQueue {
return e.GetQueueKey("*")
}
func (e *Application) GetQueueKey(key string) storage.AdapterQueue {
e.mux.Lock()
defer e.mux.Unlock()
return e.queues[key]
}
// GetQueuePrefix 获取带租户标记的queue
func (e *Application) GetQueuePrefix(key string, prefix string) storage.AdapterQueue {
return NewQueue(prefix, e.GetQueueKey(key))
}
// 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)
}
// SetSmsAdapter 设置缓存
func (e *Application) SetSmsAdapter(key string, c message.AdapterSms) {
e.mux.Lock()
defer e.mux.Unlock()
e.sms[key] = c
}
// GetSmsAdapter 获取缓存
func (e *Application) GetSmsAdapter() message.AdapterSms {
return e.GetSmsKey("*")
}
// GetSmsAdapters 获取缓存
func (e *Application) GetSmsAdapters() map[string]message.AdapterSms {
e.mux.Lock()
defer e.mux.Unlock()
return e.sms
}
// GetSmsKey 获取带租户标记的sms
func (e *Application) GetSmsKey(key string) message.AdapterSms {
e.mux.Lock()
defer e.mux.Unlock()
return e.sms[key]
}
// SetMailAdapter 设置缓存
func (e *Application) SetMailAdapter(key string, c message.AdapterMail) {
e.mux.Lock()
defer e.mux.Unlock()
e.mail[key] = c
}
// GetMailAdapter 获取缓存
func (e *Application) GetMailAdapter() message.AdapterMail {
return e.GetMailKey("*")
}
// GetMailAdapters 获取缓存
func (e *Application) GetMailAdapters() map[string]message.AdapterMail {
e.mux.Lock()
defer e.mux.Unlock()
return e.mail
}
// GetMailKey 获取带租户标记的mail
func (e *Application) GetMailKey(key string) message.AdapterMail {
e.mux.Lock()
defer e.mux.Unlock()
return e.mail[key]
}
// SetFileStoreAdapter 设置缓存
func (e *Application) SetFileStoreAdapter(key string, c storage.AdapterFileStore) {
e.mux.Lock()
defer e.mux.Unlock()
e.fileStores[key] = c
}
// GetFileStoreAdapter 获取缓存
func (e *Application) GetFileStoreAdapter() storage.AdapterFileStore {
return e.GetFileStoreKey("*")
}
// GetFileStoreAdapters 获取缓存
func (e *Application) GetFileStoreAdapters() map[string]storage.AdapterFileStore {
e.mux.Lock()
defer e.mux.Unlock()
return e.fileStores
}
// GetFileStoreKey 获取带租户标记的cos
func (e *Application) GetFileStoreKey(key string) storage.AdapterFileStore {
e.mux.Lock()
defer e.mux.Unlock()
return e.fileStores[key]
}
// SetModerationAdapter 设置缓存
func (e *Application) SetModerationAdapter(key string, c moderation.AdapterModeration) {
e.mux.Lock()
defer e.mux.Unlock()
e.moderation[key] = c
}
// GetModerationAdapter 获取缓存
func (e *Application) GetModerationAdapter() moderation.AdapterModeration {
return e.GetModerationKey("*")
}
// GetModerationAdapters 获取缓存
func (e *Application) GetModerationAdapters() map[string]moderation.AdapterModeration {
e.mux.Lock()
defer e.mux.Unlock()
return e.moderation
}
// GetModerationKey 获取带租户标记的cos
func (e *Application) GetModerationKey(key string) moderation.AdapterModeration {
e.mux.Lock()
defer e.mux.Unlock()
return e.moderation[key]
}
// SetAmqpAdapter 设置缓存
func (e *Application) SetAmqpAdapter(key string, c message.AdapterAmqp) {
e.mux.Lock()
defer e.mux.Unlock()
e.amqp[key] = c
}
// GetAmqpAdapter 获取缓存
func (e *Application) GetAmqpAdapter() message.AdapterAmqp {
return e.GetAmqpKey("*")
}
// GetAmqpAdapters 获取缓存
func (e *Application) GetAmqpAdapters() map[string]message.AdapterAmqp {
return e.amqp
}
// GetAmqpKey 获取带租户标记的amqp
func (e *Application) GetAmqpKey(key string) message.AdapterAmqp {
e.mux.Lock()
defer e.mux.Unlock()
return e.amqp[key]
}
// SetThirdPartyAdapter 设置缓存
func (e *Application) SetThirdPartyAdapter(key string, c third_party.AdapterThirdParty) {
e.mux.Lock()
defer e.mux.Unlock()
e.thirdParty[key] = c
}
// GetThirdPartyAdapter 获取缓存
func (e *Application) GetThirdPartyAdapter() third_party.AdapterThirdParty {
return e.GetThirdPartyKey("*")
}
// GetThirdPartyAdapters 获取缓存
func (e *Application) GetThirdPartyAdapters() map[string]third_party.AdapterThirdParty {
return e.thirdParty
}
// GetThirdPartyKey 获取带租户标记的amqp
func (e *Application) GetThirdPartyKey(key string) third_party.AdapterThirdParty {
e.mux.Lock()
defer e.mux.Unlock()
return e.thirdParty[key]
}
// SetLocationBasedServiceAdapter 设置LBS
func (e *Application) SetLocationBasedServiceAdapter(key string, c lbs.AdapterLocationBasedService) {
e.mux.Lock()
defer e.mux.Unlock()
e.lbs[key] = c
}
// GetLocationBasedServiceAdapter 获取LBS
func (e *Application) GetLocationBasedServiceAdapter() lbs.AdapterLocationBasedService {
return e.GetLocationBasedServiceKey("*")
}
// GetLocationBasedServiceAdapters 获取LBS
func (e *Application) GetLocationBasedServiceAdapters() map[string]lbs.AdapterLocationBasedService {
return e.lbs
}
// GetLocationBasedServiceKey 获取LBS
func (e *Application) GetLocationBasedServiceKey(key string) lbs.AdapterLocationBasedService {
e.mux.Lock()
defer e.mux.Unlock()
return e.lbs[key]
}
// SetPaymentServiceAdapter 设置支付
func (e *Application) SetPaymentServiceAdapter(key string, c payment.AdapterPaymentService) {
e.mux.Lock()
defer e.mux.Unlock()
e.payment[key] = c
}
// GetPaymentServiceAdapter 获取支付
func (e *Application) GetPaymentServiceAdapter() payment.AdapterPaymentService {
return e.GetPaymentServiceKey("*")
}
// GetPaymentServiceAdapters 获取支付
func (e *Application) GetPaymentServiceAdapters() map[string]payment.AdapterPaymentService {
return e.payment
}
// GetPaymentServiceKey 获取支付
func (e *Application) GetPaymentServiceKey(key string) payment.AdapterPaymentService {
e.mux.Lock()
defer e.mux.Unlock()
return e.payment[key]
}
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
}