http.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package http
  2. import (
  3. "net/http"
  4. "github.com/pkg/errors"
  5. "go-common/library/log"
  6. bm "go-common/library/net/http/blademaster"
  7. "go-common/library/net/http/blademaster/middleware/verify"
  8. "go-common/app/service/bbq/notice-service/api/v1"
  9. "go-common/app/service/bbq/notice-service/internal/conf"
  10. "go-common/app/service/bbq/notice-service/internal/service"
  11. )
  12. var (
  13. vfy *verify.Verify
  14. svc *service.Service
  15. )
  16. // Init init
  17. func Init(c *conf.Config, s *service.Service) {
  18. svc = s
  19. vfy = verify.New(c.Verify)
  20. engine := bm.DefaultServer(c.BM)
  21. route(engine)
  22. if err := engine.Start(); err != nil {
  23. log.Error("bm Start error(%v)", err)
  24. panic(err)
  25. }
  26. }
  27. func route(e *bm.Engine) {
  28. e.Ping(ping)
  29. e.Register(register)
  30. g := e.Group("/x/notice-service")
  31. {
  32. g.GET("/notice/list", listNotices)
  33. g.GET("/notice/unread", unreadInfo)
  34. g.POST("/notice/create", createNotice)
  35. g.POST("/push/login", login)
  36. g.POST("/push/logout", logout)
  37. }
  38. }
  39. func ping(ctx *bm.Context) {
  40. if err := svc.Ping(ctx); err != nil {
  41. log.Error("ping error(%v)", err)
  42. ctx.AbortWithStatus(http.StatusServiceUnavailable)
  43. }
  44. }
  45. func register(c *bm.Context) {
  46. c.JSON(map[string]interface{}{}, nil)
  47. }
  48. func createNotice(c *bm.Context) {
  49. arg := &v1.NoticeBase{}
  50. if err := c.Bind(arg); err != nil {
  51. errors.Wrap(err, "参数验证失败")
  52. return
  53. }
  54. resp, err := svc.CreateNotice(c, arg)
  55. c.JSON(resp, err)
  56. }
  57. func listNotices(c *bm.Context) {
  58. arg := &v1.ListNoticesReq{}
  59. if err := c.Bind(arg); err != nil {
  60. errors.Wrap(err, "参数验证失败")
  61. return
  62. }
  63. resp, err := svc.ListNotices(c, arg)
  64. c.JSON(resp, err)
  65. }
  66. func unreadInfo(c *bm.Context) {
  67. arg := &v1.GetUnreadInfoRequest{}
  68. if err := c.Bind(arg); err != nil {
  69. errors.Wrap(err, "参数验证失败")
  70. return
  71. }
  72. resp, err := svc.GetUnreadInfo(c, arg)
  73. c.JSON(resp, err)
  74. }
  75. func login(c *bm.Context) {
  76. arg := &v1.UserPushDev{}
  77. if err := c.Bind(arg); err != nil {
  78. errors.Wrap(err, "参数验证失败")
  79. return
  80. }
  81. resp, err := svc.PushLogin(c, arg)
  82. c.JSON(resp, err)
  83. }
  84. func logout(c *bm.Context) {
  85. arg := &v1.UserPushDev{}
  86. if err := c.Bind(arg); err != nil {
  87. errors.Wrap(err, "参数验证失败")
  88. return
  89. }
  90. resp, err := svc.PushLogout(c, arg)
  91. c.JSON(resp, err)
  92. }