http.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package http
  2. import (
  3. "net/http"
  4. "go-common/app/admin/main/config/conf"
  5. "go-common/app/admin/main/config/service"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. "go-common/library/net/http/blademaster/middleware/permit"
  9. )
  10. var (
  11. svr *service.Service
  12. authSrv *permit.Permit
  13. )
  14. // Init init config.
  15. func Init(c *conf.Config, s *service.Service) {
  16. svr = s
  17. authSrv = permit.New(c.Auth)
  18. engine := bm.DefaultServer(c.BM)
  19. innerRouter(engine)
  20. if err := engine.Start(); err != nil {
  21. log.Error("engine.Start error(%v)", err)
  22. panic(err)
  23. }
  24. }
  25. // innerRouter init inner router.
  26. func innerRouter(e *bm.Engine) {
  27. e.Ping(ping)
  28. b := e.Group("x/admin/config", authSrv.Verify())
  29. d := e.Group("x/admin/config")
  30. {
  31. notAuth := d.Group("/")
  32. {
  33. notAuth.POST("home/config/update", configUpdate)
  34. notAuth.POST("home/tag/update", tagUpdate)
  35. notAuth.POST("canal/tag/update", canalTagUpdate)
  36. notAuth.POST("canal/name/configs", canalNameConfigs)
  37. notAuth.POST("canal/config/create", canalConfigCreate)
  38. notAuth.POST("caster/envs", casterEnvs)
  39. notAuth.GET("get/apps", getApps)
  40. }
  41. service := b.Group("/service")
  42. {
  43. service.POST("/token/set", setToken)
  44. service.GET("/host/infos", hosts)
  45. service.POST("/delete", clearhost)
  46. }
  47. app := b.Group("/app")
  48. {
  49. app.POST("/token/update", updateToken)
  50. app.POST("/create", create)
  51. app.GET("/apps", appList)
  52. app.GET("/envs", envs)
  53. app.GET("/nodeTree", nodeTree)
  54. app.POST("/zone/copy", zoneCopy)
  55. app.POST("/rename", rename)
  56. app.POST("/status", upAppStatus)
  57. }
  58. bu := b.Group("/build")
  59. {
  60. bu.POST("/create", createBuild)
  61. bu.POST("/tag/update", updateTag)
  62. bu.POST("/tagid/update", updateTagID)
  63. bu.GET("/builds", builds)
  64. bu.GET("/build", build)
  65. bu.POST("/delete", buildDel)
  66. bu.POST("/hosts/force", hostsForce)
  67. bu.POST("/clear/force", clearForce)
  68. }
  69. config := b.Group("/config")
  70. {
  71. config.POST("/create", createConfig)
  72. config.POST("/lint", lintConfig)
  73. config.POST("/value/update", updateConfValue)
  74. config.GET("/app/configs", configsByAppName)
  75. config.GET("/build/configs", configsByBuildID)
  76. config.GET("/tag/configs", configsByTagID)
  77. config.GET("/name/configs", configsByName)
  78. config.GET("/names", namesByAppName)
  79. config.GET("/configs", configs)
  80. config.GET("/all/search", configSearchAll)
  81. config.GET("/app/search", configSearchApp)
  82. config.GET("/refs", configRefs)
  83. config.GET("/value", value)
  84. config.GET("/diff", diff)
  85. config.POST("/delete", configDel)
  86. config.GET("/build/infos", configBuildInfos)
  87. }
  88. common := b.Group("/common")
  89. {
  90. common.POST("/create", createComConfig)
  91. common.POST("/value/update", updateComConfValue)
  92. common.GET("/name/configs", comConfigsByName)
  93. common.GET("/configs", configsByTeam)
  94. common.GET("/names", namesByTeam)
  95. common.GET("/value", comValue)
  96. common.GET("/envs", envsByTeam)
  97. common.GET("/app", appByTeam)
  98. common.GET("/tag/push", tagPush)
  99. }
  100. tags := b.Group("/tag")
  101. {
  102. tags.POST("/create", createTag)
  103. tags.GET("/last/tags", lastTags)
  104. tags.GET("/build/tags", tagsByBuild)
  105. tags.GET("/tag", tag)
  106. tags.GET("/config/diff", tagConfigDiff)
  107. }
  108. apm := b.Group("/apm")
  109. {
  110. apm.GET("/copy", apmCopy)
  111. }
  112. tree := b.Group("/tree")
  113. {
  114. tree.GET("/update", syncTree)
  115. }
  116. }
  117. }
  118. // ping check server ok.
  119. func ping(ctx *bm.Context) {
  120. if err := svr.Ping(); err != nil {
  121. log.Error("service ping error(%v)", err)
  122. ctx.AbortWithStatus(http.StatusServiceUnavailable)
  123. }
  124. }
  125. func user(c *bm.Context) (username string) {
  126. usernameI, _ := c.Get("username")
  127. username, _ = usernameI.(string)
  128. return
  129. }