service.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package service
  2. import (
  3. "context"
  4. "io"
  5. "time"
  6. "go-common/library/log"
  7. "go-common/app/admin/main/macross/conf"
  8. "go-common/app/admin/main/macross/dao"
  9. "go-common/app/admin/main/macross/dao/oss"
  10. model "go-common/app/admin/main/macross/model/manager"
  11. )
  12. // Service service struct info.
  13. type Service struct {
  14. c *conf.Config
  15. oss *oss.Dao
  16. dao *dao.Dao
  17. // manager cache
  18. user map[string]map[string]*model.User // system => { username => managerInfo }
  19. role map[string]map[int64]*model.Role // system => { roleId => roleInfo }
  20. authRelation map[int64][]int64 // role_id => [ auth_id ]
  21. auth map[string]map[int64]*model.Auth // system => { authId => authInfo }
  22. // ios cache
  23. modelNameCache map[string]map[string]int64
  24. }
  25. // New service.
  26. func New(c *conf.Config) (s *Service) {
  27. s = &Service{
  28. c: c,
  29. dao: dao.New(c),
  30. oss: oss.New(c),
  31. // init manager cache
  32. user: make(map[string]map[string]*model.User),
  33. role: make(map[string]map[int64]*model.Role),
  34. authRelation: make(map[int64][]int64),
  35. auth: make(map[string]map[int64]*model.Auth),
  36. // init ios cache
  37. modelNameCache: make(map[string]map[string]int64),
  38. }
  39. // manager cache
  40. if err := s.loadUserCache(); err != nil {
  41. panic(err)
  42. }
  43. if err := s.loadRoleCache(); err != nil {
  44. panic(err)
  45. }
  46. if err := s.loadAuthRelationCache(); err != nil {
  47. panic(err)
  48. }
  49. if err := s.loadAuthCache(); err != nil {
  50. panic(err)
  51. }
  52. go s.loadproc()
  53. return
  54. }
  55. // loadproc is a routine load to cache
  56. func (s *Service) loadproc() {
  57. for {
  58. time.Sleep(time.Duration(conf.Conf.Reload))
  59. s.loadUserCache()
  60. s.loadRoleCache()
  61. s.loadAuthRelationCache()
  62. s.loadAuthCache()
  63. }
  64. }
  65. func (s *Service) loadUserCache() (err error) {
  66. var tmpUser map[string]map[string]*model.User
  67. if tmpUser, err = s.dao.Users(context.TODO()); err != nil {
  68. log.Error("s.dao.Users() error(%v)", err)
  69. return
  70. }
  71. s.user = tmpUser
  72. return
  73. }
  74. func (s *Service) loadRoleCache() (err error) {
  75. var tmpRole map[string]map[int64]*model.Role
  76. if tmpRole, err = s.dao.Roles(context.TODO()); err != nil {
  77. log.Error("s.dao.Roles() error(%v)", err)
  78. return
  79. }
  80. s.role = tmpRole
  81. return
  82. }
  83. func (s *Service) loadAuthRelationCache() (err error) {
  84. var tmpAuthRelation map[int64][]int64
  85. if tmpAuthRelation, err = s.dao.AuthRelation(context.TODO()); err != nil {
  86. log.Error("s.dao.AuthRelation() error(%v)", err)
  87. return
  88. }
  89. s.authRelation = tmpAuthRelation
  90. return
  91. }
  92. func (s *Service) loadAuthCache() (err error) {
  93. var tmpAuth map[string]map[int64]*model.Auth
  94. if tmpAuth, err = s.dao.Auths(context.TODO()); err != nil {
  95. log.Error("s.dao.Auths() error(%v)", err)
  96. return
  97. }
  98. s.auth = tmpAuth
  99. return
  100. }
  101. // DiffPutOss upload diff to oss
  102. func (s *Service) DiffPutOss(c context.Context, f io.Reader, filename string) (uri string, err error) {
  103. if uri, err = s.oss.Put(c, f, filename); err != nil {
  104. log.Error("s.oss.Put(%s) error(%v)", filename, err)
  105. }
  106. return
  107. }
  108. // Ping dao
  109. func (s *Service) Ping(c context.Context) (err error) {
  110. if err = s.dao.Ping(c); err != nil {
  111. log.Error("s.dao error(%v)", err)
  112. }
  113. return
  114. }
  115. // Close dao
  116. func (s *Service) Close() {
  117. s.dao.Close()
  118. }