http.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package http
  2. import (
  3. "net/http"
  4. "strings"
  5. "go-common/app/interface/bbq/bullet/api"
  6. "go-common/app/interface/bbq/bullet/internal/conf"
  7. "go-common/app/interface/bbq/bullet/internal/model"
  8. "go-common/app/interface/bbq/bullet/internal/service"
  9. xauth "go-common/app/interface/bbq/common/auth"
  10. chttp "go-common/app/interface/bbq/common/http"
  11. "go-common/library/ecode"
  12. "go-common/library/log"
  13. bm "go-common/library/net/http/blademaster"
  14. antispam "go-common/library/net/http/blademaster/middleware/antispam"
  15. "go-common/library/net/http/blademaster/middleware/verify"
  16. "github.com/golang/protobuf/ptypes/empty"
  17. )
  18. var (
  19. // TODO:verify
  20. vfy *verify.Verify
  21. svc *service.Service
  22. authSrv *xauth.BannedAuth
  23. logger *chttp.UILog
  24. bulletAntiSpam *antispam.Antispam
  25. )
  26. // Init init
  27. func Init(c *conf.Config, s *service.Service) {
  28. svc = s
  29. initAntiSpam(c)
  30. vfy = verify.New(c.Verify)
  31. authSrv = xauth.NewBannedAuth(c.Auth, c.OnlineMySQL)
  32. engine := bm.DefaultServer(c.BM)
  33. route(engine)
  34. if err := engine.Start(); err != nil {
  35. log.Error("bm Start error(%v)", err)
  36. panic(err)
  37. }
  38. logger = chttp.New(c.Infoc)
  39. }
  40. func initAntiSpam(c *conf.Config) {
  41. var antiConfig *antispam.Config
  42. var exists bool
  43. if antiConfig, exists = c.AntiSpam["bullet"]; !exists {
  44. panic("lose bullet anti_spam config")
  45. }
  46. bulletAntiSpam = antispam.New(antiConfig)
  47. }
  48. func route(e *bm.Engine) {
  49. e.Ping(ping)
  50. e.Register(register)
  51. g := e.Group("/bbq/bullet", wrapBBQ)
  52. {
  53. g.GET("/content/list", authSrv.Guest, contentList)
  54. g.GET("/content/get", authSrv.Guest, contentGet)
  55. g.POST("/content/post", authSrv.User, phoneCheck, bulletAntiSpam.ServeHTTP, contentPost)
  56. }
  57. }
  58. func ping(ctx *bm.Context) {
  59. if err := svc.Ping(ctx); err != nil {
  60. log.Error("ping error(%v)", err)
  61. ctx.AbortWithStatus(http.StatusServiceUnavailable)
  62. }
  63. }
  64. func register(c *bm.Context) {
  65. c.JSON(map[string]interface{}{}, nil)
  66. }
  67. //wrapRes 为返回头添加BBQ自定义字段
  68. func wrapBBQ(ctx *bm.Context) {
  69. chttp.WrapHeader(ctx)
  70. }
  71. func uiLog(ctx *bm.Context, action int, ext interface{}) {
  72. logger.Infoc(ctx, action, ext)
  73. }
  74. func contentGet(c *bm.Context) {
  75. if conf.Conf.BulletConfig.CloseRead {
  76. c.JSON([]*api.Bullet{}, nil)
  77. return
  78. }
  79. arg := &api.ListBulletReq{}
  80. if err := c.Bind(arg); err != nil {
  81. return
  82. }
  83. if arg.Oid == 0 {
  84. res := new([]interface{})
  85. c.JSON(res, ecode.DanmuGetErr)
  86. return
  87. }
  88. if midValue, exists := c.Get("mid"); exists {
  89. arg.Mid = midValue.(int64)
  90. }
  91. c.JSON(svc.ContentGet(c, arg))
  92. }
  93. func contentList(c *bm.Context) {
  94. if conf.Conf.BulletConfig.CloseRead {
  95. c.JSON(api.ListBulletReply{HasMore: false}, nil)
  96. return
  97. }
  98. arg := &api.ListBulletReq{}
  99. if err := c.Bind(arg); err != nil {
  100. return
  101. }
  102. if arg.Oid == 0 {
  103. res := new([]interface{})
  104. c.JSON(res, ecode.DanmuGetErr)
  105. return
  106. }
  107. if midValue, exists := c.Get("mid"); exists {
  108. arg.Mid = midValue.(int64)
  109. }
  110. c.JSON(svc.ContentList(c, arg))
  111. }
  112. func contentPost(c *bm.Context) {
  113. if conf.Conf.BulletConfig.CloseWrite {
  114. c.JSON(struct{}{}, nil)
  115. return
  116. }
  117. arg := &api.Bullet{}
  118. if err := c.Bind(arg); err != nil {
  119. return
  120. }
  121. // 这里客户端是有限制的,所以这里就不单独给出toast了
  122. if arg.Oid == 0 || arg.Content == "" || strings.Count(arg.Content, "") > model.BulletMaxLen {
  123. log.Warnw(c, "log", "post error", "req", arg, "content_len", strings.Count(arg.Content, ""))
  124. c.JSON(nil, ecode.DanmuPostErr)
  125. return
  126. }
  127. if midValue, exists := c.Get("mid"); exists {
  128. arg.Mid = midValue.(int64)
  129. }
  130. dmid, err := svc.ContentPost(c, arg)
  131. c.JSON(new(empty.Empty), err)
  132. uiLog(c, model.ActionDanmaku, struct {
  133. DMID int64 `json:"dmid"`
  134. }{
  135. DMID: dmid,
  136. })
  137. }
  138. // phoneCheck 进行手机校验
  139. func phoneCheck(ctx *bm.Context) {
  140. midValue, exists := ctx.Get("mid")
  141. if !exists {
  142. ctx.JSON(nil, ecode.NoLogin)
  143. ctx.Abort()
  144. return
  145. }
  146. mid := midValue.(int64)
  147. err := svc.PhoneCheck(ctx, mid)
  148. if err != nil {
  149. ctx.JSON(nil, err)
  150. ctx.Abort()
  151. return
  152. }
  153. }