http.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package http
  2. import (
  3. "net/http"
  4. "go-common/app/service/main/thumbup/conf"
  5. "go-common/app/service/main/thumbup/service"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. "go-common/library/net/http/blademaster/middleware/rate"
  9. "go-common/library/net/http/blademaster/middleware/verify"
  10. )
  11. var (
  12. likeSrv *service.Service
  13. verifySrv *verify.Verify
  14. )
  15. // Init init
  16. func Init(c *conf.Config, s *service.Service) {
  17. verifySrv = verify.New(c.Verify)
  18. rateLimit := rate.New(c.Rate)
  19. likeSrv = s
  20. // init outer router
  21. engineOuter := bm.DefaultServer(c.BM)
  22. engineOuter.Use(rateLimit)
  23. outerRouter(engineOuter)
  24. if err := engineOuter.Start(); err != nil {
  25. log.Error("engine.Start error(%v)", err)
  26. panic(err)
  27. }
  28. }
  29. // outerRouter init outer router
  30. func outerRouter(r *bm.Engine) {
  31. r.Ping(ping)
  32. r.Register(register)
  33. cr := r.Group("/x/internal/thumbup", verifySrv.Verify)
  34. {
  35. cr.GET("/stats", stats)
  36. cr.POST("/multi_stats", multiStats)
  37. cr.GET("/user_likes", userLikes)
  38. cr.GET("/item_likes", itemLikes)
  39. cr.POST("/like", like)
  40. cr.GET("/has_like", hasLike)
  41. cr.POST("/update_count", updateCount)
  42. cr.GET("/raw_stats", rawStats)
  43. cr.POST("/update_upmids", updateUpMids)
  44. cr.POST("/item_has_like", itemHasLike)
  45. }
  46. }
  47. func ping(c *bm.Context) {
  48. if err := likeSrv.Ping(c); err != nil {
  49. log.Error("ping error(%v)", err)
  50. c.AbortWithStatus(http.StatusServiceUnavailable)
  51. }
  52. }
  53. func register(c *bm.Context) {
  54. c.JSON(map[string]interface{}{}, nil)
  55. }