http.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package http
  2. import (
  3. "io"
  4. "strconv"
  5. "strings"
  6. "go-common/app/infra/config/conf"
  7. "go-common/app/infra/config/service/v1"
  8. "go-common/app/infra/config/service/v2"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. bm "go-common/library/net/http/blademaster"
  12. "go-common/library/net/http/blademaster/middleware/antispam"
  13. v "go-common/library/net/http/blademaster/middleware/verify"
  14. "github.com/dgryski/go-farm"
  15. )
  16. var (
  17. cnf *conf.Config
  18. verify *v.Verify
  19. confSvc *v1.Service
  20. confSvc2 *v2.Service
  21. anti *antispam.Antispam
  22. )
  23. // Init init.
  24. func Init(c *conf.Config, s *v1.Service, s2 *v2.Service, rpcCloser io.Closer) {
  25. initService(c)
  26. verify = v.New(c.Verify)
  27. cnf = c
  28. confSvc = s
  29. confSvc2 = s2
  30. engine := bm.DefaultServer(c.BM)
  31. innerRouter(engine)
  32. if err := engine.Start(); err != nil {
  33. log.Error("engine.Start() error(%v)", err)
  34. panic(err)
  35. }
  36. }
  37. // innerRouter init inner router.
  38. func innerRouter(e *bm.Engine) {
  39. e.Ping(ping)
  40. e.Register(register)
  41. b := e.Group("/", verify.Verify)
  42. noAuth := e.Group("/")
  43. {
  44. v1 := b.Group("v1/config/")
  45. {
  46. v1.GET("host/infos", hosts)
  47. v1.POST("host/clear", clearhost)
  48. v1.POST("push", push)
  49. }
  50. {
  51. noAuth.GET("v1/config/versions", versions)
  52. noAuth.GET("v1/config/builds", builds)
  53. noAuth.GET("v1/config/check", check)
  54. noAuth.GET("v1/config/get", config)
  55. noAuth.GET("v1/config/get2", configN)
  56. noAuth.GET("v1/config/file.so", file)
  57. noAuth.GET("v1/config/version/ing", versionIng)
  58. noAuth.POST("v1/config/config/add", addConfigs)
  59. noAuth.POST("v1/config/config/copy", copyConfigs)
  60. noAuth.POST("v1/config/config/update", updateConfigs)
  61. noAuth.GET("config/v2/versions", versions2)
  62. noAuth.GET("config/v2/builds", builds2)
  63. noAuth.GET("config/v2/check", check2)
  64. noAuth.GET("config/v2/get", setMid, anti.ServeHTTP, config2)
  65. noAuth.GET("config/v2/file.so", file2)
  66. noAuth.GET("config/v2/latest", latest)
  67. }
  68. v2 := b.Group("config/v2/")
  69. {
  70. v2.POST("host/clear", clearhost2)
  71. }
  72. }
  73. }
  74. func setMid(c *bm.Context) {
  75. var (
  76. token string
  77. service string
  78. query = c.Request.URL.Query()
  79. hash uint64
  80. )
  81. service = query.Get("service")
  82. if service == "" {
  83. token = query.Get("token")
  84. if token == "" {
  85. c.JSON(nil, ecode.RequestErr)
  86. c.Abort()
  87. return
  88. }
  89. hash = farm.Hash64([]byte(token))
  90. } else {
  91. arrs := strings.Split(service, "_")
  92. if len(arrs) != 3 {
  93. c.JSON(nil, ecode.RequestErr)
  94. c.Abort()
  95. return
  96. }
  97. _, err := strconv.ParseInt(arrs[0], 10, 64)
  98. if err != nil {
  99. c.JSON(nil, ecode.RequestErr)
  100. c.Abort()
  101. return
  102. }
  103. hash = farm.Hash64([]byte(service))
  104. }
  105. c.Set("mid", int64(hash))
  106. }
  107. func initService(c *conf.Config) {
  108. anti = antispam.New(c.Antispam)
  109. }