http.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package http
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "go-common/app/service/main/account/conf"
  6. "go-common/app/service/main/account/model"
  7. "go-common/app/service/main/account/service"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. v "go-common/library/net/http/blademaster/middleware/verify"
  12. )
  13. var (
  14. accSvc *service.Service
  15. verify *v.Verify
  16. )
  17. // Init init account service.
  18. func Init(c *conf.Config, s *service.Service) {
  19. accSvc = s
  20. verify = v.New(c.Verify)
  21. // engine
  22. engine := bm.DefaultServer(c.BM)
  23. innerRouter(engine)
  24. // init inner server
  25. if err := engine.Start(); err != nil {
  26. log.Error("engine.Start() error(%v)", err)
  27. panic(err)
  28. }
  29. }
  30. func filterByAppkey(keys []string) func(*bm.Context) {
  31. allowed := make(map[string]struct{}, len(keys))
  32. for _, k := range keys {
  33. allowed[k] = struct{}{}
  34. }
  35. return func(ctx *bm.Context) {
  36. req := ctx.Request
  37. params := req.Form
  38. appkey := params.Get("appkey")
  39. if _, ok := allowed[appkey]; !ok {
  40. log.Error("appkey: %s try to access %s failed", appkey, req.URL)
  41. ctx.JSON(nil, ecode.AccessDenied)
  42. ctx.Abort()
  43. return
  44. }
  45. }
  46. }
  47. // innerRouter init inner router.
  48. func innerRouter(e *bm.Engine) {
  49. e.Ping(ping)
  50. e.Register(register)
  51. group := e.Group("/x/internal/v3/account", verify.Verify)
  52. {
  53. group.GET("/info", info)
  54. group.GET("/info/by/name", infoByName)
  55. group.GET("/infos", infos)
  56. group.GET("/card", card)
  57. group.GET("/cards", cards)
  58. group.GET("/vip", vip)
  59. group.GET("/vips", vips)
  60. group.GET("/profile", profile)
  61. group.GET("/profile/stat", profileWithStat)
  62. group.GET("/privacy", filterByAppkey(conf.Conf.AppkeyFilter.Privacy), privacy)
  63. group.GET("/cache/del", cacheDel)
  64. group.POST("/cache/clear", cacheClear)
  65. }
  66. v2Group := e.Group("/x/internal/account/v2", verify.Verify)
  67. {
  68. v2Group.GET("/myinfo", v2MyInfo)
  69. v2Group.GET("/userinfo", v2MyInfo)
  70. }
  71. v1Group := e.Group("/x/internal/account", verify.Verify)
  72. {
  73. v1Group.GET("/info", v1Info)
  74. v1Group.GET("/infos", v1Infos)
  75. v1Group.GET("/card", v1Card)
  76. v1Group.GET("/vip", v1Vip)
  77. }
  78. }
  79. // ping check server ok.
  80. func ping(c *bm.Context) {
  81. if err := accSvc.Ping(c); err != nil {
  82. log.Error("ping error(%v)", err)
  83. c.AbortWithStatus(http.StatusServiceUnavailable)
  84. }
  85. }
  86. // register support discovery.
  87. func register(c *bm.Context) {
  88. c.JSON(map[string]struct{}{}, nil)
  89. }
  90. // cache del
  91. func cacheDel(c *bm.Context) {
  92. p := new(model.ParamModify)
  93. if err := c.Bind(p); err != nil {
  94. return
  95. }
  96. c.JSON(nil, accSvc.DelCache(c, p.Mid, p.ModifiedAttr))
  97. }
  98. // cache clear
  99. func cacheClear(c *bm.Context) {
  100. p := new(model.ParamMsg)
  101. if err := c.Bind(p); err != nil {
  102. return
  103. }
  104. var m struct {
  105. New struct {
  106. Mid int64 `json:"mid"`
  107. } `json:"new,omitempty"`
  108. Mid int64 `json:"mid"`
  109. Action string `json:"action"`
  110. }
  111. if err := json.Unmarshal([]byte(p.Msg), &m); err != nil {
  112. c.JSON(nil, err)
  113. return
  114. }
  115. mid := m.Mid
  116. if mid == 0 {
  117. mid = m.New.Mid
  118. }
  119. if mid == 0 {
  120. log.Warn("cache clear no mid msg(%s)", p.Msg)
  121. return
  122. }
  123. log.Info("Try to delete cache with mid: %d and param: %+v", mid, p)
  124. c.JSON(nil, accSvc.DelCache(c, mid, m.Action))
  125. }