http.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package http
  2. import (
  3. "net/http"
  4. "go-common/app/interface/main/playlist/conf"
  5. "go-common/app/interface/main/playlist/service"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. "go-common/library/net/http/blademaster/middleware/auth"
  9. "go-common/library/net/http/blademaster/middleware/verify"
  10. )
  11. var (
  12. plSvc *service.Service
  13. authSvr *auth.Auth
  14. vfySvr *verify.Verify
  15. )
  16. // Init init http server
  17. func Init(c *conf.Config, s *service.Service) {
  18. authSvr = auth.New(c.Auth)
  19. vfySvr = verify.New(c.Verify)
  20. plSvc = s
  21. engine := bm.DefaultServer(c.HTTPServer)
  22. outerRouter(engine)
  23. internalRouter(engine)
  24. if err := engine.Start(); err != nil {
  25. log.Error("engine.Start error(%v)", err)
  26. panic(err)
  27. }
  28. }
  29. // outerRouter init outer router api path.
  30. func outerRouter(e *bm.Engine) {
  31. // init api
  32. e.Ping(ping)
  33. group := e.Group("/x/playlist", bm.CORS())
  34. {
  35. group.GET("/whitelist", authSvr.Guest, whiteList)
  36. group.GET("/report", report)
  37. group.GET("/share/report", reportShare)
  38. group.GET("", authSvr.Guest, list)
  39. group.GET("/info", authSvr.Guest, info)
  40. group.POST("/add", authSvr.User, add)
  41. group.POST("/del", authSvr.User, del)
  42. group.POST("/update", authSvr.User, update)
  43. videoGroup := group.Group("/video")
  44. {
  45. videoGroup.GET("", videoList)
  46. videoGroup.POST("/check", authSvr.User, check)
  47. videoGroup.GET("/toview", authSvr.Guest, toView)
  48. videoGroup.POST("/add", authSvr.User, addVideo)
  49. videoGroup.POST("/del", authSvr.User, delVideo)
  50. videoGroup.POST("/sort", authSvr.User, sortVideo)
  51. videoGroup.POST("/desc/edit", authSvr.User, editVideoDesc)
  52. videoGroup.GET("/search", authSvr.User, searchVideo)
  53. }
  54. favGroup := group.Group("/fav")
  55. {
  56. favGroup.GET("", authSvr.Guest, listFavorite)
  57. favGroup.POST("/add", authSvr.User, addFavorite)
  58. favGroup.POST("/del", authSvr.User, delFavorite)
  59. }
  60. }
  61. }
  62. func internalRouter(e *bm.Engine) {
  63. group := e.Group("/x/internal/playlist")
  64. {
  65. group.GET("/whitelist", vfySvr.Verify, whiteList)
  66. group.GET("/report", vfySvr.Verify, report)
  67. group.GET("/share/report", vfySvr.Verify, reportShare)
  68. group.GET("", vfySvr.Verify, list)
  69. group.GET("/info", vfySvr.Verify, info)
  70. videoGroup := group.Group("/video")
  71. {
  72. videoGroup.GET("", vfySvr.Verify, videoList)
  73. videoGroup.GET("/toview", vfySvr.Verify, toView)
  74. videoGroup.GET("/search", vfySvr.Verify, searchVideo)
  75. }
  76. favGroup := group.Group("/fav")
  77. {
  78. favGroup.GET("", vfySvr.Verify, listFavorite)
  79. }
  80. }
  81. }
  82. func ping(c *bm.Context) {
  83. if err := plSvc.Ping(c); err != nil {
  84. log.Error("playlist interface ping error")
  85. c.AbortWithStatus(http.StatusServiceUnavailable)
  86. }
  87. }