http.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package http
  2. import (
  3. "go-common/app/admin/live/live-admin/dao"
  4. "go-common/library/net/http/blademaster/middleware/auth"
  5. "net/http"
  6. v1API "go-common/app/admin/live/live-admin/api/http/v1"
  7. v2API "go-common/app/admin/live/live-admin/api/http/v2"
  8. "go-common/app/admin/live/live-admin/conf"
  9. "go-common/app/admin/live/live-admin/service"
  10. v1Service "go-common/app/admin/live/live-admin/service/v1"
  11. v2Service "go-common/app/admin/live/live-admin/service/v2"
  12. "go-common/library/log"
  13. bm "go-common/library/net/http/blademaster"
  14. "go-common/library/net/http/blademaster/middleware/verify"
  15. )
  16. var (
  17. vfy *verify.Verify
  18. svc *service.Service
  19. midAuth *auth.Auth
  20. d *dao.Dao
  21. )
  22. // Init init
  23. func Init(c *conf.Config, s *service.Service) {
  24. svc = s
  25. dao.InitAPI()
  26. d = dao.New(c)
  27. vfy = verify.New(c.Verify)
  28. midAuth = auth.New(c.Auth)
  29. engine := bm.DefaultServer(c.BM)
  30. route(engine)
  31. if err := engine.Start(); err != nil {
  32. log.Error("bm Start error(%v)", err)
  33. panic(err)
  34. }
  35. }
  36. func route(e *bm.Engine) {
  37. e.Ping(ping)
  38. e.Register(register)
  39. g := e.Group("/x/live-admin")
  40. {
  41. g.GET("/start", vfy.Verify, howToStart)
  42. }
  43. midMap := map[string]bm.HandlerFunc{
  44. "guest": midAuth.Guest,
  45. "cors": bm.CORS(),
  46. }
  47. v1API.RegisterV1ResourceService(e, v1Service.NewResourceService(conf.Conf), midMap)
  48. v1API.RegisterV1CapsuleService(e, v1Service.NewCapsuleService(conf.Conf), midMap)
  49. v1API.RegisterV1GaeaService(e, v1Service.NewGaeaService(conf.Conf), midMap)
  50. v1API.RegisterV1RoomMngService(e, v1Service.NewRoomMngService(conf.Conf), midMap)
  51. v2API.RegisterV2UserResourceService(e, v2Service.NewUserResourceService(conf.Conf), midMap)
  52. v1API.RegisterV1PayGoodsService(e, v1Service.NewPayGoodsService(conf.Conf), midMap)
  53. v1API.RegisterV1PayLiveService(e, v1Service.NewPayLiveService(conf.Conf), midMap)
  54. v1API.RegisterV1TokenService(e, v1Service.NewTokenService(conf.Conf, d), midMap)
  55. v1API.RegisterV1UploadService(e, v1Service.NewUploadService(conf.Conf, d), midMap)
  56. }
  57. func ping(ctx *bm.Context) {
  58. if err := svc.Ping(ctx); err != nil {
  59. log.Error("ping error(%v)", err)
  60. ctx.AbortWithStatus(http.StatusServiceUnavailable)
  61. }
  62. }
  63. func register(c *bm.Context) {
  64. c.JSON(map[string]interface{}{}, nil)
  65. }
  66. // example for http request handler
  67. func howToStart(c *bm.Context) {
  68. c.String(0, "Golang 大法好 !!!")
  69. }