http.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package http
  2. import (
  3. "go-common/app/service/live/resource/api/http/v1"
  4. "go-common/app/service/live/resource/conf"
  5. "go-common/app/service/live/resource/service"
  6. v12 "go-common/app/service/live/resource/service/v1"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. "go-common/library/net/http/blademaster/middleware/verify"
  10. "net/http"
  11. )
  12. var (
  13. srv *service.Service
  14. vfy *verify.Verify
  15. titansService *v12.TitansService
  16. )
  17. // Init init
  18. func Init(c *conf.Config, srv *service.Service) {
  19. vfy = verify.New(c.Verify)
  20. initService(c)
  21. engine := bm.DefaultServer(c.BM)
  22. route(engine)
  23. if err := engine.Start(); err != nil {
  24. log.Error("bm Start error(%v)", err)
  25. panic(err)
  26. }
  27. }
  28. func initService(c *conf.Config) {
  29. srv = service.New(c)
  30. titansService = v12.NewTitansService(c)
  31. }
  32. func route(e *bm.Engine) {
  33. e.Ping(ping)
  34. e.Register(register)
  35. e.GET("/xlive/internal/resource/v1/titans/getMyTreeApps", getNodes)
  36. g := e.Group("/xlive/resource")
  37. {
  38. g.GET("/start", vfy.Verify, howToStart)
  39. }
  40. v1.RegisterV1TitansService(e, titansService, map[string]bm.HandlerFunc{})
  41. }
  42. func ping(c *bm.Context) {
  43. if err := srv.Ping(c); err != nil {
  44. log.Error("ping error(%v)", err)
  45. c.AbortWithStatus(http.StatusServiceUnavailable)
  46. }
  47. }
  48. func register(c *bm.Context) {
  49. c.JSON(map[string]interface{}{}, nil)
  50. }
  51. // example for http request handler
  52. func howToStart(c *bm.Context) {
  53. c.String(0, "Golang 大法好 !!!")
  54. }