api.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. // user group (admin permission required)
  113. r.Group("/user-groups", func() {
  114. r.Get("/:userGroupId", wrap(GetUserGroupById))
  115. r.Get("/search", wrap(SearchUserGroups))
  116. r.Post("/", quota("user-groups"), bind(m.CreateUserGroupCommand{}), wrap(CreateUserGroup))
  117. r.Put("/:userGroupId", bind(m.UpdateUserGroupCommand{}), wrap(UpdateUserGroup))
  118. r.Delete("/:userGroupId", wrap(DeleteUserGroupById))
  119. r.Get("/:userGroupId/members", wrap(GetUserGroupMembers))
  120. r.Post("/:userGroupId/members", quota("user-groups"), bind(m.AddUserGroupMemberCommand{}), wrap(AddUserGroupMember))
  121. r.Delete("/:userGroupId/members/:userId", wrap(RemoveUserGroupMember))
  122. }, reqOrgAdmin)
  123. // org information available to all users.
  124. r.Group("/org", func() {
  125. r.Get("/", wrap(GetOrgCurrent))
  126. r.Get("/quotas", wrap(GetOrgQuotas))
  127. })
  128. // current org
  129. r.Group("/org", func() {
  130. r.Put("/", bind(dtos.UpdateOrgForm{}), wrap(UpdateOrgCurrent))
  131. r.Put("/address", bind(dtos.UpdateOrgAddressForm{}), wrap(UpdateOrgAddressCurrent))
  132. r.Post("/users", quota("user"), bind(m.AddOrgUserCommand{}), wrap(AddOrgUserToCurrentOrg))
  133. r.Get("/users", wrap(GetOrgUsersForCurrentOrg))
  134. r.Patch("/users/:userId", bind(m.UpdateOrgUserCommand{}), wrap(UpdateOrgUserForCurrentOrg))
  135. r.Delete("/users/:userId", wrap(RemoveOrgUserForCurrentOrg))
  136. // invites
  137. r.Get("/invites", wrap(GetPendingOrgInvites))
  138. r.Post("/invites", quota("user"), bind(dtos.AddInviteForm{}), wrap(AddOrgInvite))
  139. r.Patch("/invites/:code/revoke", wrap(RevokeInvite))
  140. // prefs
  141. r.Get("/preferences", wrap(GetOrgPreferences))
  142. r.Put("/preferences", bind(dtos.UpdatePrefsCmd{}), wrap(UpdateOrgPreferences))
  143. }, reqOrgAdmin)
  144. // create new org
  145. r.Post("/orgs", quota("org"), bind(m.CreateOrgCommand{}), wrap(CreateOrg))
  146. // search all orgs
  147. r.Get("/orgs", reqGrafanaAdmin, wrap(SearchOrgs))
  148. // orgs (admin routes)
  149. r.Group("/orgs/:orgId", func() {
  150. r.Get("/", wrap(GetOrgById))
  151. r.Put("/", bind(dtos.UpdateOrgForm{}), wrap(UpdateOrg))
  152. r.Put("/address", bind(dtos.UpdateOrgAddressForm{}), wrap(UpdateOrgAddress))
  153. r.Delete("/", wrap(DeleteOrgById))
  154. r.Get("/users", wrap(GetOrgUsers))
  155. r.Post("/users", bind(m.AddOrgUserCommand{}), wrap(AddOrgUser))
  156. r.Patch("/users/:userId", bind(m.UpdateOrgUserCommand{}), wrap(UpdateOrgUser))
  157. r.Delete("/users/:userId", wrap(RemoveOrgUser))
  158. r.Get("/quotas", wrap(GetOrgQuotas))
  159. r.Put("/quotas/:target", bind(m.UpdateOrgQuotaCmd{}), wrap(UpdateOrgQuota))
  160. }, reqGrafanaAdmin)
  161. // orgs (admin routes)
  162. r.Group("/orgs/name/:name", func() {
  163. r.Get("/", wrap(GetOrgByName))
  164. }, reqGrafanaAdmin)
  165. // auth api keys
  166. r.Group("/auth/keys", func() {
  167. r.Get("/", wrap(GetApiKeys))
  168. r.Post("/", quota("api_key"), bind(m.AddApiKeyCommand{}), wrap(AddApiKey))
  169. r.Delete("/:id", wrap(DeleteApiKey))
  170. }, reqOrgAdmin)
  171. // Preferences
  172. r.Group("/preferences", func() {
  173. r.Post("/set-home-dash", bind(m.SavePreferencesCommand{}), wrap(SetHomeDashboard))
  174. })
  175. // Data sources
  176. r.Group("/datasources", func() {
  177. r.Get("/", wrap(GetDataSources))
  178. r.Post("/", quota("data_source"), bind(m.AddDataSourceCommand{}), AddDataSource)
  179. r.Put("/:id", bind(m.UpdateDataSourceCommand{}), wrap(UpdateDataSource))
  180. r.Delete("/:id", DeleteDataSourceById)
  181. r.Delete("/name/:name", DeleteDataSourceByName)
  182. r.Get("/:id", wrap(GetDataSourceById))
  183. r.Get("/name/:name", wrap(GetDataSourceByName))
  184. }, reqOrgAdmin)
  185. r.Get("/datasources/id/:name", wrap(GetDataSourceIdByName), reqSignedIn)
  186. r.Get("/plugins", wrap(GetPluginList))
  187. r.Get("/plugins/:pluginId/settings", wrap(GetPluginSettingById))
  188. r.Get("/plugins/:pluginId/readme", wrap(GetPluginReadme))
  189. r.Group("/plugins", func() {
  190. r.Get("/:pluginId/dashboards/", wrap(GetPluginDashboards))
  191. r.Post("/:pluginId/settings", bind(m.UpdatePluginSettingCmd{}), wrap(UpdatePluginSetting))
  192. }, reqOrgAdmin)
  193. r.Get("/frontend/settings/", GetFrontendSettings)
  194. r.Any("/datasources/proxy/:id/*", reqSignedIn, ProxyDataSourceRequest)
  195. r.Any("/datasources/proxy/:id", reqSignedIn, ProxyDataSourceRequest)
  196. // Dashboard
  197. r.Group("/dashboards", func() {
  198. r.Combo("/db/:slug").Get(wrap(GetDashboard)).Delete(wrap(DeleteDashboard))
  199. r.Post("/db", bind(m.SaveDashboardCommand{}), wrap(PostDashboard))
  200. r.Post("/calculate-diff", bind(dtos.CalculateDiffOptions{}), wrap(CalculateDashboardDiff))
  201. r.Get("/file/:file", GetDashboardFromJsonFile)
  202. r.Get("/home", wrap(GetHomeDashboard))
  203. r.Get("/tags", GetDashboardTags)
  204. r.Post("/import", bind(dtos.ImportDashboardCommand{}), wrap(ImportDashboard))
  205. r.Group("/id/:dashboardId", func() {
  206. r.Get("/versions", wrap(GetDashboardVersions))
  207. r.Get("/versions/:id", wrap(GetDashboardVersion))
  208. r.Post("/restore", bind(dtos.RestoreDashboardVersionCommand{}), wrap(RestoreDashboardVersion))
  209. r.Group("/acl", func() {
  210. r.Get("/", wrap(GetDashboardAclList))
  211. r.Post("/", bind(m.SetDashboardAclCommand{}), wrap(PostDashboardAcl))
  212. r.Delete("/:aclId", wrap(DeleteDashboardAcl))
  213. })
  214. }, reqSignedIn)
  215. })
  216. // Dashboard snapshots
  217. r.Group("/dashboard/snapshots", func() {
  218. r.Get("/", wrap(SearchDashboardSnapshots))
  219. })
  220. // Playlist
  221. r.Group("/playlists", func() {
  222. r.Get("/", wrap(SearchPlaylists))
  223. r.Get("/:id", ValidateOrgPlaylist, wrap(GetPlaylist))
  224. r.Get("/:id/items", ValidateOrgPlaylist, wrap(GetPlaylistItems))
  225. r.Get("/:id/dashboards", ValidateOrgPlaylist, wrap(GetPlaylistDashboards))
  226. r.Delete("/:id", reqEditorRole, ValidateOrgPlaylist, wrap(DeletePlaylist))
  227. r.Put("/:id", reqEditorRole, bind(m.UpdatePlaylistCommand{}), ValidateOrgPlaylist, wrap(UpdatePlaylist))
  228. r.Post("/", reqEditorRole, bind(m.CreatePlaylistCommand{}), wrap(CreatePlaylist))
  229. })
  230. // Search
  231. r.Get("/search/", Search)
  232. // metrics
  233. r.Post("/tsdb/query", bind(dtos.MetricRequest{}), wrap(QueryMetrics))
  234. r.Get("/tsdb/testdata/scenarios", wrap(GetTestDataScenarios))
  235. r.Get("/tsdb/testdata/gensql", reqGrafanaAdmin, wrap(GenerateSqlTestData))
  236. r.Get("/tsdb/testdata/random-walk", wrap(GetTestDataRandomWalk))
  237. // metrics
  238. r.Get("/metrics", wrap(GetInternalMetrics))
  239. r.Group("/alerts", func() {
  240. r.Post("/test", bind(dtos.AlertTestCommand{}), wrap(AlertTest))
  241. r.Post("/:alertId/pause", bind(dtos.PauseAlertCommand{}), wrap(PauseAlert), reqEditorRole)
  242. r.Get("/:alertId", ValidateOrgAlert, wrap(GetAlert))
  243. r.Get("/", wrap(GetAlerts))
  244. r.Get("/states-for-dashboard", wrap(GetAlertStatesForDashboard))
  245. })
  246. r.Get("/alert-notifications", wrap(GetAlertNotifications))
  247. r.Get("/alert-notifiers", wrap(GetAlertNotifiers))
  248. r.Group("/alert-notifications", func() {
  249. r.Post("/test", bind(dtos.NotificationTestCommand{}), wrap(NotificationTest))
  250. r.Post("/", bind(m.CreateAlertNotificationCommand{}), wrap(CreateAlertNotification))
  251. r.Put("/:notificationId", bind(m.UpdateAlertNotificationCommand{}), wrap(UpdateAlertNotification))
  252. r.Get("/:notificationId", wrap(GetAlertNotificationById))
  253. r.Delete("/:notificationId", wrap(DeleteAlertNotification))
  254. }, reqEditorRole)
  255. r.Get("/annotations", wrap(GetAnnotations))
  256. r.Group("/annotations", func() {
  257. r.Post("/", bind(dtos.PostAnnotationsCmd{}), wrap(PostAnnotation))
  258. }, reqEditorRole)
  259. // error test
  260. r.Get("/metrics/error", wrap(GenerateError))
  261. }, reqSignedIn)
  262. // admin api
  263. r.Group("/api/admin", func() {
  264. r.Get("/settings", AdminGetSettings)
  265. r.Post("/users", bind(dtos.AdminCreateUserForm{}), AdminCreateUser)
  266. r.Put("/users/:id/password", bind(dtos.AdminUpdateUserPasswordForm{}), AdminUpdateUserPassword)
  267. r.Put("/users/:id/permissions", bind(dtos.AdminUpdateUserPermissionsForm{}), AdminUpdateUserPermissions)
  268. r.Delete("/users/:id", AdminDeleteUser)
  269. r.Get("/users/:id/quotas", wrap(GetUserQuotas))
  270. r.Put("/users/:id/quotas/:target", bind(m.UpdateUserQuotaCmd{}), wrap(UpdateUserQuota))
  271. r.Get("/stats", AdminGetStats)
  272. r.Post("/pause-all-alerts", bind(dtos.PauseAllAlertsCommand{}), wrap(PauseAllAlerts))
  273. }, reqGrafanaAdmin)
  274. // rendering
  275. r.Get("/render/*", reqSignedIn, RenderToPng)
  276. // grafana.net proxy
  277. r.Any("/api/gnet/*", reqSignedIn, ProxyGnetRequest)
  278. // Gravatar service.
  279. avt := avatar.CacheServer()
  280. r.Get("/avatar/:hash", avt.ServeHTTP)
  281. // Websocket
  282. r.Any("/ws", hs.streamManager.Serve)
  283. // streams
  284. //r.Post("/api/streams/push", reqSignedIn, bind(dtos.StreamMessage{}), liveConn.PushToStream)
  285. InitAppPluginRoutes(r)
  286. r.NotFound(NotFoundHandler)
  287. }