http.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. package http
  2. import (
  3. "fmt"
  4. "go-common/library/ecode"
  5. "go-common/library/net/http/blademaster/middleware/antispam"
  6. "net/http"
  7. "go-common/app/interface/bbq/app-bbq/api/http/v1"
  8. "go-common/app/interface/bbq/app-bbq/conf"
  9. "go-common/app/interface/bbq/app-bbq/service"
  10. xauth "go-common/app/interface/bbq/common/auth"
  11. chttp "go-common/app/interface/bbq/common/http"
  12. "go-common/library/log"
  13. bm "go-common/library/net/http/blademaster"
  14. "go-common/library/net/http/blademaster/middleware/verify"
  15. "go-common/library/net/trace"
  16. )
  17. var (
  18. srv *service.Service
  19. vfy *verify.Verify
  20. authSrv *xauth.BannedAuth
  21. cfg *conf.Config
  22. logger *chttp.UILog
  23. likeAntiSpam *antispam.Antispam
  24. relationAntiSpam *antispam.Antispam
  25. replyAntiSpam *antispam.Antispam
  26. uploadAntiSpam *antispam.Antispam
  27. reportAntiSpam *antispam.Antispam
  28. )
  29. // Init init
  30. func Init(c *conf.Config) {
  31. cfg = c
  32. initAntiSpam(c)
  33. logger = chttp.New(c.Infoc)
  34. srv = service.New(c)
  35. vfy = verify.New(c.Verify)
  36. authSrv = xauth.NewBannedAuth(c.Auth, c.MySQL)
  37. engine := bm.DefaultServer(c.BM)
  38. route(engine)
  39. if err := engine.Start(); err != nil {
  40. log.Error("bm Start error(%v)", err)
  41. panic(err)
  42. }
  43. }
  44. func initAntiSpam(c *conf.Config) {
  45. var antiConfig *antispam.Config
  46. var exists bool
  47. if antiConfig, exists = c.AntiSpam["like"]; !exists {
  48. panic("lose like anti_spam config")
  49. }
  50. relationAntiSpam = antispam.New(antiConfig)
  51. if antiConfig, exists = c.AntiSpam["relation"]; !exists {
  52. panic("lose relation anti_spam config")
  53. }
  54. likeAntiSpam = antispam.New(antiConfig)
  55. if antiConfig, exists = c.AntiSpam["reply"]; !exists {
  56. panic("lose reply anti_spam config")
  57. }
  58. replyAntiSpam = antispam.New(antiConfig)
  59. if antiConfig, exists = c.AntiSpam["upload"]; !exists {
  60. panic("lose upload anti_spam config")
  61. }
  62. uploadAntiSpam = antispam.New(antiConfig)
  63. if antiConfig, exists = c.AntiSpam["report"]; !exists {
  64. panic("lose report anti_spam config")
  65. }
  66. reportAntiSpam = antispam.New(antiConfig)
  67. }
  68. func route(e *bm.Engine) {
  69. e.Ping(ping)
  70. e.Register(register)
  71. g := e.Group("/bbq/app-bbq", wrapBBQ)
  72. {
  73. //用户登录
  74. g.GET("/user/login", authSrv.User, login)
  75. g.POST("/user/logout", authSrv.Guest, bm.Mobile(), pushLogout)
  76. //用户相关
  77. g.GET("/user/base", authSrv.User, userBase)
  78. // 所有字段都需要携带修改
  79. g.POST("/user/base/edit", authSrv.User, userBaseEdit)
  80. g.POST("/user/like/add", authSrv.User, likeAntiSpam.ServeHTTP, addUserLike)
  81. g.POST("/user/like/cancel", authSrv.User, likeAntiSpam.ServeHTTP, cancelUserLike)
  82. g.GET("/user/like/list", userLikeList)
  83. g.POST("/user/unlike", authSrv.User, likeAntiSpam.ServeHTTP, userUnLike)
  84. g.GET("/user/follow/list", authSrv.Guest, userFollowList)
  85. g.GET("/user/fan/list", authSrv.Guest, userFanList)
  86. g.GET("/user/black/list", authSrv.User, userBlackList)
  87. g.POST("/user/relation/modify", authSrv.User, relationAntiSpam.ServeHTTP, userRelationModify)
  88. g.GET("/search/hot/word", hotWord)
  89. // feed关注列表页
  90. g.GET("/feed/list", authSrv.User, feedList)
  91. // feed关注页红点
  92. g.GET("/feed/update_num", authSrv.User, feedUpdateNum)
  93. // space发布列表页
  94. g.GET("/space/sv/list", authSrv.Guest, spaceSvList)
  95. // space 用户详情况主/客
  96. g.GET("/space/user/profile", authSrv.Guest, spaceUserProfile)
  97. // 详情页up主发布列表
  98. g.GET("/detail/sv/list", authSrv.Guest, detailSvList)
  99. //视频相关
  100. g.GET("/sv/list", authSrv.Guest, bm.Mobile(), svList)
  101. g.GET("/sv/playlist", authSrv.Guest, bm.Mobile(), svPlayList) // playurl过期时候请求
  102. g.GET("/sv/detail", authSrv.Guest, svDetail)
  103. g.GET("/sv/stat", authSrv.Guest, bm.Mobile(), svStatistics)
  104. g.GET("/sv/relate", authSrv.Guest, svRelList)
  105. g.POST("/sv/del", authSrv.User, svDel)
  106. //搜索相关
  107. g.GET("/search/sv", authSrv.Guest, videoSearch)
  108. g.GET("/search/user", authSrv.Guest, userSearch)
  109. g.GET("/search/sug", authSrv.Guest, sug)
  110. g.GET("/search/topic", authSrv.Guest, topicSearch)
  111. //发现页
  112. g.GET("/discovery", authSrv.Guest, discoveryList)
  113. //话题详情页
  114. g.GET("/topic/detail", authSrv.Guest, topicDetail)
  115. // 用户location
  116. g.GET("/location/all", authSrv.User, locationAll)
  117. g.GET("/location", authSrv.User, location)
  118. //图片上传
  119. g.POST("/img/upload", authSrv.User, uploadAntiSpam.ServeHTTP, upload)
  120. // 客户端分享链接
  121. g.GET("/share", authSrv.Guest, shareURL)
  122. g.GET("/share/callback", authSrv.Guest, shareCallback)
  123. // 邀请函接口(内测版,公测删除)内测取消
  124. // g.GET("/invitation/download", invitationDownload)
  125. // App全局设置接口
  126. g.GET("/setting", appSetting)
  127. g.GET("/package", appPackage)
  128. }
  129. //评论组
  130. r := e.Group("/bbq/app-bbq/reply", wrapBBQ, commentInit)
  131. {
  132. //评论相关
  133. r.GET("/cursor", commentCloseRead, authSrv.Guest, commentCursor)
  134. r.POST("/add", commentCloseWrite, authSrv.User, phoneCheck, replyAntiSpam.ServeHTTP, commentAdd)
  135. r.POST("/action", commentCloseWrite, authSrv.User, likeAntiSpam.ServeHTTP, commentLike)
  136. r.GET("/", commentCloseRead, authSrv.Guest, commentList)
  137. r.GET("/reply/cursor", commentCloseRead, authSrv.Guest, commentSubCursor)
  138. }
  139. // 举报接口
  140. report := e.Group("/bbq/app-bbq/report", wrapBBQ)
  141. {
  142. report.GET("/config", authSrv.Guest, bm.Mobile(), reportConfig)
  143. report.POST("/report", authSrv.Guest, bm.Mobile(), reportAntiSpam.ServeHTTP, reportReport)
  144. }
  145. // 播放数据收集
  146. d := e.Group("/bbq/app-bbq/data", wrapBBQ)
  147. {
  148. d.GET("/collect", authSrv.Guest, bm.Mobile(), videoPlay)
  149. }
  150. // 通知中心,需要登录
  151. p := e.Group("/bbq/app-bbq/notice/center", authSrv.User, wrapBBQ)
  152. {
  153. p.GET("/num", noticeNum)
  154. p.GET("/overview", noticeOverview)
  155. p.GET("/list", noticeList)
  156. }
  157. // 推送相关
  158. push := e.Group("/bbq/app-bbq/push", wrapBBQ, authSrv.Guest, bm.Mobile())
  159. {
  160. push.POST("/register", pushRegister)
  161. push.GET("/callback", pushCallback)
  162. }
  163. //视频上传相关
  164. upload := e.Group("/bbq/app-bbq/upload/sv", authSrv.Guest)
  165. {
  166. upload.POST("/preupload", perUpload)
  167. upload.POST("/callback", callBack)
  168. upload.GET("/check", authSrv.User, uploadCheck)
  169. upload.POST("/homeimg", authSrv.User, homeimg)
  170. }
  171. }
  172. func commentCloseWrite(ctx *bm.Context) {
  173. if conf.Conf.Comment.CloseWrite {
  174. ctx.JSON(struct{}{}, ecode.OK)
  175. ctx.Abort()
  176. }
  177. }
  178. func commentCloseRead(ctx *bm.Context) {
  179. if conf.Conf.Comment.CloseRead {
  180. ctx.JSON(struct{}{}, ecode.OK)
  181. ctx.Abort()
  182. }
  183. }
  184. //wrapRes 为返回头添加BBQ自定义字段
  185. func wrapBBQ(ctx *bm.Context) {
  186. chttp.WrapHeader(ctx)
  187. // Base params
  188. req := ctx.Request
  189. base := new(v1.Base)
  190. ctx.Bind(base)
  191. base.BUVID = req.Header.Get("Buvid")
  192. ctx.Set("BBQBase", base)
  193. // QueryID
  194. qid := base.QueryID
  195. if base.QueryID == "" {
  196. tracer, _ := trace.FromContext(ctx.Context)
  197. qid = fmt.Sprintf("%s", tracer)
  198. }
  199. ctx.Set("QueryID", qid)
  200. }
  201. // phoneCheck 进行手机校验
  202. func phoneCheck(ctx *bm.Context) {
  203. midValue, exists := ctx.Get("mid")
  204. if !exists {
  205. ctx.JSON(nil, ecode.NoLogin)
  206. ctx.Abort()
  207. return
  208. }
  209. mid := midValue.(int64)
  210. err := srv.PhoneCheck(ctx, mid)
  211. if err != nil {
  212. ctx.JSON(nil, err)
  213. ctx.Abort()
  214. return
  215. }
  216. }
  217. func ping(c *bm.Context) {
  218. if err := srv.Ping(c); err != nil {
  219. log.Error("ping error(%v)", err)
  220. c.AbortWithStatus(http.StatusServiceUnavailable)
  221. }
  222. }
  223. func register(c *bm.Context) {
  224. c.JSON(map[string]interface{}{}, nil)
  225. }
  226. func uiLog(ctx *bm.Context, action int, ext interface{}) {
  227. logger.Infoc(ctx, action, ext)
  228. }