http.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package http
  2. import (
  3. "net/http"
  4. "go-common/app/interface/main/history/conf"
  5. "go-common/app/interface/main/history/model"
  6. "go-common/app/interface/main/history/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/auth"
  11. "go-common/library/net/http/blademaster/middleware/verify"
  12. )
  13. var (
  14. cnf *conf.Config
  15. hisSvc *service.Service
  16. collector *anticheat.AntiCheat
  17. authSvc *auth.Auth
  18. verifySvc *verify.Verify
  19. )
  20. // Init init http
  21. func Init(c *conf.Config, s *service.Service) {
  22. cnf = c
  23. authSvc = auth.New(c.Auth)
  24. verifySvc = verify.New(c.Verify)
  25. if c.Collector != nil {
  26. collector = anticheat.New(c.Collector)
  27. }
  28. hisSvc = s
  29. engine := bm.DefaultServer(c.BM)
  30. outerRouter(engine)
  31. interRouter(engine)
  32. if err := engine.Start(); err != nil {
  33. log.Error("xhttp.Serve error(%v)", err)
  34. panic(err)
  35. }
  36. }
  37. // outerRouter init outer router api path.
  38. func outerRouter(e *bm.Engine) {
  39. e.GET("/monitor/ping", ping)
  40. group := e.Group("/x/v2/history", authSvc.User)
  41. {
  42. group.GET("", history)
  43. group.POST("/add", addHistory)
  44. group.POST("/del", delHistory)
  45. group.POST("/delete", delete)
  46. group.POST("/clear", clearHistory)
  47. group.POST("/report", report) //just mobile use it.
  48. group.POST("/reports", reports) //just mobile use it.
  49. }
  50. toViewGroup := group.Group("/toview", authSvc.User)
  51. {
  52. toViewGroup.GET("", toView)
  53. toViewGroup.GET("/web", webToView)
  54. toViewGroup.GET("/remaining", remainingToView)
  55. toViewGroup.POST("/add", addToView)
  56. toViewGroup.POST("/adds", addMultiToView)
  57. toViewGroup.POST("/del", delToView)
  58. toViewGroup.POST("/clear", clearToView)
  59. }
  60. shadowGroup := group.Group("/shadow", authSvc.User)
  61. {
  62. shadowGroup.GET("", shadow)
  63. shadowGroup.POST("/set", setShadow)
  64. }
  65. }
  66. // interRouter init internal router api path using the outer router port
  67. func interRouter(e *bm.Engine) {
  68. group := e.Group("/x/internal/v2/history")
  69. {
  70. group.GET("/manager", verifySvc.Verify, managerHistory)
  71. group.GET("/aids", verifySvc.VerifyUser, aids) // bangumi use it.
  72. group.POST("/delete", verifySvc.VerifyUser, delete)
  73. group.POST("/clear", verifySvc.VerifyUser, clearHistory)
  74. group.POST("/flush", verifySvc.Verify, flush) // history-job use it.
  75. group.POST("/report", verifySvc.VerifyUser, innerReport) //just mobile use it.
  76. group.GET("/position", verifySvc.VerifyUser, position) //just mobile use it.
  77. group.GET("/resource", verifySvc.VerifyUser, resource) //just mobile use it.
  78. group.GET("/resource/type", verifySvc.VerifyUser, resources) //just mobile use it.
  79. }
  80. toviewGroup := group.Group("/toview")
  81. {
  82. toviewGroup.GET("/manager", verifySvc.Verify, managerToView)
  83. toviewGroup.POST("/adds", verifySvc.VerifyUser, addMultiToView) // space use it.
  84. }
  85. }
  86. // ping check server ok.
  87. func ping(c *bm.Context) {
  88. if err := hisSvc.Ping(c); err != nil {
  89. log.Error("history service ping error(%v)", err)
  90. c.AbortWithStatus(http.StatusServiceUnavailable)
  91. }
  92. }
  93. func business(c *bm.Context) (tp int8, ok bool) {
  94. ok = true
  95. business := c.Request.Form.Get("business")
  96. if business == "" {
  97. return
  98. }
  99. tp, err := model.CheckBusiness(business)
  100. if err != nil {
  101. ok = false
  102. c.JSON(nil, err)
  103. c.Abort()
  104. }
  105. return
  106. }