http.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. package http
  2. import (
  3. "net/http"
  4. "go-common/app/admin/main/apm/conf"
  5. per "go-common/app/admin/main/apm/model/user"
  6. "go-common/app/admin/main/apm/service"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. mpermit "go-common/library/net/http/blademaster/middleware/permit"
  11. )
  12. var (
  13. apmSvc *service.Service
  14. authSrv *mpermit.Permit
  15. )
  16. // Init init http sever instance.
  17. func Init(c *conf.Config, s *service.Service) {
  18. apmSvc = s
  19. authSrv = mpermit.New(c.Auth)
  20. engine := bm.DefaultServer(c.BM)
  21. innerRouter(engine)
  22. if err := engine.Start(); err != nil {
  23. log.Error("engine.Start error(%v)", err)
  24. panic(err)
  25. }
  26. }
  27. // innerRouter init inner router.
  28. func innerRouter(e *bm.Engine) {
  29. e.Ping(ping)
  30. b := e.Group("x/admin/apm", authSrv.Verify())
  31. {
  32. code := b.Group("/ecode")
  33. {
  34. code.POST("/add", permit(per.EcodeEdit), ecodeAdd)
  35. code.POST("/edit", permit(per.EcodeEdit), ecodeEdit)
  36. code.POST("/delete", permit(per.EcodeEdit), ecodeDelete)
  37. code.POST("/sync/ecodes", permit(per.EcodeEdit), syncEcodes)
  38. code.POST("/langs/add", permit(per.EcodeEdit), ecodeLangsAdd)
  39. code.POST("/langs/edit", permit(per.EcodeEdit), ecodeLangsEdit)
  40. code.POST("/langs/delete", permit(per.EcodeEdit), ecodeLangsDelete)
  41. code.POST("/langs/save", permit(per.EcodeEdit), ecodeLangsSave)
  42. }
  43. user := b.Group("/user")
  44. {
  45. user.GET("/auth", userAuth)
  46. user.GET("/rule/states", userRuleStates)
  47. user.POST("/apply", userApply)
  48. user.POST("/apply/edit", permit(per.UserEdit), userApplyEdit)
  49. user.GET("/users", userList)
  50. user.GET("/info", permit(per.UserView), userInfo)
  51. user.POST("/edit", permit(per.UserEdit), userEdit)
  52. user.GET("/modules", permit(per.UserEdit), userModules)
  53. user.GET("/rules", permit(per.UserEdit), userRules)
  54. user.POST("/module/edit", permit(per.UserEdit), userModulesEdit)
  55. user.POST("/rule/edit", permit(per.UserEdit), userRulesEdit)
  56. user.GET("/applies", userApplies)
  57. user.POST("/audit", userAudit)
  58. // sync
  59. user.GET("/tree/sync", userSyncTree)
  60. user.GET("/tree/appids", userTreeAppids)
  61. user.GET("/tree/discovery", userTreeDiscovery)
  62. }
  63. databus := b.Group("/databus")
  64. {
  65. // project
  66. databus.GET("/projects", permit(per.DatabusKeyView), databusProjects)
  67. // app
  68. databus.GET("/apps", permit(per.DatabusKeyView), databusApps)
  69. databus.POST("/app/add", permit(per.DatabusKeyEdit), databusAppAdd)
  70. databus.POST("/app/edit", permit(per.DatabusKeyEdit), databusAppEdit)
  71. // group
  72. databus.GET("/groups", permit(per.DatabusGroupView), databusGroups)
  73. databus.GET("/group/projects", permit(per.DatabusGroupView), databusGroupProjects)
  74. databus.GET("/group/consumer/addrs", permit(per.DatabusGroupView), databusConsumerAddrs)
  75. databus.POST("/group/sub/add", permit(per.DatabusGroupEdit), databusGroupSubAdd)
  76. databus.POST("/group/pub/add", permit(per.DatabusGroupEdit), databusGroupPubAdd)
  77. databus.POST("/group/delete", permit(per.DatabusGroupEdit), databusGroupDelete)
  78. databus.POST("/group/rename", permit(per.DatabusGroupEdit), databusGroupRename)
  79. databus.GET("/group/offset", permit(per.DatabusGroupView), databusGroupOffset)
  80. databus.POST("/group/marked", permit(per.DatabusGroupEdit), databusGroupMarked)
  81. databus.POST("/group/begin", permit(per.DatabusGroupEdit), databusGroupBegin)
  82. databus.POST("/group/new/offset", permit(per.DatabusGroupEdit), databusGroupNewOffset)
  83. databus.POST("/group/time", permit(per.DatabusGroupEdit), databusGroupTime)
  84. // topic
  85. databus.GET("/topics", permit(per.DatabusTopicView), databusTopics)
  86. databus.GET("/topic/names", permit(per.DatabusGroupView), databusTopicNames)
  87. databus.POST("/topic/add", permit(per.DatabusTopicEdit), databusTopicAdd)
  88. databus.POST("/topic/edit", permit(per.DatabusTopicEdit), databusTopicEdit)
  89. databus.GET("/topic/all", permit(per.DatabusGroupView), databusTopicAll)
  90. // alarm
  91. databus.POST("/alarm/edit", permit(per.DatabusGroupView), databusAlarmEdit)
  92. databus.POST("/alarm/init", permit(per.DatabusGroupView), databusAlarmInit)
  93. databus.POST("/alarm/all/edit", permit(per.DatabusGroupView), databusAlarmAllEdit)
  94. // apply
  95. databus.GET("/apply/list", permit(per.DatabusGroupView), databusApplyList)
  96. databus.POST("/apply/pub/add", permit(per.DatabusGroupView), databusApplyPubAdd)
  97. databus.POST("/apply/sub/add", permit(per.DatabusGroupView), databusApplySubAdd)
  98. databus.POST("/apply/edit", permit(per.DatabusGroupView), databusApplyEdit)
  99. databus.POST("/apply/approval/process", permit(per.DatabusGroupApply), databusApplyApprovalProcess)
  100. // notify
  101. databus.GET("/notify/apply/list", permit(per.DatabusGroupView), databusNotifyList)
  102. databus.POST("/notify/edit", permit(per.DatabusNotifyEdit), databusNotifyEdit)
  103. databus.POST("/notify/apply/add", permit(per.DatabusGroupView), databusNotifyApplyAdd)
  104. // databus.POST("/notify/filter/add", databusNotifyFilterAdd)
  105. // databus.POST("/notify/filter/edit", databusNotifyFilterEdit)
  106. // message
  107. databus.GET("/message/fetch", permit(per.DatabusGroupView), databusMsgFetch)
  108. }
  109. canal := b.Group("/canal")
  110. {
  111. canal.GET("", permit(per.CanalView), canalList)
  112. canal.POST("/add", permit(per.CanalEdit), canalAdd)
  113. canal.POST("/edit", permit(per.CanalEdit), canalEdit)
  114. canal.POST("/delete", permit(per.CanalEdit), canalDelete)
  115. canal.GET("/scan", permit(per.CanalView), canalScanByAddrFromConfig)
  116. e.POST("x/admin/apm/canal/apply/config", canalApplyDetailToConfig)
  117. canal.GET("/addrs", permit(per.CanalView), canalAddrAll)
  118. canal.POST("/apply/edit", permit(per.CanalView), canalApplyConfigEdit)
  119. canal.GET("/apply", permit(per.CanalView), canalApplyList)
  120. canal.POST("/apply/approval/process", permit(per.CanalEdit), canalApplyApprovalProcess)
  121. }
  122. need := b.Group("/need")
  123. {
  124. need.GET("/list", needList)
  125. need.POST("/add", needAdd)
  126. need.POST("/edit", needEdit)
  127. need.POST("/verify", permit(per.NeedVerify), needVerify)
  128. need.POST("/thumbsup", needThumbsUp)
  129. need.GET("/vote/list", permit(per.NeedVerify), needVoteList)
  130. }
  131. discovery := b.Group("/discovery")
  132. {
  133. discovery.GET("/", discoveryProxy)
  134. }
  135. app := b.Group("/app")
  136. {
  137. app.GET("/list", permit(per.AppView), appList)
  138. app.POST("/add", permit(per.AppEdit), appAdd)
  139. app.POST("/edit", permit(per.AppEdit), appEdit)
  140. app.POST("/delete", permit(per.AppEdit), appDelete)
  141. app.GET("/auth/list", permit(per.AppAuthView), appAuthList)
  142. app.POST("/auth/add", permit(per.AppEdit), appAuthAdd)
  143. app.POST("/auth/edit", permit(per.AppEdit), appAuthEdit)
  144. app.POST("/auth/delete", permit(per.AppEdit), appAuthDelete)
  145. app.GET("/caller/search", permit(per.AppEdit), appCallerSearch)
  146. //tree
  147. app.GET("/tree", permit(per.AppEdit), appTree)
  148. }
  149. platform := b.Group("/platform")
  150. {
  151. platform.GET("/search/get/", permit(per.PlatformSearchView), searchProxyGet)
  152. platform.POST("/search/post/", permit(per.PlatformSearchView), searchProxyPost)
  153. platform.GET("/reply/get/", permit(per.PlatformReplyView), replyProxyGet)
  154. platform.POST("/reply/post/", permit(per.PlatformReplyView), replyProxyPost)
  155. platform.GET("/tag/get/", permit(per.PlatformTagView), tagProxyGet)
  156. platform.POST("/tag/post/", permit(per.PlatformTagView), tagProxyPost)
  157. platform.GET("/bfs/get/", permit(per.BFSView), bfsProxyGet)
  158. platform.POST("/bfs/post/", permit(per.BFSEdit), bfsProxyPost)
  159. platform.GET("/reply/feed/get/", permit(per.PlatformReplyView), replyFeedProxyGet)
  160. platform.POST("/reply/feed/post/", permit(per.PlatformReplyView), replyFeedProxyPost)
  161. }
  162. p := b.Group("/pprof")
  163. {
  164. p.GET("/profile", permit(per.PerformanceManager), buildSvg)
  165. p.GET("/svg", permit(per.PerformanceManager), readSvg)
  166. p.GET("/heap", permit(per.PerformanceManager), heap)
  167. p.GET("/flame", permit(per.PerformanceManager), flame)
  168. p.GET("/", permit(per.PerformanceManager), pprof)
  169. }
  170. ut := b.Group("/ut")
  171. {
  172. ut.GET("/merge/list", utList)
  173. ut.GET("/detail/list", utDetail)
  174. ut.GET("/history/commit", utHistoryCommit)
  175. ut.GET("/rank/list", utRank)
  176. ut.GET("/rank/user", userRank)
  177. ut.GET("/quality/trend", utQATrend)
  178. ut.GET("/commits", utGeneralCommit)
  179. ut.GET("/dashboard/pkgs", utDashPkgsTree)
  180. ut.GET("/dashboard/curve", utDashCurve)
  181. ut.GET("/dashboard/histogram", utDashHistogram)
  182. ut.GET("/dashboard/histogram/user", utDashUserDetail)
  183. ut.GET("/app/list", utApps)
  184. }
  185. open := b.Group("/open")
  186. {
  187. open.GET("/get/", permit(per.OpenView), openProxyGet)
  188. open.POST("/post/", permit(per.OpenView), openProxyPost)
  189. }
  190. }
  191. // no auth
  192. d := e.Group("x/admin/apm")
  193. notAuth := d.Group("/")
  194. {
  195. notAuth.GET("canal/alarm", canalList) // 运维canal报警接口
  196. notAuth.GET("databus/clusters", databusClusters)
  197. notAuth.GET("databus/alarm", databusAlarm) // 运维databus告警用
  198. notAuth.GET("databus/alarms", databusAlarms) // 有所有group diff,查询时间巨长2分钟起板
  199. notAuth.GET("ut/baseline", utBaseline)
  200. notAuth.GET("noauth/discovery/fetch", discoveryProxyNoAuth) // 提供给自动告警查询对应实例
  201. notAuth.GET("ecode", ecodeList)
  202. notAuth.POST("databus/opsmind", databusOpsmind)
  203. notAuth.POST("databus/opsmind/remove", databusOpsmindRemove)
  204. notAuth.GET("databus/query", databusQuery)
  205. notAuth.GET("ecode/get/ecodes", getEcodes)
  206. notAuth.GET("ecode/get/prod/ecodes", getProdEcodes)
  207. notAuth.GET("ecode/langs", codeLangsList)
  208. }
  209. dapper := d.Group("/dapper")
  210. {
  211. dapper.GET("/", dapperProxy)
  212. }
  213. up := d.Group("/ut")
  214. {
  215. up.POST("/upload", upload)
  216. up.POST("/upload/app", uploadApp)
  217. //up.GET("/tyrant", check)
  218. up.GET("/check", check)
  219. up.POST("/merge/set", utSetMerged)
  220. up.GET("/dashboard/history/commit", dashHistoryCommit)
  221. up.GET("/git/report", utGitReport)
  222. }
  223. m := d.Group("/monitor")
  224. {
  225. m.GET("/apps", appNameList)
  226. m.GET("/prometheus", prometheusList)
  227. m.GET("/broadcast", broadcastList)
  228. m.GET("/databus", databusList)
  229. m.GET("/online", onlineList)
  230. }
  231. warn := d.Group("/warn")
  232. {
  233. warn.POST("/active", activeWarning)
  234. }
  235. }
  236. // Paper paper.
  237. type Paper struct {
  238. Total int `json:"total"`
  239. Pn int `json:"pn"`
  240. Ps int `json:"ps"`
  241. Items interface{} `json:"items"`
  242. }
  243. // ping check server ok.
  244. func ping(c *bm.Context) {
  245. if err := apmSvc.Ping(c); err != nil {
  246. c.AbortWithStatus(http.StatusServiceUnavailable)
  247. log.Error("apm-admin service ping error(%v)", err)
  248. }
  249. }
  250. func permit(rule string) bm.HandlerFunc {
  251. return func(ctx *bm.Context) {
  252. usernameI, _ := ctx.Get("username")
  253. username, ok := usernameI.(string)
  254. if !ok || username == "" {
  255. ctx.JSON(nil, ecode.NoLogin)
  256. ctx.Abort()
  257. return
  258. }
  259. if rule == "" {
  260. return
  261. }
  262. if err := apmSvc.Permit(ctx, username, rule); err != nil {
  263. log.Error("apmSvc.Permit(%s, %s) error(%v)", username, rule, err)
  264. ctx.JSON(nil, err)
  265. ctx.Abort()
  266. }
  267. }
  268. }