api.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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/*", 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.Get("/preferences", wrap(GetUserPreferences))
  96. r.Put("/preferences", bind(dtos.UpdatePrefsCmd{}), wrap(UpdateUserPreferences))
  97. })
  98. // users (admin permission required)
  99. r.Group("/users", func() {
  100. r.Get("/", wrap(SearchUsers))
  101. r.Get("/:id", wrap(GetUserById))
  102. r.Get("/:id/orgs", wrap(GetUserOrgList))
  103. r.Put("/:id", bind(m.UpdateUserCommand{}), wrap(UpdateUser))
  104. r.Post("/:id/using/:orgId", wrap(UpdateUserActiveOrg))
  105. }, reqGrafanaAdmin)
  106. // org information available to all users.
  107. r.Group("/org", func() {
  108. r.Get("/", wrap(GetOrgCurrent))
  109. r.Get("/quotas", wrap(GetOrgQuotas))
  110. })
  111. // current org
  112. r.Group("/org", func() {
  113. r.Put("/", bind(dtos.UpdateOrgForm{}), wrap(UpdateOrgCurrent))
  114. r.Put("/address", bind(dtos.UpdateOrgAddressForm{}), wrap(UpdateOrgAddressCurrent))
  115. r.Post("/users", quota("user"), bind(m.AddOrgUserCommand{}), wrap(AddOrgUserToCurrentOrg))
  116. r.Get("/users", wrap(GetOrgUsersForCurrentOrg))
  117. r.Patch("/users/:userId", bind(m.UpdateOrgUserCommand{}), wrap(UpdateOrgUserForCurrentOrg))
  118. r.Delete("/users/:userId", wrap(RemoveOrgUserForCurrentOrg))
  119. // invites
  120. r.Get("/invites", wrap(GetPendingOrgInvites))
  121. r.Post("/invites", quota("user"), bind(dtos.AddInviteForm{}), wrap(AddOrgInvite))
  122. r.Patch("/invites/:code/revoke", wrap(RevokeInvite))
  123. // prefs
  124. r.Get("/preferences", wrap(GetOrgPreferences))
  125. r.Put("/preferences", bind(dtos.UpdatePrefsCmd{}), wrap(UpdateOrgPreferences))
  126. }, reqOrgAdmin)
  127. // create new org
  128. r.Post("/orgs", quota("org"), bind(m.CreateOrgCommand{}), wrap(CreateOrg))
  129. // search all orgs
  130. r.Get("/orgs", reqGrafanaAdmin, wrap(SearchOrgs))
  131. // orgs (admin routes)
  132. r.Group("/orgs/:orgId", func() {
  133. r.Get("/", wrap(GetOrgById))
  134. r.Put("/", bind(dtos.UpdateOrgForm{}), wrap(UpdateOrg))
  135. r.Put("/address", bind(dtos.UpdateOrgAddressForm{}), wrap(UpdateOrgAddress))
  136. r.Delete("/", wrap(DeleteOrgById))
  137. r.Get("/users", wrap(GetOrgUsers))
  138. r.Post("/users", bind(m.AddOrgUserCommand{}), wrap(AddOrgUser))
  139. r.Patch("/users/:userId", bind(m.UpdateOrgUserCommand{}), wrap(UpdateOrgUser))
  140. r.Delete("/users/:userId", wrap(RemoveOrgUser))
  141. r.Get("/quotas", wrap(GetOrgQuotas))
  142. r.Put("/quotas/:target", bind(m.UpdateOrgQuotaCmd{}), wrap(UpdateOrgQuota))
  143. }, reqGrafanaAdmin)
  144. // orgs (admin routes)
  145. r.Group("/orgs/name/:name", func() {
  146. r.Get("/", wrap(GetOrgByName))
  147. }, reqGrafanaAdmin)
  148. // auth api keys
  149. r.Group("/auth/keys", func() {
  150. r.Get("/", wrap(GetApiKeys))
  151. r.Post("/", quota("api_key"), bind(m.AddApiKeyCommand{}), wrap(AddApiKey))
  152. r.Delete("/:id", wrap(DeleteApiKey))
  153. }, reqOrgAdmin)
  154. // Preferences
  155. r.Group("/preferences", func() {
  156. r.Post("/set-home-dash", bind(m.SavePreferencesCommand{}), wrap(SetHomeDashboard))
  157. })
  158. // Data sources
  159. r.Group("/datasources", func() {
  160. r.Get("/", GetDataSources)
  161. r.Post("/", quota("data_source"), bind(m.AddDataSourceCommand{}), AddDataSource)
  162. r.Put("/:id", bind(m.UpdateDataSourceCommand{}), UpdateDataSource)
  163. r.Delete("/:id", DeleteDataSource)
  164. r.Get("/:id", wrap(GetDataSourceById))
  165. r.Get("/name/:name", wrap(GetDataSourceByName))
  166. }, reqOrgAdmin)
  167. r.Get("/datasources/id/:name", wrap(GetDataSourceIdByName), reqSignedIn)
  168. r.Get("/plugins", wrap(GetPluginList))
  169. r.Get("/plugins/:pluginId/settings", wrap(GetPluginSettingById))
  170. r.Group("/plugins", func() {
  171. r.Get("/:pluginId/readme", wrap(GetPluginReadme))
  172. r.Get("/:pluginId/dashboards/", wrap(GetPluginDashboards))
  173. r.Post("/:pluginId/settings", bind(m.UpdatePluginSettingCmd{}), wrap(UpdatePluginSetting))
  174. }, reqOrgAdmin)
  175. r.Get("/frontend/settings/", GetFrontendSettings)
  176. r.Any("/datasources/proxy/:id/*", reqSignedIn, ProxyDataSourceRequest)
  177. r.Any("/datasources/proxy/:id", reqSignedIn, ProxyDataSourceRequest)
  178. // Dashboard
  179. r.Group("/dashboards", func() {
  180. r.Combo("/db/:slug").Get(GetDashboard).Delete(DeleteDashboard)
  181. r.Post("/db", reqEditorRole, bind(m.SaveDashboardCommand{}), wrap(PostDashboard))
  182. r.Get("/file/:file", GetDashboardFromJsonFile)
  183. r.Get("/home", wrap(GetHomeDashboard))
  184. r.Get("/tags", GetDashboardTags)
  185. r.Post("/import", bind(dtos.ImportDashboardCommand{}), wrap(ImportDashboard))
  186. })
  187. // Dashboard snapshots
  188. r.Group("/dashboard/snapshots", func() {
  189. r.Get("/", wrap(SearchDashboardSnapshots))
  190. })
  191. // Playlist
  192. r.Group("/playlists", func() {
  193. r.Get("/", wrap(SearchPlaylists))
  194. r.Get("/:id", ValidateOrgPlaylist, wrap(GetPlaylist))
  195. r.Get("/:id/items", ValidateOrgPlaylist, wrap(GetPlaylistItems))
  196. r.Get("/:id/dashboards", ValidateOrgPlaylist, wrap(GetPlaylistDashboards))
  197. r.Delete("/:id", reqEditorRole, ValidateOrgPlaylist, wrap(DeletePlaylist))
  198. r.Put("/:id", reqEditorRole, bind(m.UpdatePlaylistCommand{}), ValidateOrgPlaylist, wrap(UpdatePlaylist))
  199. r.Post("/", reqEditorRole, bind(m.CreatePlaylistCommand{}), wrap(CreatePlaylist))
  200. })
  201. // Search
  202. r.Get("/search/", Search)
  203. // metrics
  204. r.Get("/metrics/test", wrap(GetTestMetrics))
  205. // metrics
  206. r.Get("/metrics", wrap(GetInternalMetrics))
  207. r.Group("/alerts", func() {
  208. r.Post("/test", bind(dtos.AlertTestCommand{}), wrap(AlertTest))
  209. r.Get("/:alertId", ValidateOrgAlert, wrap(GetAlert))
  210. r.Get("/", wrap(GetAlerts))
  211. })
  212. r.Get("/alert-notifications", wrap(GetAlertNotifications))
  213. r.Group("/alert-notifications", func() {
  214. r.Post("/test", bind(dtos.NotificationTestCommand{}), wrap(NotificationTest))
  215. r.Post("/", bind(m.CreateAlertNotificationCommand{}), wrap(CreateAlertNotification))
  216. r.Put("/:notificationId", bind(m.UpdateAlertNotificationCommand{}), wrap(UpdateAlertNotification))
  217. r.Get("/:notificationId", wrap(GetAlertNotificationById))
  218. r.Delete("/:notificationId", wrap(DeleteAlertNotification))
  219. }, reqOrgAdmin)
  220. r.Get("/annotations", wrap(GetAnnotations))
  221. // error test
  222. r.Get("/metrics/error", wrap(GenerateError))
  223. }, reqSignedIn)
  224. // admin api
  225. r.Group("/api/admin", func() {
  226. r.Get("/settings", AdminGetSettings)
  227. r.Post("/users", bind(dtos.AdminCreateUserForm{}), AdminCreateUser)
  228. r.Put("/users/:id/password", bind(dtos.AdminUpdateUserPasswordForm{}), AdminUpdateUserPassword)
  229. r.Put("/users/:id/permissions", bind(dtos.AdminUpdateUserPermissionsForm{}), AdminUpdateUserPermissions)
  230. r.Delete("/users/:id", AdminDeleteUser)
  231. r.Get("/users/:id/quotas", wrap(GetUserQuotas))
  232. r.Put("/users/:id/quotas/:target", bind(m.UpdateUserQuotaCmd{}), wrap(UpdateUserQuota))
  233. r.Get("/stats", AdminGetStats)
  234. }, reqGrafanaAdmin)
  235. // rendering
  236. r.Get("/render/*", reqSignedIn, RenderToPng)
  237. // grafana.net proxy
  238. r.Any("/api/gnet/*", reqSignedIn, ProxyGnetRequest)
  239. // Gravatar service.
  240. avt := avatar.CacheServer()
  241. r.Get("/avatar/:hash", avt.ServeHTTP)
  242. // Websocket
  243. liveConn := live.New()
  244. r.Any("/ws", liveConn.Serve)
  245. // streams
  246. r.Post("/api/streams/push", reqSignedIn, bind(dtos.StreamMessage{}), liveConn.PushToStream)
  247. InitAppPluginRoutes(r)
  248. }