http.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package http
  2. import (
  3. "errors"
  4. "net/http"
  5. "go-common/app/interface/live/push-live/conf"
  6. "go-common/app/interface/live/push-live/service"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. )
  11. var (
  12. pushLiveSvr *service.Service
  13. errInvalidParams = errors.New("invalid params")
  14. )
  15. // Init http
  16. func Init(c *conf.Config, srv *service.Service) {
  17. pushLiveSvr = srv
  18. // init router
  19. engineInner := bm.DefaultServer(c.BM.Inner)
  20. innerRouter(engineInner)
  21. if err := engineInner.Start(); err != nil {
  22. log.Error("bm.DefaultServer error(%v)", err)
  23. panic(err)
  24. }
  25. }
  26. // innerRouter init inner router api path.
  27. func innerRouter(e *bm.Engine) {
  28. // init api
  29. e.Ping(ping)
  30. // http internal api
  31. group := e.Group("/xlive/internal/push-live/")
  32. {
  33. // 用户每日推送额度占用
  34. group.POST("/limit/decrease", decrease)
  35. }
  36. }
  37. // ping check server ok.
  38. func ping(c *bm.Context) {
  39. if err := pushLiveSvr.Ping(c); err != nil {
  40. log.Error("[http.http|ping] push-live ping error(%v)", err)
  41. c.AbortWithStatus(http.StatusServiceUnavailable)
  42. }
  43. }
  44. // decrease decrease user daily push limit
  45. func decrease(c *bm.Context) {
  46. params := c.Request.Form
  47. business := params.Get("business")
  48. uuid := params.Get("uuid")
  49. targetID := params.Get("target_id")
  50. mids := params.Get("mids")
  51. // params check
  52. if business == "" || uuid == "" || targetID == "" || mids == "" {
  53. log.Error("[http.http|decrease] request params(%v) error(%v)", params, errInvalidParams)
  54. c.JSON(nil, ecode.RequestErr)
  55. return
  56. }
  57. if err := pushLiveSvr.LimitDecrease(c, business, targetID, uuid, mids); err != nil {
  58. log.Error("[http.http|decrease] pushLiveSvr.LimitDecrease error(%v), params(%v)", err, params)
  59. c.JSON(nil, ecode.RequestErr)
  60. return
  61. }
  62. log.Info("[http.http|decrease] decrease success, params(%v)", params)
  63. c.JSON(nil, nil)
  64. }