http.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package http
  2. import (
  3. uphttp "go-common/app/service/main/up/api/v1"
  4. "go-common/app/service/main/up/conf"
  5. "go-common/app/service/main/up/service"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. "go-common/library/net/http/blademaster/middleware/permit"
  9. "go-common/library/net/http/blademaster/middleware/verify"
  10. )
  11. var (
  12. idfSvc *verify.Verify
  13. //Svc service.
  14. Svc *service.Service
  15. authSrc *permit.Permit
  16. )
  17. // Init init account service.
  18. func Init(c *conf.Config, s *service.Service) {
  19. // service
  20. initService(c, s)
  21. // init internal router
  22. innerEngine := bm.DefaultServer(c.BM.Inner)
  23. setupInnerEngine(innerEngine)
  24. registerUpEngine(innerEngine)
  25. uphttp.RegisterUpBMServer(innerEngine, s)
  26. // init internal server
  27. if err := innerEngine.Start(); err != nil {
  28. log.Error("innerEngine.Start error(%v)", err)
  29. panic(err)
  30. }
  31. }
  32. func initService(c *conf.Config, s *service.Service) {
  33. idfSvc = verify.New(nil)
  34. Svc = s
  35. authSrc = permit.New(c.Auth)
  36. Svc.SetAuthServer(authSrc)
  37. }
  38. // registerUpEngine .
  39. func registerUpEngine(e *bm.Engine) {
  40. e.Inject("^/x/internal/uper/archive", idfSvc.Verify)
  41. }
  42. // innerRouter
  43. func setupInnerEngine(e *bm.Engine) {
  44. // monitor ping
  45. e.Ping(ping)
  46. e.Register(disRegister)
  47. // base
  48. var base, admin *bm.RouterGroup
  49. if conf.Conf.IsTest {
  50. base = e.Group("/x/internal/uper")
  51. admin = e.Group("/x/admin/uper")
  52. } else {
  53. base = e.Group("/x/internal/uper", idfSvc.Verify)
  54. // 现在只要登录,默认放过
  55. admin = e.Group("/x/admin/uper", authSrc.Verify(), authSrc.Permit("PRIORITY_UP"))
  56. }
  57. {
  58. base.POST("/register", register)
  59. base.GET("/info", info)
  60. base.GET("/all", all)
  61. base.GET("/special", specialUps)
  62. base.GET("/stat/base", baseStat)
  63. base.GET("/info/active", active)
  64. base.GET("/info/actives", actives)
  65. //播放器关注按钮开关
  66. base.POST("/switch/set", switchSet)
  67. base.GET("/switch", upSwitch)
  68. //人物卡片
  69. base.GET("/card/all", listCardBase)
  70. base.GET("/card/info", getCardInfo)
  71. base.GET("/card/info/list", listCardDetail)
  72. base.GET("/card/info/list_by_mids", listCardByMids)
  73. // 下面接口在admin中也有
  74. base.GET("/special/get", specialGet)
  75. base.GET("/group/get", getGroup)
  76. base.GET("/special/get_by_mid", specialGetByMid)
  77. // list_up
  78. base.GET("/list_up", listUp)
  79. }
  80. admin.GET("/special/get", specialGet)
  81. admin.GET("/special/get_by_mid", specialGetByMid)
  82. admin.POST("/special/delete", specialDel)
  83. admin.POST("/special/add", specialAdd)
  84. admin.POST("/special/edit", specialEdit)
  85. admin.GET("/group/get", getGroup)
  86. admin.POST("/group/add", authSrc.Permit("UPGROUP_ADD"), addGroup)
  87. admin.POST("/group/update", updateGroup)
  88. admin.POST("/group/delete", authSrc.Permit("UPGROUP_ADD"), removeGroup)
  89. }
  90. // ping check server ok.
  91. func ping(ctx *bm.Context) {
  92. if err := Svc.Ping(ctx); err != nil {
  93. ctx.Error = err
  94. ctx.AbortWithStatus(503)
  95. }
  96. }
  97. // disRegister check server ok.
  98. func disRegister(ctx *bm.Context) {
  99. ctx.JSON(map[string]interface{}{}, nil)
  100. }
  101. //bmGetStringOrDefault get string
  102. func bmGetStringOrDefault(c *bm.Context, key string, defaul string) (value string, exist bool) {
  103. i, exist := c.Get(key)
  104. if !exist {
  105. value = defaul
  106. return
  107. }
  108. value, exist = i.(string)
  109. if !exist {
  110. value = defaul
  111. }
  112. return
  113. }