service.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/interface/main/playlist/conf"
  5. "go-common/app/interface/main/playlist/dao"
  6. accclient "go-common/app/service/main/account/api"
  7. accwarden "go-common/app/service/main/account/api"
  8. accrpc "go-common/app/service/main/account/rpc/client"
  9. arcrpc "go-common/app/service/main/archive/api/gorpc"
  10. arcclient "go-common/app/service/main/archive/api"
  11. favrpc "go-common/app/service/main/favorite/api/gorpc"
  12. "go-common/app/service/main/filter/rpc/client"
  13. "go-common/library/cache"
  14. "go-common/library/log"
  15. )
  16. // Service service struct.
  17. type Service struct {
  18. c *conf.Config
  19. dao *dao.Dao
  20. // rpc
  21. fav *favrpc.Service
  22. arc *arcrpc.Service2
  23. acc *accrpc.Service3
  24. filter *filter.Service
  25. // cache proc
  26. cache *cache.Cache
  27. // playlist power mids
  28. allowMids map[int64]struct{}
  29. maxSort int64
  30. arcClient arcclient.ArchiveClient
  31. accClient accwarden.AccountClient
  32. }
  33. // New new service.
  34. func New(c *conf.Config) *Service {
  35. s := &Service{
  36. c: c,
  37. dao: dao.New(c),
  38. fav: favrpc.New2(c.FavoriteRPC),
  39. arc: arcrpc.New2(c.ArchiveRPC),
  40. acc: accrpc.New3(c.AccountRPC),
  41. filter: filter.New(c.FilterRPC),
  42. cache: cache.New(1, 1024),
  43. maxSort: c.Rule.MinSort + 4*c.Rule.SortStep*int64(c.Rule.MaxVideoCnt),
  44. }
  45. var err error
  46. if s.arcClient, err = arcclient.NewClient(c.ArcClient); err != nil {
  47. panic(err)
  48. }
  49. if s.accClient, err = accclient.NewClient(c.AccClient); err != nil {
  50. panic(err)
  51. }
  52. s.initMids()
  53. return s
  54. }
  55. func (s *Service) initMids() {
  56. tmp := make(map[int64]struct{}, len(s.c.Rule.PowerMids))
  57. for _, id := range s.c.Rule.PowerMids {
  58. tmp[id] = struct{}{}
  59. }
  60. s.allowMids = tmp
  61. }
  62. // Ping ping service.
  63. func (s *Service) Ping(c context.Context) (err error) {
  64. if err = s.dao.Ping(c); err != nil {
  65. log.Error("s.dao.Ping error(%v)", err)
  66. }
  67. return
  68. }