http.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package http
  2. import (
  3. "net/http"
  4. "go-common/app/interface/openplatform/article/conf"
  5. "go-common/app/interface/openplatform/article/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/auth"
  10. "go-common/library/net/http/blademaster/middleware/cache"
  11. "go-common/library/net/http/blademaster/middleware/cache/store"
  12. "go-common/library/net/http/blademaster/middleware/verify"
  13. )
  14. var (
  15. artSrv *service.Service
  16. authSvr *auth.Auth
  17. vfySvr *verify.Verify
  18. antispamM *antispam.Antispam
  19. // cache components
  20. cacheSvr *cache.Cache
  21. deg *cache.Degrader
  22. )
  23. // Init init
  24. func Init(c *conf.Config, s *service.Service) {
  25. authSvr = auth.New(c.Auth)
  26. vfySvr = verify.New(c.Verify)
  27. artSrv = s
  28. antispamM = antispam.New(c.Antispam)
  29. cacheSvr = cache.New(store.NewMemcache(c.DegradeConfig.Memcache))
  30. deg = cache.NewDegrader(c.DegradeConfig.Expire)
  31. // init outer router
  32. engine := bm.DefaultServer(c.BM)
  33. outerRouter(engine)
  34. if err := engine.Start(); err != nil {
  35. log.Error("engine.Start error(%v)", err)
  36. panic(err)
  37. }
  38. }
  39. // outerRouter init outer router
  40. func outerRouter(r *bm.Engine) {
  41. r.Ping(ping)
  42. r.Register(register)
  43. cr := r.Group("/x/article")
  44. {
  45. cr.GET("/recommends", authSvr.Guest, recommends)
  46. cr.GET("/recommends/plus", authSvr.Guest, recommendsPlus)
  47. cr.GET("/home", authSvr.Guest, cacheSvr.Cache(deg.Args("pn", "ps", "device", "mobi_app", "build"), nil), home)
  48. cr.GET("/view", authSvr.Guest, view)
  49. cr.GET("/metas", metas)
  50. cr.GET("/card", card)
  51. cr.GET("/cards", cards)
  52. cr.GET("/notice", notice)
  53. cr.GET("/addview", authSvr.Guest, addView)
  54. cr.POST("/addshare", authSvr.Guest, addShare)
  55. cr.GET("/viewinfo", authSvr.Guest, viewInfo)
  56. cr.GET("/actinfo", actInfo)
  57. cr.POST("/like", authSvr.User, like)
  58. cr.GET("/applyinfo", authSvr.Guest, applyInfo)
  59. cr.GET("/is_author", authSvr.User, isAuthor)
  60. cr.POST("/author/add", authSvr.User, addAuthor)
  61. cr.POST("/apply", authSvr.User, apply)
  62. cr.POST("/complaints", authSvr.User, addComplaint)
  63. cr.GET("/list", list)
  64. cr.GET("/categories", categories)
  65. cr.GET("/anniversary", authSvr.User, anniversaryInfo)
  66. cr.GET("/sentinel/config", authSvr.Guest, sentinel)
  67. ccr := cr.Group("/favorites", authSvr.User)
  68. {
  69. ccr.POST("/add", addFavorite)
  70. ccr.POST("/del", delFavorite)
  71. ccr.GET("/list", favorites)
  72. ccr.GET("/list/all", allFavorites)
  73. }
  74. cr.GET("/archives", archives)
  75. cr.GET("/early", earlyArticles)
  76. cr.GET("/more", authSvr.Guest, moreArts)
  77. ccr = cr.Group("/rank")
  78. {
  79. ccr.GET("/categories", rankCategories)
  80. ccr.GET("/list", authSvr.Guest, ranks)
  81. }
  82. ccr = cr.Group("/user", authSvr.User)
  83. {
  84. ccr.GET("/notice", userNotice)
  85. ccr.POST("/notice/update", updateUserNotice)
  86. }
  87. // read list
  88. cr.GET("/list/articles", authSvr.Guest, listArticles)
  89. cr.GET("/list/web/articles", authSvr.Guest, webListArticles)
  90. cr.GET("/listinfo", listInfo)
  91. cr.GET("/up/lists", upLists)
  92. cr.GET("/hotspots", authSvr.Guest, hotspotArts)
  93. cr.GET("/authors", authSvr.User, authors)
  94. ccr = cr.Group("/creative", authSvr.User)
  95. {
  96. cr1 := ccr.Group("/list")
  97. {
  98. cr1.GET("/all", lists)
  99. cr1.POST("/add", addList)
  100. cr1.POST("/del", delList)
  101. cr1.POST("/update", updateListArticles)
  102. cr1.GET("/articles/all", listAllArticles)
  103. cr1.GET("/articles/can_add", canAddArts)
  104. cr1.POST("/articles/update", updateArticleList)
  105. }
  106. // creative article
  107. ccr.POST("/article/submit", webSubArticle)
  108. ccr.POST("/article/update", webUpdateArticle)
  109. ccr.POST("/draft/addupdate", webSubmitDraft)
  110. ccr.GET("/draft/view", webDraft)
  111. ccr.GET("/draft/list", webDraftList)
  112. ccr.GET("/draft/count", draftCount)
  113. ccr.GET("/article/view", webArticle)
  114. ccr.GET("/article/list", webArticleList)
  115. ccr.GET("/app/pre", creatorArticlePre)
  116. ccr.POST("/upload/image", antispamM.ServeHTTP, uploadImage)
  117. ccr.POST("/draft/delete", deleteDraft)
  118. ccr.POST("/article/delete", delArticle)
  119. ccr.POST("/article/withdraw", withdrawArticle)
  120. ccr.POST("/article/capture", antispamM.ServeHTTP, articleCapture)
  121. ccr.POST("/segment", segment)
  122. }
  123. // article read ping for timing
  124. cr.GET("/read/ping", authSvr.Guest, readPing)
  125. }
  126. cr = r.Group("/x/internal/article", vfySvr.Verify)
  127. {
  128. cr.GET("/meta", meta)
  129. cr.GET("/metas", metas)
  130. cr.GET("/list", list)
  131. cr.GET("/view", view)
  132. cr.GET("/recommends/all", allRecommends)
  133. cr.POST("/refresh_list", refreshLists)
  134. cr.POST("/rebuild_allrc", rebuildAllListReadCount)
  135. cr.POST("/lock", addCheatFilter)
  136. cr.POST("/unlock", delCheatFilter)
  137. }
  138. }
  139. func ping(c *bm.Context) {
  140. if err := artSrv.Ping(c); err != nil {
  141. log.Error("ping error(%v)", err)
  142. c.AbortWithStatus(http.StatusServiceUnavailable)
  143. }
  144. }
  145. func register(c *bm.Context) {
  146. c.JSON(map[string]interface{}{}, nil)
  147. }
  148. func buvid(c *bm.Context) string {
  149. buvid := c.Request.Header.Get(_headerBuvid)
  150. if buvid == "" {
  151. cookie, _ := c.Request.Cookie(_buvid)
  152. if cookie != nil {
  153. buvid = cookie.Value
  154. }
  155. }
  156. return buvid
  157. }