http.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package http
  2. import (
  3. grpc "go-common/app/service/bbq/push/api/grpc/v1"
  4. "go-common/app/service/bbq/push/api/http/v1"
  5. "go-common/app/service/bbq/push/conf"
  6. "go-common/app/service/bbq/push/service"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. "go-common/library/net/http/blademaster/middleware/verify"
  10. "github.com/json-iterator/go"
  11. "github.com/pkg/errors"
  12. )
  13. var (
  14. srv *service.Service
  15. vfy *verify.Verify
  16. )
  17. // Init init
  18. func Init(c *conf.Config, svc *service.Service) {
  19. srv = svc
  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. g := e.Group("/bbq/internal/push")
  32. {
  33. g.POST("/notice", notice)
  34. g.POST("/message", message)
  35. }
  36. }
  37. func ping(c *bm.Context) {
  38. c.String(0, "pong")
  39. }
  40. func register(c *bm.Context) {
  41. c.JSON(map[string]interface{}{}, nil)
  42. }
  43. func notice(c *bm.Context) {
  44. args := &v1.NoticeRequest{}
  45. if err := c.Bind(args); err != nil {
  46. errors.Wrap(err, "参数验证失败")
  47. return
  48. }
  49. devs := []*grpc.Device{
  50. {
  51. RegisterID: args.RegID,
  52. Platform: args.Platform,
  53. SDK: args.SDK,
  54. SendNo: 0,
  55. },
  56. }
  57. ext := make(map[string]string)
  58. ext["schema"] = args.Schema
  59. ext["callback"] = args.Callback
  60. extBytes, _ := jsoniter.Marshal(ext)
  61. req := &grpc.NotificationRequest{
  62. Dev: devs,
  63. Body: &grpc.NotificationBody{
  64. Title: args.Title,
  65. Content: args.Content,
  66. Extra: string(extBytes),
  67. },
  68. }
  69. resp, err := srv.Notification(c, req)
  70. if err != nil {
  71. log.Errorv(c, log.KV("error", err))
  72. }
  73. c.String(0, resp.String())
  74. }
  75. func message(c *bm.Context) {
  76. args := &v1.MessageRequest{}
  77. if err := c.Bind(args); err != nil {
  78. errors.Wrap(err, "参数验证失败")
  79. return
  80. }
  81. devs := []*grpc.Device{
  82. {
  83. RegisterID: args.RegID,
  84. Platform: args.Platform,
  85. SDK: args.SDK,
  86. SendNo: 0,
  87. },
  88. }
  89. ext := make(map[string]string)
  90. ext["schema"] = args.Schema
  91. ext["callback"] = args.Callback
  92. extBytes, _ := jsoniter.Marshal(ext)
  93. req := &grpc.MessageRequest{
  94. Dev: devs,
  95. Body: &grpc.MessageBody{
  96. Title: args.Title,
  97. Content: args.Content,
  98. ContentType: args.ContentType,
  99. Extra: string(extBytes),
  100. },
  101. }
  102. resp, err := srv.Message(c, req)
  103. if err != nil {
  104. log.Errorv(c, log.KV("error", err))
  105. }
  106. c.String(0, resp.String())
  107. }