http.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package http
  2. import (
  3. "github.com/pkg/errors"
  4. "net/http"
  5. "go-common/library/log"
  6. bm "go-common/library/net/http/blademaster"
  7. "go-common/library/net/http/blademaster/binding"
  8. "go-common/library/net/http/blademaster/middleware/verify"
  9. "go-common/app/service/bbq/sys-msg/api/v1"
  10. "go-common/app/service/bbq/sys-msg/internal/conf"
  11. "go-common/app/service/bbq/sys-msg/internal/service"
  12. )
  13. var (
  14. vfy *verify.Verify
  15. svc *service.Service
  16. )
  17. // Init init
  18. func Init(c *conf.Config, s *service.Service) {
  19. svc = s
  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("/x/sys-msg")
  32. {
  33. g.GET("/msg/list", listSysMsg)
  34. g.POST("/msg/create", createSysMsg)
  35. g.POST("/msg/update", updateSysMsg)
  36. }
  37. }
  38. func ping(ctx *bm.Context) {
  39. if err := svc.Ping(ctx); err != nil {
  40. log.Error("ping error(%v)", err)
  41. ctx.AbortWithStatus(http.StatusServiceUnavailable)
  42. }
  43. }
  44. func register(c *bm.Context) {
  45. c.JSON(map[string]interface{}{}, nil)
  46. }
  47. func createSysMsg(c *bm.Context) {
  48. arg := &v1.SysMsg{}
  49. if err := c.BindWith(arg, binding.JSON); err != nil {
  50. errors.Wrap(err, "参数验证失败")
  51. return
  52. }
  53. resp, err := svc.CreateSysMsg(c, arg)
  54. c.JSON(resp, err)
  55. }
  56. func updateSysMsg(c *bm.Context) {
  57. arg := &v1.SysMsg{}
  58. if err := c.BindWith(arg, binding.JSON); err != nil {
  59. errors.Wrap(err, "参数验证失败")
  60. return
  61. }
  62. resp, err := svc.UpdateSysMsg(c, arg)
  63. c.JSON(resp, err)
  64. }
  65. func listSysMsg(c *bm.Context) {
  66. arg := &v1.ListSysMsgReq{}
  67. if err := c.BindWith(arg, binding.Query); err != nil {
  68. errors.Wrap(err, "参数验证失败")
  69. return
  70. }
  71. resp, err := svc.ListSysMsg(c, arg)
  72. c.JSON(resp, err)
  73. }