http.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package http
  2. import (
  3. "net/http"
  4. "go-common/app/service/main/passport-game/conf"
  5. "go-common/app/service/main/passport-game/service"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. )
  9. var (
  10. srv *service.Service
  11. )
  12. // Init init http sever instance.
  13. func Init(c *conf.Config, s *service.Service) {
  14. srv = s
  15. // init inner router
  16. engine := bm.DefaultServer(c.BM)
  17. initRouter(engine)
  18. // init inner server
  19. if err := engine.Start(); err != nil {
  20. log.Error("engine.Start error(%v)", err)
  21. panic(err)
  22. }
  23. }
  24. // initRouter init inner router.
  25. func initRouter(e *bm.Engine) {
  26. // health check
  27. e.Ping(ping)
  28. e.Register(register)
  29. // new defined api lists
  30. g := e.Group("/x/passport-game")
  31. {
  32. g.GET("/oauth", verify, oauth)
  33. g.GET("/myinfo", verify, myInfo)
  34. g.GET("/info", verify, info)
  35. g.GET("/key", verify, getKeyProxy)
  36. g.GET("/login", verify, loginProxy)
  37. g.GET("/renewtoken", verify, renewToken)
  38. g.GET("/regions", verify, regions)
  39. g.POST("/reg/v3", verify, regV3)
  40. g.POST("/reg/v2", verify, regV2)
  41. g.POST("/reg", verify, reg)
  42. g.POST("/reg/byTel", verify, byTel)
  43. g.GET("/captcha", verify, captcha)
  44. g.POST("/sendSms", verify, sendSms)
  45. }
  46. inner := e.Group("/cache/pb")
  47. {
  48. inner.GET("/token", tokenPBCache)
  49. inner.GET("/info", infoPBCache)
  50. }
  51. }
  52. func verify(c *bm.Context) {
  53. app, err := verifySign(c, srv)
  54. if err != nil {
  55. c.JSON(nil, err)
  56. c.Abort()
  57. }
  58. c.Set("app", app)
  59. }
  60. // ping check server ok.
  61. func ping(c *bm.Context) {
  62. if err := srv.Ping(c); err != nil {
  63. log.Error("ping error(%v)", err)
  64. c.AbortWithStatus(http.StatusServiceUnavailable)
  65. }
  66. }
  67. // register support discovery.
  68. func register(c *bm.Context) {
  69. c.JSON(map[string]struct{}{}, nil)
  70. }