http.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package http
  2. import (
  3. "net/http"
  4. "go-common/app/service/main/relation/conf"
  5. "go-common/app/service/main/relation/service"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. "go-common/library/net/http/blademaster/middleware/antispam"
  9. "go-common/library/net/http/blademaster/middleware/rate"
  10. v "go-common/library/net/http/blademaster/middleware/verify"
  11. "go-common/library/net/metadata"
  12. )
  13. var (
  14. anti *antispam.Antispam
  15. relationSvc *service.Service
  16. verify *v.Verify
  17. addFollowingRate *rate.Limiter
  18. )
  19. // Init init http sever instance.
  20. func Init(c *conf.Config, s *service.Service) {
  21. relationSvc = s
  22. verify = v.New(c.Verify)
  23. anti = antispam.New(c.Antispam)
  24. addFollowingRate = rate.New(c.AddFollowingRate)
  25. // init inner router
  26. engine := bm.DefaultServer(c.BM)
  27. setupInnerEngine(engine)
  28. if err := engine.Start(); err != nil {
  29. log.Error("engine.Start() error(%v)", err)
  30. panic(err)
  31. }
  32. }
  33. // innerRouter init inner router.
  34. func setupInnerEngine(e *bm.Engine) {
  35. // health check
  36. e.Ping(ping)
  37. e.Register(register)
  38. //new defined api lists
  39. g := e.Group("/x/internal/relation", verify.Verify)
  40. // relation
  41. g.GET("", relation)
  42. g.GET("/relations", relations)
  43. // stat
  44. g.GET("/stat", stat)
  45. g.GET("/stats", stats)
  46. // private api
  47. g.POST("/stat/set", setStat)
  48. g.POST("/tag/cache/del", delTagCache)
  49. g.GET("/tag/special", special)
  50. // following group
  51. following := g.Group("/following")
  52. following.GET("/followings", followings)
  53. following.GET("/same/followings", sameFollowings)
  54. following.POST("/add", addFollowingRate.Handler(), anti.ServeHTTP, addFollowing)
  55. following.POST("/del", anti.ServeHTTP, delFollowing)
  56. // whisper group
  57. whisper := g.Group("/whisper")
  58. whisper.GET("/whispers", whispers)
  59. whisper.POST("/add", anti.ServeHTTP, addWhisper)
  60. whisper.POST("/del", anti.ServeHTTP, delWhisper)
  61. // black group
  62. black := g.Group("/black")
  63. black.GET("/blacks", blacks)
  64. black.POST("/add", anti.ServeHTTP, addBlack)
  65. black.POST("/del", anti.ServeHTTP, delBlack)
  66. // follower group
  67. follower := g.Group("/follower")
  68. follower.GET("/followers", followers)
  69. follower.POST("/del", anti.ServeHTTP, delFollower)
  70. // recommend group
  71. // recommend := g.Group("/recommend")
  72. // recommend.GET("/global/hot", globalHot)
  73. // cache group
  74. cache := g.Group("/cache")
  75. cache.POST("/following/del", delFollowingCache)
  76. cache.POST("/following/update", updateFollowingCache)
  77. cache.POST("/follower/del", delFollowerCache)
  78. cache.POST("/stat/del", delStatCache)
  79. // cache group
  80. admin := g.Group("/admin")
  81. admin.POST("/monitor/add", addMonitor)
  82. admin.POST("/monitor/del", delMonitor)
  83. admin.GET("/monitor/load", loadMonitor)
  84. }
  85. // ping check server ok.
  86. func ping(c *bm.Context) {
  87. if err := relationSvc.Ping(c); err != nil {
  88. log.Error("service ping error(%v)", err)
  89. c.Writer.WriteHeader(http.StatusServiceUnavailable)
  90. }
  91. }
  92. // register check server ok.
  93. func register(c *bm.Context) {
  94. c.JSON(map[string]struct{}{}, nil)
  95. }
  96. func infocArg(c *bm.Context) (arg map[string]string) {
  97. var (
  98. ua string
  99. referer string
  100. sid string
  101. req = c.Request
  102. )
  103. ua = req.Header.Get(service.RelInfocUA)
  104. referer = req.Header.Get(service.RelInfocReferer)
  105. sidCookie, err := req.Cookie(service.RelInfocSid)
  106. if err != nil {
  107. log.Warn("relation infoc get sid failed error(%v)", err)
  108. } else {
  109. sid = sidCookie.Value
  110. }
  111. buvid := req.Header.Get(service.RelInfocHeaderBuvid)
  112. if buvid == "" {
  113. buvidCookie, _ := req.Cookie(service.RelInfocCookieBuvid)
  114. if buvidCookie != nil {
  115. buvid = buvidCookie.Value
  116. }
  117. }
  118. arg = map[string]string{
  119. service.RelInfocIP: metadata.String(c, metadata.RemoteIP),
  120. service.RelInfocReferer: referer,
  121. service.RelInfocSid: sid,
  122. service.RelInfocBuvid: buvid,
  123. service.RelInfocUA: ua,
  124. }
  125. return
  126. }