api.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. reqAccountAdmin := 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{}), 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("/datasources/", reqSignedIn, Index)
  26. r.Get("/datasources/edit/*", reqSignedIn, Index)
  27. r.Get("/org/users/", reqSignedIn, Index)
  28. r.Get("/org/apikeys/", reqSignedIn, Index)
  29. r.Get("/dashboard/import/", reqSignedIn, Index)
  30. r.Get("/admin/settings", reqGrafanaAdmin, Index)
  31. r.Get("/admin/users", reqGrafanaAdmin, Index)
  32. r.Get("/admin/users/create", reqGrafanaAdmin, Index)
  33. r.Get("/admin/users/edit/:id", reqGrafanaAdmin, Index)
  34. r.Get("/dashboard/*", reqSignedIn, Index)
  35. // sign up
  36. r.Get("/signup", Index)
  37. r.Post("/api/user/signup", bind(m.CreateUserCommand{}), SignUp)
  38. // dashboard snapshots
  39. r.Post("/api/snapshots/", bind(m.CreateDashboardSnapshotCommand{}), CreateDashboardSnapshot)
  40. r.Get("/dashboard/snapshot/*", Index)
  41. r.Get("/api/snapshots/:key", GetDashboardSnapshot)
  42. r.Get("/api/snapshots-delete/:key", DeleteDashboardSnapshot)
  43. // api renew session based on remember cookie
  44. r.Get("/api/login/ping", LoginApiPing)
  45. // authed api
  46. r.Group("/api", func() {
  47. // user
  48. r.Group("/user", func() {
  49. r.Get("/", wrap(GetSignedInUser))
  50. r.Put("/", bind(m.UpdateUserCommand{}), wrap(UpdateSignedInUser))
  51. r.Post("/using/:id", UserSetUsingOrg)
  52. r.Get("/orgs", wrap(GetSignedInUserOrgList))
  53. r.Post("/stars/dashboard/:id", StarDashboard)
  54. r.Delete("/stars/dashboard/:id", UnstarDashboard)
  55. r.Put("/password", bind(m.ChangeUserPasswordCommand{}), ChangeUserPassword)
  56. })
  57. // users
  58. r.Group("/users", func() {
  59. r.Get("/:id", wrap(GetUserById))
  60. r.Get("/:id/org", wrap(GetUserOrgList))
  61. r.Put("/:id", bind(m.UpdateUserCommand{}), wrap(UpdateUser))
  62. }, reqGrafanaAdmin)
  63. // account
  64. r.Group("/org", func() {
  65. r.Get("/", GetOrg)
  66. r.Post("/", bind(m.CreateOrgCommand{}), CreateOrg)
  67. r.Put("/", bind(m.UpdateOrgCommand{}), UpdateOrg)
  68. r.Post("/users", bind(m.AddOrgUserCommand{}), AddOrgUser)
  69. r.Get("/users", GetOrgUsers)
  70. r.Patch("/users/:id", bind(m.UpdateOrgUserCommand{}), UpdateOrgUser)
  71. r.Delete("/users/:id", RemoveOrgUser)
  72. }, reqAccountAdmin)
  73. // auth api keys
  74. r.Group("/auth/keys", func() {
  75. r.Get("/", GetApiKeys)
  76. r.Post("/", bind(m.AddApiKeyCommand{}), AddApiKey)
  77. r.Delete("/:id", DeleteApiKey)
  78. }, reqAccountAdmin)
  79. // Data sources
  80. r.Group("/datasources", func() {
  81. r.Combo("/").
  82. Get(GetDataSources).
  83. Put(bind(m.AddDataSourceCommand{}), AddDataSource).
  84. Post(bind(m.UpdateDataSourceCommand{}), UpdateDataSource)
  85. r.Delete("/:id", DeleteDataSource)
  86. r.Get("/:id", GetDataSourceById)
  87. r.Get("/plugins", GetDataSourcePlugins)
  88. }, reqAccountAdmin)
  89. r.Get("/frontend/settings/", GetFrontendSettings)
  90. r.Any("/datasources/proxy/:id/*", reqSignedIn, ProxyDataSourceRequest)
  91. // Dashboard
  92. r.Group("/dashboards", func() {
  93. r.Combo("/db/:slug").Get(GetDashboard).Delete(DeleteDashboard)
  94. r.Post("/db", reqEditorRole, bind(m.SaveDashboardCommand{}), PostDashboard)
  95. r.Get("/file/:file", GetDashboardFromJsonFile)
  96. r.Get("/home", GetHomeDashboard)
  97. r.Get("/tags", GetDashboardTags)
  98. })
  99. // Search
  100. r.Get("/search/", Search)
  101. // metrics
  102. r.Get("/metrics/test", GetTestMetrics)
  103. }, reqSignedIn)
  104. // admin api
  105. r.Group("/api/admin", func() {
  106. r.Get("/settings", AdminGetSettings)
  107. r.Get("/users", AdminSearchUsers)
  108. r.Post("/users", bind(dtos.AdminCreateUserForm{}), AdminCreateUser)
  109. r.Put("/users/:id/password", bind(dtos.AdminUpdateUserPasswordForm{}), AdminUpdateUserPassword)
  110. r.Put("/users/:id/permissions", bind(dtos.AdminUpdateUserPermissionsForm{}), AdminUpdateUserPermissions)
  111. r.Delete("/users/:id", AdminDeleteUser)
  112. }, reqGrafanaAdmin)
  113. // rendering
  114. r.Get("/render/*", reqSignedIn, RenderToPng)
  115. r.NotFound(NotFoundHandler)
  116. }