api.go 12 KB

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