api.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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("/account/", reqSignedIn, Index)
  25. r.Get("/account/datasources/", reqSignedIn, Index)
  26. r.Get("/account/users/", reqSignedIn, Index)
  27. r.Get("/account/apikeys/", reqSignedIn, Index)
  28. r.Get("/account/import/", reqSignedIn, Index)
  29. r.Get("/admin/users", reqGrafanaAdmin, Index)
  30. r.Get("/dashboard/*", reqSignedIn, Index)
  31. // sign up
  32. r.Get("/signup", Index)
  33. r.Post("/api/user/signup", bind(m.CreateUserCommand{}), SignUp)
  34. // authed api
  35. r.Group("/api", func() {
  36. // user
  37. r.Group("/user", func() {
  38. r.Get("/", GetUser)
  39. r.Put("/", bind(m.UpdateUserCommand{}), UpdateUser)
  40. r.Post("/using/:id", SetUsingAccount)
  41. r.Get("/accounts", GetUserAccounts)
  42. r.Post("/stars/dashboard/:id", StarDashboard)
  43. r.Delete("/stars/dashboard/:id", UnstarDashboard)
  44. })
  45. // account
  46. r.Group("/account", func() {
  47. r.Get("/", GetAccount)
  48. r.Post("/", bind(m.CreateAccountCommand{}), CreateAccount)
  49. r.Put("/", bind(m.UpdateAccountCommand{}), UpdateAccount)
  50. r.Post("/users", bind(m.AddAccountUserCommand{}), AddAccountUser)
  51. r.Get("/users", GetAccountUsers)
  52. r.Delete("/users/:id", RemoveAccountUser)
  53. }, reqAccountAdmin)
  54. // auth api keys
  55. r.Group("/auth/keys", func() {
  56. r.Combo("/").
  57. Get(GetApiKeys).
  58. Post(bind(m.AddApiKeyCommand{}), AddApiKey).
  59. Put(bind(m.UpdateApiKeyCommand{}), UpdateApiKey)
  60. r.Delete("/:id", DeleteApiKey)
  61. }, reqAccountAdmin)
  62. // Data sources
  63. r.Group("/datasources", func() {
  64. r.Combo("/").Get(GetDataSources).Put(AddDataSource).Post(UpdateDataSource)
  65. r.Delete("/:id", DeleteDataSource)
  66. }, reqAccountAdmin)
  67. r.Any("/datasources/proxy/:id/*", reqSignedIn, ProxyDataSourceRequest)
  68. // Dashboard
  69. r.Group("/dashboards", func() {
  70. r.Combo("/db/:slug").Get(GetDashboard).Delete(DeleteDashboard)
  71. r.Post("/db", reqEditorRole, bind(m.SaveDashboardCommand{}), PostDashboard)
  72. r.Get("/home", GetHomeDashboard)
  73. })
  74. // Search
  75. r.Get("/search/", Search)
  76. // metrics
  77. r.Get("/metrics/test", GetTestMetrics)
  78. }, reqSignedIn)
  79. // admin api
  80. r.Group("/api/admin", func() {
  81. r.Get("/users", AdminSearchUsers)
  82. r.Get("/users/:id", AdminGetUser)
  83. r.Post("/users", bind(dtos.AdminCreateUserForm{}), AdminCreateUser)
  84. r.Put("/users/:id", bind(dtos.AdminUpdateUserForm{}), AdminUpdateUser)
  85. }, reqGrafanaAdmin)
  86. // rendering
  87. r.Get("/render/*", reqSignedIn, RenderToPng)
  88. r.NotFound(NotFound)
  89. }