application.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. package runtime
  2. import (
  3. "IotAdmin/core/logger"
  4. "IotAdmin/core/storage"
  5. "IotAdmin/core/storage/queue"
  6. "net/http"
  7. "sync"
  8. "github.com/casbin/casbin/v2"
  9. "github.com/gin-gonic/gin"
  10. "github.com/robfig/cron/v3"
  11. "gorm.io/gorm"
  12. )
  13. type Application struct {
  14. dbs map[string]*gorm.DB
  15. casbins map[string]*casbin.SyncedEnforcer
  16. engine http.Handler
  17. crontab map[string]*cron.Cron
  18. mux sync.RWMutex
  19. middlewares map[string]interface{}
  20. cache storage.AdapterCache
  21. queue storage.AdapterQueue
  22. locker storage.AdapterLocker
  23. memoryQueue storage.AdapterQueue
  24. handler map[string][]func(r *gin.RouterGroup, hand ...*gin.HandlerFunc)
  25. routers []Router
  26. configs map[string]interface{} // 系统参数
  27. appRouters []func() // app路由
  28. }
  29. type Router struct {
  30. HttpMethod, RelativePath, Handler string
  31. }
  32. type Routers struct {
  33. List []Router
  34. }
  35. // NewConfig 默认值
  36. func NewConfig() *Application {
  37. return &Application{
  38. dbs: make(map[string]*gorm.DB),
  39. casbins: make(map[string]*casbin.SyncedEnforcer),
  40. crontab: make(map[string]*cron.Cron),
  41. middlewares: make(map[string]interface{}),
  42. memoryQueue: queue.NewMemory(10000),
  43. handler: make(map[string][]func(r *gin.RouterGroup, hand ...*gin.HandlerFunc)),
  44. routers: make([]Router, 0),
  45. configs: make(map[string]interface{}),
  46. }
  47. }
  48. // SetConfig 设置对应key的config
  49. func (e *Application) SetConfig(key string, value interface{}) {
  50. e.mux.Lock()
  51. defer e.mux.Unlock()
  52. e.configs[key] = value
  53. }
  54. // GetConfig 获取对应key的config
  55. func (e *Application) GetConfig(key string) interface{} {
  56. e.mux.Lock()
  57. defer e.mux.Unlock()
  58. return e.configs[key]
  59. }
  60. // SetDb 设置对应key的db
  61. func (e *Application) SetDb(key string, db *gorm.DB) {
  62. e.mux.Lock()
  63. defer e.mux.Unlock()
  64. e.dbs[key] = db
  65. }
  66. // GetDb 获取所有map里的db数据
  67. func (e *Application) GetDb() map[string]*gorm.DB {
  68. e.mux.Lock()
  69. defer e.mux.Unlock()
  70. return e.dbs
  71. }
  72. // GetDbByKey 根据key获取db
  73. func (e *Application) GetDbByKey(key string) *gorm.DB {
  74. e.mux.Lock()
  75. defer e.mux.Unlock()
  76. if db, ok := e.dbs["*"]; ok {
  77. return db
  78. }
  79. return e.dbs[key]
  80. }
  81. // SetCasbin 设置对应key的casbin
  82. func (e *Application) SetCasbin(key string, enforcer *casbin.SyncedEnforcer) {
  83. e.mux.Lock()
  84. defer e.mux.Unlock()
  85. e.casbins[key] = enforcer
  86. }
  87. // GetCasbin 获取所有map里的casbin数据
  88. func (e *Application) GetCasbin() map[string]*casbin.SyncedEnforcer {
  89. return e.casbins
  90. }
  91. // GetCasbinKey 根据key获取casbin
  92. func (e *Application) GetCasbinKey(key string) *casbin.SyncedEnforcer {
  93. e.mux.Lock()
  94. defer e.mux.Unlock()
  95. if e, ok := e.casbins["*"]; ok {
  96. return e
  97. }
  98. return e.casbins[key]
  99. }
  100. // SetEngine 设置路由引擎
  101. func (e *Application) SetEngine(engine http.Handler) {
  102. e.engine = engine
  103. }
  104. // GetEngine 获取路由引擎
  105. func (e *Application) GetEngine() http.Handler {
  106. return e.engine
  107. }
  108. // GetRouter 获取路由表
  109. func (e *Application) GetRouter() []Router {
  110. return e.setRouter()
  111. }
  112. // setRouter 设置路由表
  113. func (e *Application) setRouter() []Router {
  114. switch e.engine.(type) {
  115. case *gin.Engine:
  116. routers := e.engine.(*gin.Engine).Routes()
  117. for _, router := range routers {
  118. e.routers = append(e.routers, Router{RelativePath: router.Path, Handler: router.Handler, HttpMethod: router.Method})
  119. }
  120. }
  121. return e.routers
  122. }
  123. // SetLogger 设置日志组件
  124. func (e *Application) SetLogger(l logger.Logger) {
  125. logger.DefaultLogger = l
  126. }
  127. // GetLogger 获取日志组件
  128. func (e *Application) GetLogger() logger.Logger {
  129. return logger.DefaultLogger
  130. }
  131. // SetCrontab 设置对应key的crontab
  132. func (e *Application) SetCrontab(key string, crontab *cron.Cron) {
  133. e.mux.Lock()
  134. defer e.mux.Unlock()
  135. e.crontab[key] = crontab
  136. }
  137. // GetCrontab 获取所有map里的crontab数据
  138. func (e *Application) GetCrontab() map[string]*cron.Cron {
  139. e.mux.Lock()
  140. defer e.mux.Unlock()
  141. return e.crontab
  142. }
  143. // GetCrontabKey 根据key获取crontab
  144. func (e *Application) GetCrontabKey(key string) *cron.Cron {
  145. e.mux.Lock()
  146. defer e.mux.Unlock()
  147. if e, ok := e.crontab["*"]; ok {
  148. return e
  149. }
  150. return e.crontab[key]
  151. }
  152. // SetMiddleware 设置中间件
  153. func (e *Application) SetMiddleware(key string, middleware interface{}) {
  154. e.mux.Lock()
  155. defer e.mux.Unlock()
  156. e.middlewares[key] = middleware
  157. }
  158. // GetMiddleware 获取所有中间件
  159. func (e *Application) GetMiddleware() map[string]interface{} {
  160. return e.middlewares
  161. }
  162. // GetMiddlewareKey 获取对应key的中间件
  163. func (e *Application) GetMiddlewareKey(key string) interface{} {
  164. e.mux.Lock()
  165. defer e.mux.Unlock()
  166. return e.middlewares[key]
  167. }
  168. // SetCacheAdapter 设置缓存
  169. func (e *Application) SetCacheAdapter(c storage.AdapterCache) {
  170. e.cache = c
  171. }
  172. // GetCacheAdapter 获取缓存
  173. func (e *Application) GetCacheAdapter() storage.AdapterCache {
  174. return NewCache("", e.cache, "")
  175. }
  176. // GetCachePrefix 获取带租户标记的cache
  177. func (e *Application) GetCachePrefix(key string) storage.AdapterCache {
  178. return NewCache(key, e.cache, "")
  179. }
  180. // SetQueueAdapter 设置队列适配器
  181. func (e *Application) SetQueueAdapter(c storage.AdapterQueue) {
  182. e.queue = c
  183. }
  184. // GetQueueAdapter 获取队列适配器
  185. func (e *Application) GetQueueAdapter() storage.AdapterQueue {
  186. return NewQueue("", e.queue)
  187. }
  188. // GetQueuePrefix 获取带租户标记的queue
  189. func (e *Application) GetQueuePrefix(key string) storage.AdapterQueue {
  190. return NewQueue(key, e.queue)
  191. }
  192. // SetLockerAdapter 设置分布式锁
  193. func (e *Application) SetLockerAdapter(c storage.AdapterLocker) {
  194. e.locker = c
  195. }
  196. // GetLockerAdapter 获取分布式锁
  197. func (e *Application) GetLockerAdapter() storage.AdapterLocker {
  198. return NewLocker("", e.locker)
  199. }
  200. // GetLockerPrefix 获取带租户标记的分布式锁
  201. func (e *Application) GetLockerPrefix(key string) storage.AdapterLocker {
  202. return NewLocker(key, e.locker)
  203. }
  204. // SetHandler 将路由组函数添加到应用的路由处理器中
  205. func (e *Application) SetHandler(key string, routerGroup func(r *gin.RouterGroup, hand ...*gin.HandlerFunc)) {
  206. e.mux.Lock()
  207. defer e.mux.Unlock()
  208. e.handler[key] = append(e.handler[key], routerGroup)
  209. }
  210. // GetHandler 返回应用的路由处理器
  211. func (e *Application) GetHandler() map[string][]func(r *gin.RouterGroup, hand ...*gin.HandlerFunc) {
  212. e.mux.Lock()
  213. defer e.mux.Unlock()
  214. return e.handler
  215. }
  216. // GetHandlerPrefix 返回指定键的路由处理器
  217. func (e *Application) GetHandlerPrefix(key string) []func(r *gin.RouterGroup, hand ...*gin.HandlerFunc) {
  218. e.mux.Lock()
  219. defer e.mux.Unlock()
  220. return e.handler[key]
  221. }
  222. // SetAppRouters 设置app的路由
  223. func (e *Application) SetAppRouters(appRouters func()) {
  224. e.appRouters = append(e.appRouters, appRouters)
  225. }
  226. // GetAppRouters 获取app的路由
  227. func (e *Application) GetAppRouters() []func() {
  228. return e.appRouters
  229. }
  230. // GetStreamMessage 获取队列需要用的message
  231. func (e *Application) GetStreamMessage(id, stream string, value map[string]interface{}) (storage.Message, error) {
  232. message := &queue.Message{}
  233. message.SetID(id)
  234. message.SetStream(stream)
  235. message.SetValues(value)
  236. return message, nil
  237. }
  238. // GetMemoryQueue 获取队列
  239. func (e *Application) GetMemoryQueue(prefix string) storage.AdapterQueue {
  240. return NewQueue(prefix, e.memoryQueue)
  241. }