server.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package grpc
  2. import (
  3. "context"
  4. v1 "go-common/app/service/main/archive/api"
  5. "go-common/app/service/main/archive/service"
  6. "go-common/library/net/rpc/warden"
  7. )
  8. type server struct {
  9. srv *service.Service
  10. }
  11. // New grpc server
  12. func New(cfg *warden.ServerConfig, srv *service.Service) (wsvr *warden.Server, err error) {
  13. wsvr = warden.NewServer(cfg)
  14. v1.RegisterArchiveServer(wsvr.Server(), &server{srv: srv})
  15. wsvr, err = wsvr.Start()
  16. return
  17. }
  18. // Types get all types
  19. func (s *server) Types(c context.Context, noArg *v1.NoArgRequest) (resp *v1.TypesReply, err error) {
  20. types := s.srv.AllTypes(c)
  21. resp = new(v1.TypesReply)
  22. resp.Types = make(map[int32]*v1.Tp)
  23. for _, tp := range types {
  24. resp.Types[int32(tp.ID)] = &v1.Tp{
  25. ID: int32(tp.ID),
  26. Pid: int32(tp.Pid),
  27. Name: tp.Name,
  28. }
  29. }
  30. return
  31. }
  32. // Arc get archive
  33. func (s *server) Arc(c context.Context, req *v1.ArcRequest) (resp *v1.ArcReply, err error) {
  34. resp = new(v1.ArcReply)
  35. a, err := s.srv.Archive3(c, req.Aid)
  36. if err != nil {
  37. return
  38. }
  39. resp.Arc = a
  40. return
  41. }
  42. // Arcs get archives
  43. func (s *server) Arcs(c context.Context, req *v1.ArcsRequest) (resp *v1.ArcsReply, err error) {
  44. resp = new(v1.ArcsReply)
  45. resp.Arcs = make(map[int64]*v1.Arc)
  46. as, err := s.srv.Archives3(c, req.Aids)
  47. if err != nil {
  48. return
  49. }
  50. if len(as) == 0 {
  51. return
  52. }
  53. for aid, a := range as {
  54. resp.Arcs[aid] = a
  55. }
  56. return
  57. }
  58. // View get archive and page
  59. func (s *server) View(c context.Context, req *v1.ViewRequest) (resp *v1.ViewReply, err error) {
  60. resp = new(v1.ViewReply)
  61. v, err := s.srv.View3(c, req.Aid)
  62. if err != nil {
  63. return
  64. }
  65. resp = v
  66. return
  67. }
  68. // Views get archives and pages
  69. func (s *server) Views(c context.Context, req *v1.ViewsRequest) (resp *v1.ViewsReply, err error) {
  70. resp = new(v1.ViewsReply)
  71. resp.Views = make(map[int64]*v1.ViewReply)
  72. vs, err := s.srv.Views3(c, req.Aids)
  73. if err != nil {
  74. return
  75. }
  76. if len(vs) == 0 {
  77. return
  78. }
  79. resp.Views = vs
  80. return
  81. }
  82. func (s *server) Stat(c context.Context, req *v1.StatRequest) (resp *v1.StatReply, err error) {
  83. resp = new(v1.StatReply)
  84. stat, err := s.srv.Stat3(c, req.Aid)
  85. if err != nil {
  86. return
  87. }
  88. resp.Stat = stat
  89. return
  90. }
  91. func (s *server) Stats(c context.Context, req *v1.StatsRequest) (resp *v1.StatsReply, err error) {
  92. resp = new(v1.StatsReply)
  93. resp.Stats = make(map[int64]*v1.Stat)
  94. stats, err := s.srv.Stats3(c, req.Aids)
  95. if err != nil {
  96. return
  97. }
  98. resp.Stats = stats
  99. return
  100. }
  101. func (s *server) Click(c context.Context, req *v1.ClickRequest) (resp *v1.ClickReply, err error) {
  102. resp = new(v1.ClickReply)
  103. resp.Click, err = s.srv.Click3(c, req.Aid)
  104. return
  105. }
  106. func (s *server) Page(c context.Context, req *v1.PageRequest) (resp *v1.PageReply, err error) {
  107. resp = new(v1.PageReply)
  108. resp.Pages, err = s.srv.Page3(c, req.Aid)
  109. return
  110. }
  111. func (s *server) Recommend(c context.Context, req *v1.RecommendRequest) (resp *v1.RecommendReply, err error) {
  112. resp = new(v1.RecommendReply)
  113. resp.Arcs, err = s.srv.UpperReommend(c, req.Aid)
  114. return
  115. }
  116. func (s *server) Video(c context.Context, req *v1.VideoRequest) (resp *v1.VideoReply, err error) {
  117. resp = new(v1.VideoReply)
  118. resp.Page, err = s.srv.Video3(c, req.Aid, req.Cid)
  119. return
  120. }
  121. func (s *server) MaxAid(c context.Context, req *v1.NoArgRequest) (resp *v1.MaxAidReply, err error) {
  122. resp = new(v1.MaxAidReply)
  123. resp.Aid, err = s.srv.MaxAID(c)
  124. return
  125. }
  126. func (s *server) ArcFieldCache(c context.Context, req *v1.ArcFieldCacheRequest) (resp *v1.NoReply, err error) {
  127. resp = new(v1.NoReply)
  128. err = s.srv.FieldCacheUpdate(c, req.Aid, int16(req.OldTypeID), int16(req.TypeID))
  129. return
  130. }
  131. func (s *server) ArcCache(c context.Context, req *v1.ArcCacheRequest) (resp *v1.NoReply, err error) {
  132. resp = new(v1.NoReply)
  133. err = s.srv.CacheUpdate(c, req.Aid, req.Tp, req.OldMid)
  134. return
  135. }
  136. func (s *server) DelVideoCache(c context.Context, req *v1.DelVideoCacheRequest) (resp *v1.NoReply, err error) {
  137. resp = new(v1.NoReply)
  138. err = s.srv.DelVideo(c, req.Aid, req.Cid)
  139. return
  140. }
  141. func (s *server) UpVideoCache(c context.Context, req *v1.UpVideoCacheRequest) (resp *v1.NoReply, err error) {
  142. resp = new(v1.NoReply)
  143. err = s.srv.UpVideo(c, req.Aid, req.Cid)
  144. return
  145. }
  146. func (s *server) Description(c context.Context, req *v1.DescriptionRequest) (resp *v1.DescriptionReply, err error) {
  147. resp = new(v1.DescriptionReply)
  148. resp.Desc, err = s.srv.Description(c, req.Aid)
  149. return
  150. }
  151. func (s *server) SetStat(c context.Context, req *v1.SetStatRequest) (resp *v1.NoReply, err error) {
  152. resp = new(v1.NoReply)
  153. err = s.srv.SetStat(c, req.Stat)
  154. return
  155. }