|
@@ -53,6 +53,23 @@ func NewConfig() *Application {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// GetDefaultKey 获取默认key
|
|
|
|
|
+func (e *Application) GetDefaultKey() string {
|
|
|
|
|
+ return "*"
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// GetKey 获取key
|
|
|
|
|
+func (e *Application) GetKey(c *gin.Context) string {
|
|
|
|
|
+ key := c.GetHeader("key")
|
|
|
|
|
+ if key == "" {
|
|
|
|
|
+ key = c.GetHeader("tenant")
|
|
|
|
|
+ }
|
|
|
|
|
+ if key == "" {
|
|
|
|
|
+ key = e.GetDefaultKey()
|
|
|
|
|
+ }
|
|
|
|
|
+ return key
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// SetConfig 设置对应key的config
|
|
// SetConfig 设置对应key的config
|
|
|
func (e *Application) SetConfig(key string, value interface{}) {
|
|
func (e *Application) SetConfig(key string, value interface{}) {
|
|
|
e.mux.Lock()
|
|
e.mux.Lock()
|
|
@@ -74,20 +91,32 @@ func (e *Application) SetDb(key string, db *gorm.DB) {
|
|
|
e.dbs[key] = db
|
|
e.dbs[key] = db
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// GetDb 获取所有map里的db数据
|
|
|
|
|
-func (e *Application) GetDb() map[string]*gorm.DB {
|
|
|
|
|
|
|
+// GetDbMap 获取所有map里的db数据
|
|
|
|
|
+func (e *Application) GetDbMap() map[string]*gorm.DB {
|
|
|
e.mux.Lock()
|
|
e.mux.Lock()
|
|
|
defer e.mux.Unlock()
|
|
defer e.mux.Unlock()
|
|
|
return e.dbs
|
|
return e.dbs
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// GetDb 获取对应key的db
|
|
|
|
|
+func (e *Application) GetDb(c *gin.Context) *gorm.DB {
|
|
|
|
|
+ key := c.GetHeader("db")
|
|
|
|
|
+ if key == "" {
|
|
|
|
|
+ key = c.GetHeader("db-key")
|
|
|
|
|
+ }
|
|
|
|
|
+ if key == "" {
|
|
|
|
|
+ key = e.GetKey(c)
|
|
|
|
|
+ }
|
|
|
|
|
+ return e.GetDbByKey(key)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// GetDbByKey 根据key获取db
|
|
// GetDbByKey 根据key获取db
|
|
|
func (e *Application) GetDbByKey(key string) *gorm.DB {
|
|
func (e *Application) GetDbByKey(key string) *gorm.DB {
|
|
|
e.mux.Lock()
|
|
e.mux.Lock()
|
|
|
defer e.mux.Unlock()
|
|
defer e.mux.Unlock()
|
|
|
- if db, ok := e.dbs["*"]; ok {
|
|
|
|
|
- return db
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ //if db, ok := e.dbs["*"]; ok {
|
|
|
|
|
+ // return db
|
|
|
|
|
+ //}
|
|
|
return e.dbs[key]
|
|
return e.dbs[key]
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -98,18 +127,30 @@ func (e *Application) SetCasbin(key string, enforcer *casbin.SyncedEnforcer) {
|
|
|
e.casbins[key] = enforcer
|
|
e.casbins[key] = enforcer
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// GetCasbin 获取所有map里的casbin数据
|
|
|
|
|
-func (e *Application) GetCasbin() map[string]*casbin.SyncedEnforcer {
|
|
|
|
|
|
|
+// GetCasbinMap 获取所有map里的casbin数据
|
|
|
|
|
+func (e *Application) GetCasbinMap() map[string]*casbin.SyncedEnforcer {
|
|
|
return e.casbins
|
|
return e.casbins
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// GetCasbinKey 根据key获取casbin
|
|
|
|
|
-func (e *Application) GetCasbinKey(key string) *casbin.SyncedEnforcer {
|
|
|
|
|
|
|
+// GetCasbin 获取对应key的casbin
|
|
|
|
|
+func (e *Application) GetCasbin(c *gin.Context) *casbin.SyncedEnforcer {
|
|
|
|
|
+ key := c.GetHeader("casbin")
|
|
|
|
|
+ if key == "" {
|
|
|
|
|
+ key = c.GetHeader("casbin-key")
|
|
|
|
|
+ }
|
|
|
|
|
+ if key == "" {
|
|
|
|
|
+ key = e.GetKey(c)
|
|
|
|
|
+ }
|
|
|
|
|
+ return e.GetCasbinByKey(key)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// GetCasbinByKey 根据key获取casbin
|
|
|
|
|
+func (e *Application) GetCasbinByKey(key string) *casbin.SyncedEnforcer {
|
|
|
e.mux.Lock()
|
|
e.mux.Lock()
|
|
|
defer e.mux.Unlock()
|
|
defer e.mux.Unlock()
|
|
|
- if e, ok := e.casbins["*"]; ok {
|
|
|
|
|
- return e
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ //if e, ok := e.casbins["*"]; ok {
|
|
|
|
|
+ // return e
|
|
|
|
|
+ //}
|
|
|
return e.casbins[key]
|
|
return e.casbins[key]
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -157,20 +198,32 @@ func (e *Application) SetCrontab(key string, crontab *cron.Cron) {
|
|
|
e.crontab[key] = crontab
|
|
e.crontab[key] = crontab
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// GetCrontab 获取所有map里的crontab数据
|
|
|
|
|
-func (e *Application) GetCrontab() map[string]*cron.Cron {
|
|
|
|
|
|
|
+// GetCrontabMap 获取所有map里的crontab数据
|
|
|
|
|
+func (e *Application) GetCrontabMap() map[string]*cron.Cron {
|
|
|
e.mux.Lock()
|
|
e.mux.Lock()
|
|
|
defer e.mux.Unlock()
|
|
defer e.mux.Unlock()
|
|
|
return e.crontab
|
|
return e.crontab
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// GetCrontabKey 根据key获取crontab
|
|
|
|
|
-func (e *Application) GetCrontabKey(key string) *cron.Cron {
|
|
|
|
|
|
|
+// GetCrontab 获取对应key的crontab
|
|
|
|
|
+func (e *Application) GetCrontab(c *gin.Context) *cron.Cron {
|
|
|
|
|
+ key := c.GetHeader("crontab")
|
|
|
|
|
+ if key == "" {
|
|
|
|
|
+ key = c.GetHeader("crontab-key")
|
|
|
|
|
+ }
|
|
|
|
|
+ if key == "" {
|
|
|
|
|
+ key = e.GetKey(c)
|
|
|
|
|
+ }
|
|
|
|
|
+ return e.GetCrontabByKey(key)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// GetCrontabByKey 根据key获取crontab
|
|
|
|
|
+func (e *Application) GetCrontabByKey(key string) *cron.Cron {
|
|
|
e.mux.Lock()
|
|
e.mux.Lock()
|
|
|
defer e.mux.Unlock()
|
|
defer e.mux.Unlock()
|
|
|
- if e, ok := e.crontab["*"]; ok {
|
|
|
|
|
- return e
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ //if e, ok := e.crontab["*"]; ok {
|
|
|
|
|
+ // return e
|
|
|
|
|
+ //}
|
|
|
return e.crontab[key]
|
|
return e.crontab[key]
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -181,13 +234,25 @@ func (e *Application) SetMiddleware(key string, middleware interface{}) {
|
|
|
e.middlewares[key] = middleware
|
|
e.middlewares[key] = middleware
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// GetMiddleware 获取所有中间件
|
|
|
|
|
-func (e *Application) GetMiddleware() map[string]interface{} {
|
|
|
|
|
|
|
+// GetMiddlewareMap 获取所有中间件
|
|
|
|
|
+func (e *Application) GetMiddlewareMap() map[string]interface{} {
|
|
|
return e.middlewares
|
|
return e.middlewares
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// GetMiddlewareKey 获取对应key的中间件
|
|
|
|
|
-func (e *Application) GetMiddlewareKey(key string) interface{} {
|
|
|
|
|
|
|
+// GetMiddleware 获取对应key的中间件
|
|
|
|
|
+func (e *Application) GetMiddleware(c *gin.Context) interface{} {
|
|
|
|
|
+ key := c.GetHeader("middleware")
|
|
|
|
|
+ if key == "" {
|
|
|
|
|
+ key = c.GetHeader("middleware-key")
|
|
|
|
|
+ }
|
|
|
|
|
+ if key == "" {
|
|
|
|
|
+ key = e.GetKey(c)
|
|
|
|
|
+ }
|
|
|
|
|
+ return e.GetMiddlewareByKey(key)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// GetMiddlewareByKey 获取对应key的中间件
|
|
|
|
|
+func (e *Application) GetMiddlewareByKey(key string) interface{} {
|
|
|
e.mux.Lock()
|
|
e.mux.Lock()
|
|
|
defer e.mux.Unlock()
|
|
defer e.mux.Unlock()
|
|
|
return e.middlewares[key]
|
|
return e.middlewares[key]
|
|
@@ -203,8 +268,8 @@ func (e *Application) GetCacheAdapter() storage.AdapterCache {
|
|
|
return NewCache("", e.cache, "")
|
|
return NewCache("", e.cache, "")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// GetCachePrefix 获取带租户标记的cache
|
|
|
|
|
-func (e *Application) GetCachePrefix(key string) storage.AdapterCache {
|
|
|
|
|
|
|
+// GetCacheByKey 获取对应key的cache
|
|
|
|
|
+func (e *Application) GetCacheByKey(key string) storage.AdapterCache {
|
|
|
return NewCache(key, e.cache, "")
|
|
return NewCache(key, e.cache, "")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -218,11 +283,26 @@ func (e *Application) GetQueueAdapter() storage.AdapterQueue {
|
|
|
return NewQueue("", e.queue)
|
|
return NewQueue("", e.queue)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// GetQueuePrefix 获取带租户标记的queue
|
|
|
|
|
-func (e *Application) GetQueuePrefix(key string) storage.AdapterQueue {
|
|
|
|
|
|
|
+// GetQueue 获取队列
|
|
|
|
|
+func (e *Application) GetQueue(c *gin.Context) storage.AdapterQueue {
|
|
|
|
|
+ return e.GetQueueByKey(e.GetKey(c))
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// GetQueueByKey 获取对应key的queue
|
|
|
|
|
+func (e *Application) GetQueueByKey(key string) storage.AdapterQueue {
|
|
|
return NewQueue(key, e.queue)
|
|
return NewQueue(key, e.queue)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// GetMemoryQueue 获取内存队列
|
|
|
|
|
+func (e *Application) GetMemoryQueue(c *gin.Context) storage.AdapterQueue {
|
|
|
|
|
+ return e.GetMemoryQueueByKey(e.GetKey(c))
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// GetMemoryQueueByKey 获取内存队列
|
|
|
|
|
+func (e *Application) GetMemoryQueueByKey(key string) storage.AdapterQueue {
|
|
|
|
|
+ return NewQueue(key, e.memoryQueue)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// SetLockerAdapter 设置分布式锁
|
|
// SetLockerAdapter 设置分布式锁
|
|
|
func (e *Application) SetLockerAdapter(c storage.AdapterLocker) {
|
|
func (e *Application) SetLockerAdapter(c storage.AdapterLocker) {
|
|
|
e.locker = c
|
|
e.locker = c
|
|
@@ -233,8 +313,8 @@ func (e *Application) GetLockerAdapter() storage.AdapterLocker {
|
|
|
return NewLocker("", e.locker)
|
|
return NewLocker("", e.locker)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// GetLockerPrefix 获取带租户标记的分布式锁
|
|
|
|
|
-func (e *Application) GetLockerPrefix(key string) storage.AdapterLocker {
|
|
|
|
|
|
|
+// GetLockerByKey 获取对应key的locker
|
|
|
|
|
+func (e *Application) GetLockerByKey(key string) storage.AdapterLocker {
|
|
|
return NewLocker(key, e.locker)
|
|
return NewLocker(key, e.locker)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -245,15 +325,27 @@ func (e *Application) SetHandler(key string, routerGroup func(r *gin.RouterGroup
|
|
|
e.handler[key] = append(e.handler[key], routerGroup)
|
|
e.handler[key] = append(e.handler[key], routerGroup)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// GetHandler 返回应用的路由处理器
|
|
|
|
|
-func (e *Application) GetHandler() map[string][]func(r *gin.RouterGroup, hand ...*gin.HandlerFunc) {
|
|
|
|
|
|
|
+// GetHandlerMap 返回应用的路由处理器
|
|
|
|
|
+func (e *Application) GetHandlerMap() map[string][]func(r *gin.RouterGroup, hand ...*gin.HandlerFunc) {
|
|
|
e.mux.Lock()
|
|
e.mux.Lock()
|
|
|
defer e.mux.Unlock()
|
|
defer e.mux.Unlock()
|
|
|
return e.handler
|
|
return e.handler
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// GetHandlerPrefix 返回指定键的路由处理器
|
|
|
|
|
-func (e *Application) GetHandlerPrefix(key string) []func(r *gin.RouterGroup, hand ...*gin.HandlerFunc) {
|
|
|
|
|
|
|
+// GetHandler 返回指定键的路由处理器
|
|
|
|
|
+func (e *Application) GetHandler(c *gin.Context) []func(r *gin.RouterGroup, hand ...*gin.HandlerFunc) {
|
|
|
|
|
+ key := c.GetHeader("handler")
|
|
|
|
|
+ if key == "" {
|
|
|
|
|
+ key = c.GetHeader("handler-key")
|
|
|
|
|
+ }
|
|
|
|
|
+ if key == "" {
|
|
|
|
|
+ key = e.GetKey(c)
|
|
|
|
|
+ }
|
|
|
|
|
+ return e.GetHandlerByKey(key)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// GetHandlerByKey 返回指定键的路由处理器
|
|
|
|
|
+func (e *Application) GetHandlerByKey(key string) []func(r *gin.RouterGroup, hand ...*gin.HandlerFunc) {
|
|
|
e.mux.Lock()
|
|
e.mux.Lock()
|
|
|
defer e.mux.Unlock()
|
|
defer e.mux.Unlock()
|
|
|
return e.handler[key]
|
|
return e.handler[key]
|
|
@@ -277,8 +369,3 @@ func (e *Application) BuildStreamMessage(id, stream string, value map[string]int
|
|
|
message.SetValues(value)
|
|
message.SetValues(value)
|
|
|
return message, nil
|
|
return message, nil
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
-// GetMemoryQueue 获取队列
|
|
|
|
|
-func (e *Application) GetMemoryQueue(prefix string) storage.AdapterQueue {
|
|
|
|
|
- return NewQueue(prefix, e.memoryQueue)
|
|
|
|
|
-}
|
|
|