http.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Package http is where http server init,
  2. // including routes
  3. package http
  4. import (
  5. "net/http"
  6. pb "go-common/app/interface/live/live-demo/api/http"
  7. v2pb "go-common/app/interface/live/live-demo/api/http/v2"
  8. "go-common/app/interface/live/live-demo/conf"
  9. "go-common/app/interface/live/live-demo/dao"
  10. svc "go-common/app/interface/live/live-demo/service"
  11. "go-common/app/interface/live/live-demo/service/v2"
  12. "go-common/library/log"
  13. bm "go-common/library/net/http/blademaster"
  14. "go-common/library/net/http/blademaster/middleware/auth"
  15. "go-common/library/net/http/blademaster/middleware/verify"
  16. )
  17. var (
  18. vfy *verify.Verify
  19. midAuth *auth.Auth
  20. )
  21. // Init init
  22. func Init(c *conf.Config) {
  23. dao.InitAPI()
  24. initMiddleware(c)
  25. engine := bm.DefaultServer(nil)
  26. route(engine)
  27. if err := engine.Start(); err != nil {
  28. log.Error("bm Start error(%v)", err)
  29. panic(err)
  30. }
  31. }
  32. func initMiddleware(c *conf.Config) {
  33. vfy = verify.New(c.Verify)
  34. midAuth = auth.New(nil)
  35. }
  36. func route(e *bm.Engine) {
  37. e.Ping(ping)
  38. e.Register(register)
  39. g := e.Group("/x/live-demo")
  40. {
  41. g.GET("/start", vfy.Verify, howToStart)
  42. }
  43. midMap := map[string]bm.HandlerFunc{
  44. "auth": midAuth.User,
  45. "guest": midAuth.Guest,
  46. "verify": vfy.Verify}
  47. v2pb.RegisterV2FooService(e, v2.NewFooService(conf.Conf), midMap)
  48. v2pb.RegisterV2Foo2Service(e, v2.NewFoo2Service(conf.Conf), midMap)
  49. pb.RegisterFooService(e, svc.NewFooService(conf.Conf), midMap)
  50. pb.RegisterFoo2Service(e, svc.NewFoo2Service(conf.Conf), midMap)
  51. e.Inject(pb.PathFooGetInfo, midAuth.User)
  52. pb.RegisterFooBMServer(e, svc.NewFooService(conf.Conf))
  53. }
  54. func ping(c *bm.Context) {
  55. c.AbortWithStatus(http.StatusOK)
  56. }
  57. func register(c *bm.Context) {
  58. c.JSON(map[string]interface{}{}, nil)
  59. }
  60. // example for http request handler
  61. func howToStart(c *bm.Context) {
  62. c.String(0, "Golang 大法好 !!!")
  63. }