http.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package http
  2. import (
  3. "go-common/library/net/metadata"
  4. "net/http"
  5. "go-common/app/service/live/userexp/conf"
  6. "go-common/app/service/live/userexp/service"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. )
  10. var (
  11. expSvr *service.Service
  12. )
  13. // Init init account service.
  14. func Init(c *conf.Config, s *service.Service) {
  15. // init service
  16. expSvr = s
  17. // init external router
  18. engineOut := bm.DefaultServer(c.BM.Inner)
  19. innerRouter(engineOut)
  20. // init Inner serve
  21. if err := engineOut.Start(); err != nil {
  22. log.Error("bm.DefaultServer error(%v)", err)
  23. panic(err)
  24. }
  25. engineLocal := bm.DefaultServer(c.BM.Local)
  26. localRouter(engineLocal)
  27. // init Local serve
  28. if err := engineLocal.Start(); err != nil {
  29. log.Error("bm.DefaultServer error(%v)", err)
  30. panic(err)
  31. }
  32. }
  33. // outerRouter init outer router api path.
  34. func innerRouter(e *bm.Engine) {
  35. // init api
  36. e.GET("/monitor/ping", ping)
  37. group := e.Group("/x/internal/liveexp")
  38. {
  39. group.GET("/level/get", level)
  40. group.GET("/level/mulGet", multiGetLevel)
  41. group.GET("/level/addUexp", addUexp)
  42. group.GET("/level/addRexp", addRexp)
  43. }
  44. }
  45. // localRouter init local router.
  46. func localRouter(e *bm.Engine) {
  47. }
  48. // ping check server ok.
  49. func ping(c *bm.Context) {
  50. if err := expSvr.Ping(c); err != nil {
  51. log.Error("live-userexp http service ping error(%v)", err)
  52. c.AbortWithStatus(http.StatusServiceUnavailable)
  53. }
  54. }
  55. func infocArg(c *bm.Context) (arg map[string]string) {
  56. var (
  57. ua string
  58. referer string
  59. sid string
  60. req = c.Request
  61. )
  62. ua = req.Header.Get(service.RelInfocUA)
  63. referer = req.Header.Get(service.RelInfocReferer)
  64. sidCookie, err := req.Cookie(service.RelInfocSid)
  65. if err != nil {
  66. //log.Warn("live-userexo infoc get sid failed error(%v)", err)
  67. } else {
  68. sid = sidCookie.Value
  69. }
  70. buvid := req.Header.Get(service.RelInfocHeaderBuvid)
  71. if buvid == "" {
  72. buvidCookie, _ := req.Cookie(service.RelInfocCookieBuvid)
  73. if buvidCookie != nil {
  74. buvid = buvidCookie.Value
  75. }
  76. }
  77. arg = map[string]string{
  78. service.RelInfocIP: metadata.String(c, metadata.RemoteIP),
  79. service.RelInfocReferer: referer,
  80. service.RelInfocSid: sid,
  81. service.RelInfocBuvid: buvid,
  82. service.RelInfocUA: ua,
  83. }
  84. return
  85. }
  86. //func checkHealth(c wctx.Context) {
  87. // _, err := os.Stat(conf.Conf.CheckFile)
  88. // if os.IsNotExist(err) {
  89. // http.Error(c.Response(), http.StatusText(http.StatusServiceUnavailable), http.StatusServiceUnavailable)
  90. // }
  91. // c.Cancel()
  92. //}