api.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. package api
  2. import (
  3. "github.com/go-macaron/binding"
  4. "github.com/grafana/grafana/pkg/api/avatar"
  5. "github.com/grafana/grafana/pkg/api/dtos"
  6. "github.com/grafana/grafana/pkg/api/live"
  7. "github.com/grafana/grafana/pkg/middleware"
  8. m "github.com/grafana/grafana/pkg/models"
  9. "gopkg.in/macaron.v1"
  10. )
  11. // Register adds http routes
  12. func Register(r *macaron.Macaron) {
  13. reqSignedIn := middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true})
  14. reqGrafanaAdmin := middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true, ReqGrafanaAdmin: true})
  15. reqEditorRole := middleware.RoleAuth(m.ROLE_EDITOR, m.ROLE_ADMIN)
  16. reqOrgAdmin := middleware.RoleAuth(m.ROLE_ADMIN)
  17. quota := middleware.Quota
  18. bind := binding.Bind
  19. // not logged in views
  20. r.Get("/", reqSignedIn, Index)
  21. r.Get("/logout", Logout)
  22. r.Post("/login", quota("session"), bind(dtos.LoginCommand{}), wrap(LoginPost))
  23. r.Get("/login/:name", quota("session"), OAuthLogin)
  24. r.Get("/login", LoginView)
  25. r.Get("/invite/:code", Index)
  26. // authed views
  27. r.Get("/profile/", reqSignedIn, Index)
  28. r.Get("/org/", reqSignedIn, Index)
  29. r.Get("/org/new", reqSignedIn, Index)
  30. r.Get("/datasources/", reqSignedIn, Index)
  31. r.Get("/datasources/edit/*", reqSignedIn, Index)
  32. r.Get("/org/users/", reqSignedIn, Index)
  33. r.Get("/org/apikeys/", reqSignedIn, Index)
  34. r.Get("/dashboard/import/", reqSignedIn, Index)
  35. r.Get("/admin", reqGrafanaAdmin, Index)
  36. r.Get("/admin/settings", reqGrafanaAdmin, Index)
  37. r.Get("/admin/users", reqGrafanaAdmin, Index)
  38. r.Get("/admin/users/create", reqGrafanaAdmin, Index)
  39. r.Get("/admin/users/edit/:id", reqGrafanaAdmin, Index)
  40. r.Get("/admin/orgs", reqGrafanaAdmin, Index)
  41. r.Get("/admin/orgs/edit/:id", reqGrafanaAdmin, Index)
  42. r.Get("/admin/stats", reqGrafanaAdmin, Index)
  43. r.Get("/styleguide", reqSignedIn, Index)
  44. r.Get("/plugins", reqSignedIn, Index)
  45. r.Get("/plugins/:id/edit", reqSignedIn, Index)
  46. r.Get("/plugins/:id/page/:page", reqSignedIn, Index)
  47. r.Get("/dashboard/*", reqSignedIn, Index)
  48. r.Get("/dashboard-solo/*", reqSignedIn, Index)
  49. r.Get("/playlists/", reqSignedIn, Index)
  50. r.Get("/playlists/*", reqSignedIn, Index)
  51. // sign up
  52. r.Get("/signup", Index)
  53. r.Get("/api/user/signup/options", wrap(GetSignUpOptions))
  54. r.Post("/api/user/signup", quota("user"), bind(dtos.SignUpForm{}), wrap(SignUp))
  55. r.Post("/api/user/signup/step2", bind(dtos.SignUpStep2Form{}), wrap(SignUpStep2))
  56. // invited
  57. r.Get("/api/user/invite/:code", wrap(GetInviteInfoByCode))
  58. r.Post("/api/user/invite/complete", bind(dtos.CompleteInviteForm{}), wrap(CompleteInvite))
  59. // reset password
  60. r.Get("/user/password/send-reset-email", Index)
  61. r.Get("/user/password/reset", Index)
  62. r.Post("/api/user/password/send-reset-email", bind(dtos.SendResetPasswordEmailForm{}), wrap(SendResetPasswordEmail))
  63. r.Post("/api/user/password/reset", bind(dtos.ResetUserPasswordForm{}), wrap(ResetPassword))
  64. // dashboard snapshots
  65. r.Get("/dashboard/snapshot/*", Index)
  66. r.Get("/dashboard/snapshots/", reqSignedIn, Index)
  67. // api for dashboard snapshots
  68. r.Post("/api/snapshots/", bind(m.CreateDashboardSnapshotCommand{}), CreateDashboardSnapshot)
  69. r.Get("/api/snapshot/shared-options/", GetSharingOptions)
  70. r.Get("/api/snapshots/:key", GetDashboardSnapshot)
  71. r.Get("/api/snapshots-delete/:key", reqEditorRole, DeleteDashboardSnapshot)
  72. // api renew session based on remember cookie
  73. r.Get("/api/login/ping", quota("session"), LoginApiPing)
  74. // authed api
  75. r.Group("/api", func() {
  76. // user (signed in)
  77. r.Group("/user", func() {
  78. r.Get("/", wrap(GetSignedInUser))
  79. r.Put("/", bind(m.UpdateUserCommand{}), wrap(UpdateSignedInUser))
  80. r.Post("/using/:id", wrap(UserSetUsingOrg))
  81. r.Get("/orgs", wrap(GetSignedInUserOrgList))
  82. r.Post("/stars/dashboard/:id", wrap(StarDashboard))
  83. r.Delete("/stars/dashboard/:id", wrap(UnstarDashboard))
  84. r.Put("/password", bind(m.ChangeUserPasswordCommand{}), wrap(ChangeUserPassword))
  85. r.Get("/quotas", wrap(GetUserQuotas))
  86. r.Get("/preferences", wrap(GetUserPreferences))
  87. r.Put("/preferences", bind(dtos.UpdatePrefsCmd{}), wrap(UpdateUserPreferences))
  88. })
  89. // users (admin permission required)
  90. r.Group("/users", func() {
  91. r.Get("/", wrap(SearchUsers))
  92. r.Get("/:id", wrap(GetUserById))
  93. r.Get("/:id/orgs", wrap(GetUserOrgList))
  94. r.Put("/:id", bind(m.UpdateUserCommand{}), wrap(UpdateUser))
  95. }, reqGrafanaAdmin)
  96. // org information available to all users.
  97. r.Group("/org", func() {
  98. r.Get("/", wrap(GetOrgCurrent))
  99. r.Get("/quotas", wrap(GetOrgQuotas))
  100. })
  101. // current org
  102. r.Group("/org", func() {
  103. r.Put("/", bind(dtos.UpdateOrgForm{}), wrap(UpdateOrgCurrent))
  104. r.Put("/address", bind(dtos.UpdateOrgAddressForm{}), wrap(UpdateOrgAddressCurrent))
  105. r.Post("/users", quota("user"), bind(m.AddOrgUserCommand{}), wrap(AddOrgUserToCurrentOrg))
  106. r.Get("/users", wrap(GetOrgUsersForCurrentOrg))
  107. r.Patch("/users/:userId", bind(m.UpdateOrgUserCommand{}), wrap(UpdateOrgUserForCurrentOrg))
  108. r.Delete("/users/:userId", wrap(RemoveOrgUserForCurrentOrg))
  109. // invites
  110. r.Get("/invites", wrap(GetPendingOrgInvites))
  111. r.Post("/invites", quota("user"), bind(dtos.AddInviteForm{}), wrap(AddOrgInvite))
  112. r.Patch("/invites/:code/revoke", wrap(RevokeInvite))
  113. // prefs
  114. r.Get("/preferences", wrap(GetOrgPreferences))
  115. r.Put("/preferences", bind(dtos.UpdatePrefsCmd{}), wrap(UpdateOrgPreferences))
  116. }, reqOrgAdmin)
  117. // create new org
  118. r.Post("/orgs", quota("org"), bind(m.CreateOrgCommand{}), wrap(CreateOrg))
  119. // search all orgs
  120. r.Get("/orgs", reqGrafanaAdmin, wrap(SearchOrgs))
  121. // orgs (admin routes)
  122. r.Group("/orgs/:orgId", func() {
  123. r.Get("/", wrap(GetOrgById))
  124. r.Put("/", bind(dtos.UpdateOrgForm{}), wrap(UpdateOrg))
  125. r.Put("/address", bind(dtos.UpdateOrgAddressForm{}), wrap(UpdateOrgAddress))
  126. r.Delete("/", wrap(DeleteOrgById))
  127. r.Get("/users", wrap(GetOrgUsers))
  128. r.Post("/users", bind(m.AddOrgUserCommand{}), wrap(AddOrgUser))
  129. r.Patch("/users/:userId", bind(m.UpdateOrgUserCommand{}), wrap(UpdateOrgUser))
  130. r.Delete("/users/:userId", wrap(RemoveOrgUser))
  131. r.Get("/quotas", wrap(GetOrgQuotas))
  132. r.Put("/quotas/:target", bind(m.UpdateOrgQuotaCmd{}), wrap(UpdateOrgQuota))
  133. }, reqGrafanaAdmin)
  134. // orgs (admin routes)
  135. r.Group("/orgs/name/:name", func() {
  136. r.Get("/", wrap(GetOrgByName))
  137. }, reqGrafanaAdmin)
  138. // auth api keys
  139. r.Group("/auth/keys", func() {
  140. r.Get("/", wrap(GetApiKeys))
  141. r.Post("/", quota("api_key"), bind(m.AddApiKeyCommand{}), wrap(AddApiKey))
  142. r.Delete("/:id", wrap(DeleteApiKey))
  143. }, reqOrgAdmin)
  144. // Preferences
  145. r.Group("/preferences", func() {
  146. r.Post("/set-home-dash", bind(m.SavePreferencesCommand{}), wrap(SetHomeDashboard))
  147. })
  148. // Data sources
  149. r.Group("/datasources", func() {
  150. r.Get("/", GetDataSources)
  151. r.Post("/", quota("data_source"), bind(m.AddDataSourceCommand{}), AddDataSource)
  152. r.Put("/:id", bind(m.UpdateDataSourceCommand{}), UpdateDataSource)
  153. r.Delete("/:id", DeleteDataSource)
  154. r.Get("/:id", wrap(GetDataSourceById))
  155. r.Get("/name/:name", wrap(GetDataSourceByName))
  156. }, reqOrgAdmin)
  157. r.Get("/datasources/id/:name", wrap(GetDataSourceIdByName), reqSignedIn)
  158. r.Group("/plugins", func() {
  159. r.Get("/", wrap(GetPluginList))
  160. r.Get("/:pluginId/readme", wrap(GetPluginReadme))
  161. r.Get("/:pluginId/dashboards/", wrap(GetPluginDashboards))
  162. r.Get("/:pluginId/settings", wrap(GetPluginSettingById))
  163. r.Post("/:pluginId/settings", bind(m.UpdatePluginSettingCmd{}), wrap(UpdatePluginSetting))
  164. }, reqOrgAdmin)
  165. r.Get("/frontend/settings/", GetFrontendSettings)
  166. r.Any("/datasources/proxy/:id/*", reqSignedIn, ProxyDataSourceRequest)
  167. r.Any("/datasources/proxy/:id", reqSignedIn, ProxyDataSourceRequest)
  168. // Dashboard
  169. r.Group("/dashboards", func() {
  170. r.Combo("/db/:slug").Get(GetDashboard).Delete(DeleteDashboard)
  171. r.Post("/db", reqEditorRole, bind(m.SaveDashboardCommand{}), PostDashboard)
  172. r.Get("/file/:file", GetDashboardFromJsonFile)
  173. r.Get("/home", GetHomeDashboard)
  174. r.Get("/tags", GetDashboardTags)
  175. r.Post("/import", bind(dtos.ImportDashboardCommand{}), wrap(ImportDashboard))
  176. })
  177. // Dashboard snapshots
  178. r.Group("/dashboard/snapshots", func() {
  179. r.Get("/", wrap(SearchDashboardSnapshots))
  180. })
  181. // Playlist
  182. r.Group("/playlists", func() {
  183. r.Get("/", wrap(SearchPlaylists))
  184. r.Get("/:id", ValidateOrgPlaylist, wrap(GetPlaylist))
  185. r.Get("/:id/items", ValidateOrgPlaylist, wrap(GetPlaylistItems))
  186. r.Get("/:id/dashboards", ValidateOrgPlaylist, wrap(GetPlaylistDashboards))
  187. r.Delete("/:id", reqEditorRole, ValidateOrgPlaylist, wrap(DeletePlaylist))
  188. r.Put("/:id", reqEditorRole, bind(m.UpdatePlaylistCommand{}), ValidateOrgPlaylist, wrap(UpdatePlaylist))
  189. r.Post("/", reqEditorRole, bind(m.CreatePlaylistCommand{}), wrap(CreatePlaylist))
  190. })
  191. // Search
  192. r.Get("/search/", Search)
  193. // metrics
  194. r.Get("/metrics/test", GetTestMetrics)
  195. }, reqSignedIn)
  196. // admin api
  197. r.Group("/api/admin", func() {
  198. r.Get("/settings", AdminGetSettings)
  199. r.Post("/users", bind(dtos.AdminCreateUserForm{}), AdminCreateUser)
  200. r.Put("/users/:id/password", bind(dtos.AdminUpdateUserPasswordForm{}), AdminUpdateUserPassword)
  201. r.Put("/users/:id/permissions", bind(dtos.AdminUpdateUserPermissionsForm{}), AdminUpdateUserPermissions)
  202. r.Delete("/users/:id", AdminDeleteUser)
  203. r.Get("/users/:id/quotas", wrap(GetUserQuotas))
  204. r.Put("/users/:id/quotas/:target", bind(m.UpdateUserQuotaCmd{}), wrap(UpdateUserQuota))
  205. r.Get("/stats", AdminGetStats)
  206. }, reqGrafanaAdmin)
  207. // rendering
  208. r.Get("/render/*", reqSignedIn, RenderToPng)
  209. // Gravatar service.
  210. avt := avatar.CacheServer()
  211. r.Get("/avatar/:hash", avt.ServeHTTP)
  212. // Websocket
  213. liveConn := live.New()
  214. r.Any("/ws", liveConn.Serve)
  215. // streams
  216. r.Post("/api/streams/push", reqSignedIn, bind(dtos.StreamMessage{}), liveConn.PushToStream)
  217. InitAppPluginRoutes(r)
  218. }