http.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package http
  2. import (
  3. "net/http"
  4. "strings"
  5. "go-common/app/service/main/rank/conf"
  6. "go-common/app/service/main/rank/model"
  7. "go-common/app/service/main/rank/service"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/http/blademaster/middleware/verify"
  11. )
  12. var (
  13. srv *service.Service
  14. vfy *verify.Verify
  15. )
  16. // Init init
  17. func Init(c *conf.Config, s *service.Service) {
  18. srv = s
  19. vfy = verify.New(c.Verify)
  20. engine := bm.DefaultServer(c.BM)
  21. router(engine)
  22. if err := engine.Start(); err != nil {
  23. log.Error("engine.Start() error(%v)", err)
  24. panic(err)
  25. }
  26. }
  27. func router(e *bm.Engine) {
  28. e.Ping(ping)
  29. e.Register(register)
  30. g := e.Group("/x/internal/rank")
  31. {
  32. g.GET("/do", do)
  33. g.GET("/mget", mget)
  34. g.GET("/sort", sort)
  35. g.GET("/group", vfy.Verify, group)
  36. }
  37. }
  38. func ping(c *bm.Context) {
  39. if err := srv.Ping(c); err != nil {
  40. log.Error("ping error(%v)", err)
  41. c.AbortWithStatus(http.StatusServiceUnavailable)
  42. }
  43. }
  44. func register(c *bm.Context) {
  45. c.JSON(map[string]interface{}{}, nil)
  46. }
  47. func do(c *bm.Context) {
  48. arg := new(model.DoReq)
  49. if err := c.Bind(arg); err != nil {
  50. return
  51. }
  52. c.JSON(srv.Do(c, arg), nil)
  53. }
  54. func mget(c *bm.Context) {
  55. arg := new(model.MgetReq)
  56. if err := c.Bind(arg); err != nil {
  57. return
  58. }
  59. c.JSON(srv.Mget(c, arg))
  60. }
  61. func sort(c *bm.Context) {
  62. arg := new(struct {
  63. Business string `form:"business" validate:"required"`
  64. Field string `form:"field" validate:"required"`
  65. Order string `form:"order" validate:"required"`
  66. Filters []string `form:"filters,split"`
  67. Oids []int64 `form:"oids,split" validate:"required"`
  68. Pn int `form:"pn"`
  69. Ps int `form:"ps"`
  70. })
  71. if err := c.Bind(arg); err != nil {
  72. return
  73. }
  74. filterMap := make(map[string]string)
  75. for _, v := range arg.Filters {
  76. strs := strings.Split(v, "|")
  77. if len(strs) == 2 {
  78. filterMap[strs[0]] = strs[1]
  79. }
  80. }
  81. a := new(model.SortReq)
  82. a.Business = arg.Business
  83. a.Field = arg.Field
  84. a.Order = arg.Order
  85. a.Filters = filterMap
  86. a.Oids = arg.Oids
  87. a.Pn = arg.Pn
  88. a.Ps = arg.Ps
  89. c.JSON(srv.Sort(c, a))
  90. }
  91. func group(c *bm.Context) {
  92. arg := new(model.GroupReq)
  93. if err := c.Bind(arg); err != nil {
  94. return
  95. }
  96. c.JSON(srv.Group(c, arg))
  97. }