1
0

application.go 6.1 KB

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