notify.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package http
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "go-common/app/service/video/stream-mng/model"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "io/ioutil"
  11. )
  12. func openNotify(c *bm.Context) {
  13. p, err := parseNotifyBody(c)
  14. if err != nil {
  15. // log.Errorv(c, log.KV("log", fmt.Sprintf("open_notify_err = %v", err)))
  16. c.JSONMap(map[string]interface{}{"msg": err.Error()}, ecode.RequestErr)
  17. c.Set("output_data", err.Error())
  18. return
  19. }
  20. err = srv.StreamingNotify(c, p, true)
  21. if err != nil {
  22. // log.Errorv(c, log.KV("log", fmt.Sprintf("open_notify_err = %v", err)))
  23. c.JSONMap(map[string]interface{}{"msg": err.Error()}, ecode.RequestErr)
  24. c.Set("output_data", err.Error())
  25. // c.Abort()
  26. return
  27. }
  28. c.Set("output_data", "success")
  29. c.JSONMap(map[string]interface{}{"msg": "success"}, ecode.OK)
  30. }
  31. func closeNotify(c *bm.Context) {
  32. p, err := parseNotifyBody(c)
  33. if err != nil {
  34. c.JSONMap(map[string]interface{}{"msg": err.Error()}, ecode.RequestErr)
  35. c.Set("output_data", err.Error())
  36. return
  37. }
  38. err = srv.StreamingNotify(c, p, false)
  39. if err != nil {
  40. c.JSONMap(map[string]interface{}{"msg": err.Error()}, ecode.RequestErr)
  41. c.Set("output_data", err.Error())
  42. return
  43. }
  44. c.Set("output_data", "success")
  45. c.JSONMap(map[string]interface{}{"msg": "success"}, ecode.OK)
  46. }
  47. func parseNotifyBody(c *bm.Context) (*model.StreamingNotifyParam, error) {
  48. // log.Info("%v %v", c.Request.Header.Get("Content-Type"), c.Request.Form)
  49. switch c.Request.Header.Get("Content-Type") {
  50. case "application/x-www-form-urlencoded":
  51. // log.Info("%+v", c.Request.PostForm)
  52. if len(c.Request.PostForm) == 0 {
  53. return nil, errors.New("empty post body")
  54. }
  55. p := &model.StreamingNotifyParam{}
  56. p.Key = c.Request.PostFormValue("key")
  57. p.Sign = c.Request.PostFormValue("sign")
  58. p.SRC = c.Request.PostFormValue("src")
  59. p.StreamName = c.Request.PostFormValue("stream_name")
  60. p.SUID = c.Request.PostFormValue("suid")
  61. p.TS = json.Number(c.Request.PostFormValue("ts"))
  62. p.Type = json.Number(c.Request.PostFormValue("type"))
  63. // log.Info("%+v", p)
  64. // log.Infov(c, log.KV("log", fmt.Sprintf("notify_input = %+v", p)))
  65. c.Set("input_params", *p)
  66. return p, nil
  67. default:
  68. defer c.Request.Body.Close()
  69. b, err := ioutil.ReadAll(c.Request.Body)
  70. if err != nil {
  71. return nil, err
  72. }
  73. if len(b) == 0 {
  74. return nil, errors.New("empty body")
  75. }
  76. var snp model.StreamingNotifyParam
  77. err = json.Unmarshal(b, &snp)
  78. if err != nil {
  79. log.Errorv(c, log.KV("log", fmt.Sprintf("notify_parse_body_error = %v", err)))
  80. return &snp, errors.New("invalid json body")
  81. }
  82. // log.Infov(c, log.KV("log", fmt.Sprintf("notify_input = %+v", snp)))
  83. c.Set("input_params", snp)
  84. return &snp, nil
  85. }
  86. }