http.go 1.1 KB

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