http.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package http
  2. import (
  3. "net/http"
  4. v1pb "go-common/app/service/live/zeus/api/v1"
  5. "go-common/app/service/live/zeus/internal/conf"
  6. "go-common/app/service/live/zeus/internal/service"
  7. v1srv "go-common/app/service/live/zeus/internal/service/v1"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/http/blademaster/middleware/verify"
  11. )
  12. var (
  13. vfy *verify.Verify
  14. svc *service.Service
  15. )
  16. // Init init
  17. func Init(c *conf.Config, s *service.Service, zeus *v1srv.ZeusService) {
  18. svc = s
  19. vfy = verify.New(c.Verify)
  20. engine := bm.DefaultServer(c.BM)
  21. route(engine, zeus)
  22. if err := engine.Start(); err != nil {
  23. log.Error("bm Start error(%v)", err)
  24. panic(err)
  25. }
  26. }
  27. func route(e *bm.Engine, zeus *v1srv.ZeusService) {
  28. e.Ping(ping)
  29. e.Register(register)
  30. g := e.Group("/x/zeus")
  31. {
  32. g.GET("/start", vfy.Verify, howToStart)
  33. }
  34. v1pb.RegisterZeusBMServer(e, zeus)
  35. }
  36. func ping(ctx *bm.Context) {
  37. if err := svc.Ping(ctx); err != nil {
  38. log.Error("ping error(%v)", err)
  39. ctx.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. }