http.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package http
  2. import (
  3. "net/http"
  4. "strconv"
  5. "go-common/app/interface/main/favorite/conf"
  6. "go-common/app/interface/main/favorite/service"
  7. "go-common/library/log"
  8. "go-common/library/log/anticheat"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/http/blademaster/middleware/antispam"
  11. "go-common/library/net/http/blademaster/middleware/auth"
  12. "go-common/library/net/http/blademaster/middleware/supervisor"
  13. "go-common/library/net/http/blademaster/middleware/verify"
  14. )
  15. var (
  16. favSvc *service.Service
  17. authSvc *auth.Auth
  18. verifySvc *verify.Verify
  19. antispamM *antispam.Antispam
  20. supervisorM *supervisor.Supervisor
  21. collector *anticheat.AntiCheat
  22. )
  23. // Init init router
  24. func Init(c *conf.Config, svc *service.Service) {
  25. verifySvc = verify.New(c.Verify)
  26. authSvc = auth.New(c.Auth)
  27. antispamM = antispam.New(c.Antispam)
  28. supervisorM = supervisor.New(c.Supervisor)
  29. favSvc = svc
  30. if c.Infoc2 != nil {
  31. collector = anticheat.New(c.Infoc2)
  32. }
  33. // init outer router
  34. engineOut := bm.DefaultServer(c.BM)
  35. outerRouter(engineOut)
  36. internalRouter(engineOut)
  37. // init Out serve
  38. if err := engineOut.Start(); err != nil {
  39. log.Error("engineOut.Start() error(%v)", err)
  40. panic(err)
  41. }
  42. }
  43. // outerRouter init outer router api path
  44. func outerRouter(e *bm.Engine) {
  45. // init api
  46. e.GET("/monitor/ping", ping)
  47. folderG := e.Group("/x/v2/fav/folder")
  48. {
  49. folderG.GET("", authSvc.Guest, videoFolders)
  50. folderG.POST("/add", authSvc.User, antispamM.ServeHTTP, supervisorM.ServeHTTP, addVideoFolder)
  51. folderG.POST("/del", authSvc.User, antispamM.ServeHTTP, delVideoFolder)
  52. folderG.POST("/rename", authSvc.User, antispamM.ServeHTTP, supervisorM.ServeHTTP, renameVideoFolder)
  53. folderG.POST("/public", authSvc.User, antispamM.ServeHTTP, upStateVideoFolder)
  54. folderG.POST("/sort", authSvc.User, antispamM.ServeHTTP, sortVideoFolders)
  55. }
  56. videoG := e.Group("/x/v2/fav/video")
  57. {
  58. videoG.GET("", authSvc.Guest, favVideo)
  59. videoG.GET("/tlist", authSvc.Guest, tidList)
  60. videoG.GET("/newest", authSvc.User, favVideoNewest)
  61. videoG.POST("/add", authSvc.User, antispamM.ServeHTTP, addFavVideo)
  62. videoG.POST("/del", authSvc.User, antispamM.ServeHTTP, delFavVideo)
  63. videoG.POST("/mdel", authSvc.User, antispamM.ServeHTTP, delFavVideos)
  64. videoG.POST("/move", authSvc.User, antispamM.ServeHTTP, moveFavVideos)
  65. videoG.POST("/copy", authSvc.User, antispamM.ServeHTTP, copyFavVideos)
  66. videoG.GET("/favoureds", authSvc.User, isFavoureds)
  67. videoG.GET("/favoured", authSvc.User, isFavoured)
  68. videoG.GET("/default", authSvc.User, inDefaultFav)
  69. videoG.GET("/cleaned", authSvc.User, isCleaned)
  70. videoG.POST("/clean", authSvc.User, cleanInvalidArcs)
  71. }
  72. topicG := e.Group("/x/v2/fav/topic")
  73. {
  74. topicG.POST("/add", authSvc.User, antispamM.ServeHTTP, addFavTopic)
  75. topicG.POST("/del", authSvc.User, antispamM.ServeHTTP, delFavTopic)
  76. topicG.GET("/favoured", authSvc.User, isTopicFavoured)
  77. topicG.GET("", authSvc.User, favTopics)
  78. }
  79. }
  80. // internalRouter init internal router api path
  81. func internalRouter(e *bm.Engine) {
  82. // init api
  83. folderG := e.Group("/x/internal/v2/fav/folder")
  84. {
  85. folderG.GET("", verifySvc.Verify, setMid, videoFolders)
  86. folderG.POST("/add", verifySvc.VerifyUser, addVideoFolder)
  87. folderG.POST("/del", verifySvc.VerifyUser, delVideoFolder)
  88. folderG.POST("/rename", verifySvc.VerifyUser, renameVideoFolder)
  89. folderG.POST("/public", verifySvc.VerifyUser, upStateVideoFolder)
  90. folderG.POST("/sort", verifySvc.VerifyUser, sortVideoFolders)
  91. }
  92. videoG := e.Group("/x/internal/v2/fav/video")
  93. {
  94. videoG.GET("", verifySvc.Verify, setMid, favVideo)
  95. videoG.GET("/tlist", verifySvc.Verify, setMid, tidList)
  96. videoG.GET("/newest", verifySvc.VerifyUser, favVideoNewest)
  97. videoG.POST("/add", verifySvc.VerifyUser, addFavVideo)
  98. videoG.POST("/del", verifySvc.VerifyUser, delFavVideo)
  99. videoG.POST("/mdel", verifySvc.VerifyUser, delFavVideos)
  100. videoG.POST("/move", verifySvc.VerifyUser, moveFavVideos)
  101. videoG.POST("/copy", verifySvc.VerifyUser, copyFavVideos)
  102. videoG.GET("/favoureds", verifySvc.VerifyUser, isFavoureds)
  103. videoG.GET("/favoured", verifySvc.VerifyUser, isFavoured)
  104. videoG.GET("/default", verifySvc.VerifyUser, inDefaultFav)
  105. videoG.GET("/cleaned", verifySvc.VerifyUser, isCleaned)
  106. videoG.POST("/clean", verifySvc.VerifyUser, cleanInvalidArcs)
  107. }
  108. topicG := e.Group("/x/internal/v2/fav/topic")
  109. {
  110. topicG.POST("/add", verifySvc.VerifyUser, addFavTopic)
  111. topicG.POST("/del", verifySvc.VerifyUser, delFavTopic)
  112. topicG.GET("/favoured", verifySvc.VerifyUser, isTopicFavoured)
  113. topicG.GET("", verifySvc.VerifyUser, favTopics)
  114. }
  115. }
  116. func setMid(c *bm.Context) {
  117. var (
  118. err error
  119. mid int64
  120. )
  121. req := c.Request
  122. midStr := req.Form.Get("mid")
  123. if midStr != "" {
  124. if mid, err = strconv.ParseInt(midStr, 10, 64); err != nil {
  125. c.JSON(nil, err)
  126. c.Abort()
  127. return
  128. }
  129. }
  130. c.Set("mid", mid)
  131. }
  132. // ping check server ok.
  133. func ping(c *bm.Context) {
  134. if err := favSvc.Ping(c); err != nil {
  135. log.Error("favorite http service ping error(%v)", err)
  136. c.AbortWithStatus(http.StatusServiceUnavailable)
  137. }
  138. }