api.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package api
  2. import (
  3. "github.com/Unknwon/macaron"
  4. "github.com/grafana/grafana/pkg/api/dtos"
  5. "github.com/grafana/grafana/pkg/middleware"
  6. m "github.com/grafana/grafana/pkg/models"
  7. "github.com/macaron-contrib/binding"
  8. )
  9. // Register adds http routes
  10. func Register(r *macaron.Macaron) {
  11. reqSignedIn := middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true})
  12. reqGrafanaAdmin := middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true, ReqGrafanaAdmin: true})
  13. reqEditorRole := middleware.RoleAuth(m.ROLE_EDITOR, m.ROLE_ADMIN)
  14. regOrgAdmin := middleware.RoleAuth(m.ROLE_ADMIN)
  15. bind := binding.Bind
  16. // not logged in views
  17. r.Get("/", reqSignedIn, Index)
  18. r.Get("/logout", Logout)
  19. r.Post("/login", bind(dtos.LoginCommand{}), wrap(LoginPost))
  20. r.Get("/login/:name", OAuthLogin)
  21. r.Get("/login", LoginView)
  22. // authed views
  23. r.Get("/profile/", reqSignedIn, Index)
  24. r.Get("/org/", reqSignedIn, Index)
  25. r.Get("/org/new", reqSignedIn, Index)
  26. r.Get("/datasources/", reqSignedIn, Index)
  27. r.Get("/datasources/edit/*", reqSignedIn, Index)
  28. r.Get("/org/users/", reqSignedIn, Index)
  29. r.Get("/org/apikeys/", reqSignedIn, Index)
  30. r.Get("/dashboard/import/", reqSignedIn, Index)
  31. r.Get("/admin/settings", reqGrafanaAdmin, Index)
  32. r.Get("/admin/users", reqGrafanaAdmin, Index)
  33. r.Get("/admin/users/create", reqGrafanaAdmin, Index)
  34. r.Get("/admin/users/edit/:id", reqGrafanaAdmin, Index)
  35. r.Get("/dashboard/*", reqSignedIn, Index)
  36. // sign up
  37. r.Post("/api/user/signup", bind(m.CreateUserCommand{}), wrap(SignUp))
  38. // reset password
  39. r.Get("/user/password/send-reset-email", Index)
  40. r.Get("/user/password/reset", Index)
  41. r.Post("/api/user/password/send-reset-email", bind(dtos.SendResetPasswordEmailForm{}), wrap(SendResetPasswordEmail))
  42. r.Post("/api/user/password/reset", bind(dtos.ResetUserPasswordForm{}), wrap(ResetPassword))
  43. // dashboard snapshots
  44. r.Post("/api/snapshots/", bind(m.CreateDashboardSnapshotCommand{}), CreateDashboardSnapshot)
  45. r.Get("/dashboard/snapshot/*", Index)
  46. r.Get("/api/snapshots/:key", GetDashboardSnapshot)
  47. r.Get("/api/snapshots-delete/:key", DeleteDashboardSnapshot)
  48. // api renew session based on remember cookie
  49. r.Get("/api/login/ping", LoginApiPing)
  50. // authed api
  51. r.Group("/api", func() {
  52. // user (signed in)
  53. r.Group("/user", func() {
  54. r.Get("/", wrap(GetSignedInUser))
  55. r.Put("/", bind(m.UpdateUserCommand{}), wrap(UpdateSignedInUser))
  56. r.Post("/using/:id", wrap(UserSetUsingOrg))
  57. r.Get("/orgs", wrap(GetSignedInUserOrgList))
  58. r.Post("/stars/dashboard/:id", wrap(StarDashboard))
  59. r.Delete("/stars/dashboard/:id", wrap(UnstarDashboard))
  60. r.Put("/password", bind(m.ChangeUserPasswordCommand{}), wrap(ChangeUserPassword))
  61. })
  62. // users (admin permission required)
  63. r.Group("/users", func() {
  64. r.Get("/", wrap(SearchUsers))
  65. r.Get("/:id", wrap(GetUserById))
  66. r.Get("/:id/orgs", wrap(GetUserOrgList))
  67. r.Put("/:id", bind(m.UpdateUserCommand{}), wrap(UpdateUser))
  68. }, reqGrafanaAdmin)
  69. // current org
  70. r.Group("/org", func() {
  71. r.Get("/", wrap(GetOrgCurrent))
  72. r.Put("/", bind(m.UpdateOrgCommand{}), wrap(UpdateOrgCurrent))
  73. r.Post("/users", bind(m.AddOrgUserCommand{}), wrap(AddOrgUserToCurrentOrg))
  74. r.Get("/users", wrap(GetOrgUsersForCurrentOrg))
  75. r.Patch("/users/:userId", bind(m.UpdateOrgUserCommand{}), wrap(UpdateOrgUserForCurrentOrg))
  76. r.Delete("/users/:userId", wrap(RemoveOrgUserForCurrentOrg))
  77. // invites
  78. r.Get("/invites", wrap(GetPendingOrgInvites))
  79. r.Post("/invites", bind(dtos.AddInviteForm{}), wrap(AddOrgInvite))
  80. }, regOrgAdmin)
  81. // create new org
  82. r.Post("/orgs", bind(m.CreateOrgCommand{}), wrap(CreateOrg))
  83. // search all orgs
  84. r.Get("/orgs", reqGrafanaAdmin, wrap(SearchOrgs))
  85. // orgs (admin routes)
  86. r.Group("/orgs/:orgId", func() {
  87. r.Put("/", bind(m.UpdateOrgCommand{}), wrap(UpdateOrg))
  88. r.Get("/users", wrap(GetOrgUsers))
  89. r.Post("/users", bind(m.AddOrgUserCommand{}), wrap(AddOrgUser))
  90. r.Patch("/users/:userId", bind(m.UpdateOrgUserCommand{}), wrap(UpdateOrgUser))
  91. r.Delete("/users/:userId", wrap(RemoveOrgUser))
  92. }, reqGrafanaAdmin)
  93. // auth api keys
  94. r.Group("/auth/keys", func() {
  95. r.Get("/", wrap(GetApiKeys))
  96. r.Post("/", bind(m.AddApiKeyCommand{}), wrap(AddApiKey))
  97. r.Delete("/:id", wrap(DeleteApiKey))
  98. }, regOrgAdmin)
  99. // Data sources
  100. r.Group("/datasources", func() {
  101. r.Get("/", GetDataSources)
  102. r.Post("/", bind(m.AddDataSourceCommand{}), AddDataSource)
  103. r.Put("/:id", bind(m.UpdateDataSourceCommand{}), UpdateDataSource)
  104. r.Delete("/:id", DeleteDataSource)
  105. r.Get("/:id", GetDataSourceById)
  106. r.Get("/plugins", GetDataSourcePlugins)
  107. }, regOrgAdmin)
  108. r.Get("/frontend/settings/", GetFrontendSettings)
  109. r.Any("/datasources/proxy/:id/*", reqSignedIn, ProxyDataSourceRequest)
  110. r.Any("/datasources/proxy/:id", reqSignedIn, ProxyDataSourceRequest)
  111. // Dashboard
  112. r.Group("/dashboards", func() {
  113. r.Combo("/db/:slug").Get(GetDashboard).Delete(DeleteDashboard)
  114. r.Post("/db", reqEditorRole, bind(m.SaveDashboardCommand{}), PostDashboard)
  115. r.Get("/file/:file", GetDashboardFromJsonFile)
  116. r.Get("/home", GetHomeDashboard)
  117. r.Get("/tags", GetDashboardTags)
  118. })
  119. // Search
  120. r.Get("/search/", Search)
  121. // metrics
  122. r.Get("/metrics/test", GetTestMetrics)
  123. }, reqSignedIn)
  124. // admin api
  125. r.Group("/api/admin", func() {
  126. r.Get("/settings", AdminGetSettings)
  127. r.Post("/users", bind(dtos.AdminCreateUserForm{}), AdminCreateUser)
  128. r.Put("/users/:id/password", bind(dtos.AdminUpdateUserPasswordForm{}), AdminUpdateUserPassword)
  129. r.Put("/users/:id/permissions", bind(dtos.AdminUpdateUserPermissionsForm{}), AdminUpdateUserPermissions)
  130. r.Delete("/users/:id", AdminDeleteUser)
  131. }, reqGrafanaAdmin)
  132. // rendering
  133. r.Get("/render/*", reqSignedIn, RenderToPng)
  134. r.NotFound(NotFoundHandler)
  135. }