123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- package runtime
- import (
- "github.com/gin-gonic/gin"
- "net/http"
- "sync"
- "github.com/casbin/casbin/v2"
- "github.com/robfig/cron/v3"
- "gogs.baozhida.cn/zoie/OAuth-core/logger"
- "gogs.baozhida.cn/zoie/OAuth-core/storage"
- "gogs.baozhida.cn/zoie/OAuth-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
- queue storage.AdapterQueue
- locker storage.AdapterLocker
- memoryQueue storage.AdapterQueue
- handler map[string][]func(r *gin.RouterGroup, hand ...*gin.HandlerFunc)
- routers []Router
- }
- 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),
- }
- }
- // 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)
- }
|