http.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package http
  2. import (
  3. "net/http"
  4. "go-common/library/net/http/blademaster/middleware/auth"
  5. "go-common/app/interface/live/app-blink/api/http/v1"
  6. "go-common/app/interface/live/app-blink/conf"
  7. "go-common/app/interface/live/app-blink/service"
  8. spSrv "go-common/app/interface/live/app-blink/service/v1"
  9. resRpc "go-common/app/service/live/resource/api/grpc/v1"
  10. "go-common/library/log"
  11. bm "go-common/library/net/http/blademaster"
  12. "go-common/library/net/http/blademaster/middleware/verify"
  13. )
  14. var (
  15. srv *service.Service
  16. vfy *verify.Verify
  17. slSrv *spSrv.SplashService
  18. bnSrv *spSrv.BannerService
  19. roomSrv *spSrv.RoomService
  20. midAuth *auth.Auth
  21. topicSrv *spSrv.TopicService
  22. )
  23. // Init init
  24. func Init(c *conf.Config, svc *service.Service) {
  25. srv = svc
  26. vfy = verify.New(c.Verify)
  27. initService(c)
  28. engine := bm.DefaultServer(c.BM)
  29. route(engine)
  30. if err := engine.Start(); err != nil {
  31. log.Error("bm Start error(%v)", err)
  32. panic(err)
  33. }
  34. }
  35. func initService(c *conf.Config) {
  36. slSrv = spSrv.NewSplashService(c)
  37. bnSrv = spSrv.NewBannerService(c)
  38. roomSrv = spSrv.NewRoomService(c)
  39. midAuth = auth.New(c.Auth)
  40. topicSrv = spSrv.NewTopicService(c)
  41. }
  42. func getInfo(ctx *bm.Context) {
  43. p := new(resRpc.GetInfoReq)
  44. if err := ctx.Bind(p); err != nil {
  45. return
  46. }
  47. resp, err := slSrv.GetInfo(ctx, p)
  48. ctx.JSON(resp, err)
  49. }
  50. func getBlinkBanner(ctx *bm.Context) {
  51. p := new(resRpc.GetInfoReq)
  52. if err := ctx.Bind(p); err != nil {
  53. return
  54. }
  55. resp, err := bnSrv.GetBlinkBanner(ctx, p)
  56. ctx.JSON(resp, err)
  57. }
  58. func route(e *bm.Engine) {
  59. e.Ping(ping)
  60. e.Register(register)
  61. g := e.Group("/xlive/app-blink/v1")
  62. {
  63. g.GET("/start", vfy.Verify, howToStart)
  64. }
  65. g.GET("/splash/getInfo", getInfo)
  66. g.GET("/banner/getBlinkBanner", getBlinkBanner)
  67. v1.RegisterV1RoomService(e, roomSrv, map[string]bm.HandlerFunc{"auth": midAuth.User})
  68. v1.RegisterV1TopicService(e, topicSrv, map[string]bm.HandlerFunc{"auth": midAuth.User})
  69. }
  70. func ping(c *bm.Context) {
  71. if err := srv.Ping(c); err != nil {
  72. log.Error("ping error(%v)", err)
  73. c.AbortWithStatus(http.StatusServiceUnavailable)
  74. }
  75. }
  76. func register(c *bm.Context) {
  77. c.JSON(map[string]interface{}{}, nil)
  78. }
  79. // example for http request handler
  80. func howToStart(c *bm.Context) {
  81. c.String(0, "Golang 大法好 !!!")
  82. }