http.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package http
  2. import (
  3. "net/http"
  4. "go-common/app/interface/live/lottery-interface/internal/conf"
  5. "go-common/app/interface/live/lottery-interface/internal/service"
  6. v1 "go-common/app/interface/live/lottery-interface/internal/service/v1"
  7. risk "go-common/app/service/live/live_riskcontrol/api/grpc/v1"
  8. "go-common/library/log"
  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. "go-common/library/net/metadata"
  13. "go-common/library/net/http/blademaster/render"
  14. )
  15. var (
  16. vfy *verify.Verify
  17. )
  18. // Init init
  19. func Init(c *conf.Config) {
  20. vfy = verify.New(c.Verify)
  21. engine := bm.DefaultServer(c.BM)
  22. route(engine)
  23. if err := engine.Start(); err != nil {
  24. log.Error("bm Start error(%v)", err)
  25. panic(err)
  26. }
  27. }
  28. func route(e *bm.Engine) {
  29. e.Ping(ping)
  30. e.Register(register)
  31. auth := auth.New(&auth.Config{
  32. Identify: conf.Conf.ShortClient,
  33. DisableCSRF: true,
  34. })
  35. g := e.Group("/xlive/lottery-interface")
  36. {
  37. g.POST("/v1/storm/Join", abortGET, auth.Guest, riskControl, v1.StormJoin)
  38. g.GET("/v1/storm/Check", auth.Guest, v1.StormCheck)
  39. }
  40. }
  41. func abortGET(c *bm.Context) {
  42. if c.Request.Method == http.MethodGet {
  43. c.Render(http.StatusOK, render.MapJSON{
  44. "code": 0,
  45. "msg": "",
  46. "data": []int{},
  47. })
  48. c.Abort()
  49. return
  50. }
  51. }
  52. func riskControl(c *bm.Context) {
  53. mid, ok := c.Get("mid")
  54. if !ok {
  55. c.Render(http.StatusOK, render.MapJSON{
  56. "code": 401,
  57. "msg": "未登录",
  58. "data": []int{},
  59. })
  60. c.Abort()
  61. return
  62. }
  63. midi := mid.(int64)
  64. header := make(map[string]string)
  65. for k := range c.Request.Header {
  66. if v := c.Request.Header[k]; len(v) > 0 {
  67. header[k] = v[0]
  68. }
  69. }
  70. header["Platform"] = c.Request.URL.Query().Get("platform")
  71. resp, err := service.ServiceInstance.IsForbiddenClient.GetForbidden(c, &risk.GetForbiddenReq{
  72. Uid: midi,
  73. Uri: c.Request.URL.Path,
  74. Ip: metadata.String(c, metadata.RemoteIP),
  75. Method: c.Request.Method,
  76. Header: header,
  77. })
  78. if err != nil {
  79. log.Info("#call_IsForbiddenClient_fail %s", err.Error())
  80. c.Render(http.StatusOK, render.MapJSON{
  81. "code": 400,
  82. "msg": "没抢到",
  83. "data": []int{},
  84. })
  85. c.Abort()
  86. return
  87. }
  88. if resp.GetIsForbidden() == risk.GetForbiddenReply_FORBIDDEN {
  89. log.Info("#IsForbidden# mid= %d", midi)
  90. c.Render(http.StatusOK, render.MapJSON{
  91. "code": 400,
  92. "msg": "访问被拒绝",
  93. "data": []int{},
  94. })
  95. c.Abort()
  96. return
  97. }
  98. }
  99. func ping(ctx *bm.Context) {
  100. ctx.String(http.StatusOK, "PONG")
  101. }
  102. func register(c *bm.Context) {
  103. c.JSON(map[string]interface{}{}, nil)
  104. }