http.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package http
  2. import (
  3. "net/http"
  4. "strconv"
  5. "go-common/app/service/main/favorite/conf"
  6. "go-common/app/service/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/supervisor"
  12. "go-common/library/net/http/blademaster/middleware/verify"
  13. )
  14. var (
  15. favSvc *service.Service
  16. verifySvc *verify.Verify
  17. antispamM *antispam.Antispam
  18. supervisorM *supervisor.Supervisor
  19. collector *anticheat.AntiCheat
  20. )
  21. // Init init router
  22. func Init(c *conf.Config, svc *service.Service) {
  23. verifySvc = verify.New(c.Verify)
  24. antispamM = antispam.New(c.Antispam)
  25. supervisorM = supervisor.New(c.Supervisor)
  26. favSvc = svc
  27. if c.Infoc2 != nil {
  28. collector = anticheat.New(c.Infoc2)
  29. }
  30. // init outer router
  31. engineOut := bm.DefaultServer(c.BM)
  32. internalRouter(engineOut)
  33. // init serve
  34. if err := engineOut.Start(); err != nil {
  35. log.Error("engineOut.Start() error(%v)", err)
  36. panic(err)
  37. }
  38. }
  39. // internalRouter init internal router api path
  40. func internalRouter(e *bm.Engine) {
  41. // init api
  42. e.Ping(ping)
  43. e.Register(register)
  44. favV3 := e.Group("/x/internal/v3/fav")
  45. {
  46. favV3.GET("", verifySvc.Verify, setMid, Favorites)
  47. favV3.GET("/test", setMid, Favorites)
  48. favV3.GET("/tlists", verifySvc.Verify, setMid, tlists)
  49. favV3.GET("/recents", verifySvc.VerifyUser, recentFavs)
  50. favV3.GET("/batch", verifySvc.Verify, batchFavs)
  51. favV3.POST("/add", verifySvc.VerifyUser, addFav)
  52. favV3.POST("/del", verifySvc.VerifyUser, delFav)
  53. favV3.POST("/madd", verifySvc.VerifyUser, multiAddFavs)
  54. favV3.POST("/mdel", verifySvc.VerifyUser, multiDelFavs)
  55. favV3.POST("/move", verifySvc.VerifyUser, moveFavs)
  56. favV3.POST("/copy", verifySvc.VerifyUser, copyFavs)
  57. favV3.GET("/favored", verifySvc.VerifyUser, isFavored)
  58. favV3.GET("/favoreds", verifySvc.VerifyUser, isFavoreds)
  59. favV3.GET("/users", verifySvc.Verify, userList)
  60. favV3.GET("/count", verifySvc.Verify, oidCount)
  61. favV3.GET("/counts", verifySvc.Verify, oidsCount)
  62. favV3.GET("/default", verifySvc.VerifyUser, inDefaultFolder)
  63. }
  64. folderV3 := e.Group("/x/internal/v3/fav/folder")
  65. {
  66. folderV3.GET("", verifySvc.Verify, setMid, userFolders)
  67. folderV3.GET("/multi", verifySvc.Verify, folders)
  68. folderV3.GET("/info", verifySvc.Verify, folderInfo)
  69. folderV3.GET("/count", verifySvc.Verify, cntUserFolders)
  70. folderV3.POST("/add", verifySvc.VerifyUser, addFolder)
  71. folderV3.POST("/update", verifySvc.VerifyUser, updateFolder)
  72. folderV3.POST("/del", verifySvc.VerifyUser, delFolder)
  73. folderV3.POST("/rename", verifySvc.VerifyUser, renameFolder)
  74. folderV3.POST("/public", verifySvc.VerifyUser, upAttrFolder)
  75. folderV3.POST("/sort", verifySvc.VerifyUser, sortFolders)
  76. folderV3.GET("/cleaned", verifySvc.VerifyUser, isCleaned)
  77. folderV3.POST("/clean", verifySvc.VerifyUser, cleanInvalidFavs)
  78. }
  79. }
  80. func setMid(c *bm.Context) {
  81. var (
  82. err error
  83. mid int64
  84. )
  85. req := c.Request
  86. midStr := req.Form.Get("mid")
  87. if midStr != "" {
  88. mid, err = strconv.ParseInt(midStr, 10, 64)
  89. if err != nil {
  90. c.JSON(nil, err)
  91. c.Abort()
  92. return
  93. }
  94. }
  95. c.Set("mid", mid)
  96. }
  97. // ping check server ok.
  98. func ping(c *bm.Context) {
  99. if err := favSvc.Ping(c); err != nil {
  100. log.Error("favorite http service ping error(%v)", err)
  101. c.AbortWithStatus(http.StatusServiceUnavailable)
  102. }
  103. }
  104. // register check server ok.
  105. func register(c *bm.Context) {
  106. c.JSON(map[string]interface{}{}, nil)
  107. }