api.go 11 KB

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