| 
									
										
										
										
											2014-12-16 04:25:02 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"github.com/Unknwon/macaron" | 
					
						
							| 
									
										
										
										
											2015-02-05 17:37:13 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/dtos" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/middleware" | 
					
						
							|  |  |  | 	m "github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2015-02-09 20:30:04 +08:00
										 |  |  | 	"github.com/macaron-contrib/binding" | 
					
						
							| 
									
										
										
										
											2014-12-16 04:25:02 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-14 21:25:12 +08:00
										 |  |  | // Register adds http routes
 | 
					
						
							| 
									
										
										
										
											2015-01-16 18:54:19 +08:00
										 |  |  | func Register(r *macaron.Macaron) { | 
					
						
							| 
									
										
										
										
											2015-01-15 19:16:54 +08:00
										 |  |  | 	reqSignedIn := middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true}) | 
					
						
							| 
									
										
										
										
											2015-01-16 21:32:18 +08:00
										 |  |  | 	reqGrafanaAdmin := middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true, ReqGrafanaAdmin: true}) | 
					
						
							| 
									
										
										
										
											2015-01-20 01:01:04 +08:00
										 |  |  | 	reqEditorRole := middleware.RoleAuth(m.ROLE_EDITOR, m.ROLE_ADMIN) | 
					
						
							| 
									
										
										
										
											2015-01-27 03:26:17 +08:00
										 |  |  | 	reqAccountAdmin := middleware.RoleAuth(m.ROLE_ADMIN) | 
					
						
							| 
									
										
										
										
											2015-01-16 18:54:19 +08:00
										 |  |  | 	bind := binding.Bind | 
					
						
							| 
									
										
										
										
											2014-12-16 04:25:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-14 21:25:12 +08:00
										 |  |  | 	// not logged in views
 | 
					
						
							| 
									
										
										
										
											2015-01-16 18:54:19 +08:00
										 |  |  | 	r.Get("/", reqSignedIn, Index) | 
					
						
							| 
									
										
										
										
											2015-01-29 22:46:54 +08:00
										 |  |  | 	r.Get("/logout", Logout) | 
					
						
							| 
									
										
										
										
											2015-01-21 16:52:40 +08:00
										 |  |  | 	r.Post("/login", bind(dtos.LoginCommand{}), LoginPost) | 
					
						
							| 
									
										
										
										
											2015-01-16 18:54:19 +08:00
										 |  |  | 	r.Get("/login/:name", OAuthLogin) | 
					
						
							| 
									
										
										
										
											2015-01-27 17:09:54 +08:00
										 |  |  | 	r.Get("/login", LoginView) | 
					
						
							| 
									
										
										
										
											2014-12-16 04:25:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-14 21:25:12 +08:00
										 |  |  | 	// authed views
 | 
					
						
							| 
									
										
										
										
											2015-01-20 02:10:29 +08:00
										 |  |  | 	r.Get("/profile/", reqSignedIn, Index) | 
					
						
							| 
									
										
										
										
											2015-02-25 21:27:34 +08:00
										 |  |  | 	r.Get("/org/", reqSignedIn, Index) | 
					
						
							|  |  |  | 	r.Get("/datasources/", reqSignedIn, Index) | 
					
						
							| 
									
										
										
										
											2015-02-28 15:25:13 +08:00
										 |  |  | 	r.Get("/datasources/edit/*", reqSignedIn, Index) | 
					
						
							| 
									
										
										
										
											2015-02-25 21:27:34 +08:00
										 |  |  | 	r.Get("/org/users/", reqSignedIn, Index) | 
					
						
							|  |  |  | 	r.Get("/org/apikeys/", reqSignedIn, Index) | 
					
						
							|  |  |  | 	r.Get("/dashboard/import/", reqSignedIn, Index) | 
					
						
							| 
									
										
										
										
											2015-02-12 22:46:14 +08:00
										 |  |  | 	r.Get("/admin/settings", reqGrafanaAdmin, Index) | 
					
						
							| 
									
										
										
										
											2015-01-28 18:33:50 +08:00
										 |  |  | 	r.Get("/admin/users", reqGrafanaAdmin, Index) | 
					
						
							| 
									
										
										
										
											2015-02-10 23:26:23 +08:00
										 |  |  | 	r.Get("/admin/users/create", reqGrafanaAdmin, Index) | 
					
						
							|  |  |  | 	r.Get("/admin/users/edit/:id", reqGrafanaAdmin, Index) | 
					
						
							| 
									
										
										
										
											2015-01-16 18:54:19 +08:00
										 |  |  | 	r.Get("/dashboard/*", reqSignedIn, Index) | 
					
						
							| 
									
										
										
										
											2014-12-16 19:04:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-14 21:25:12 +08:00
										 |  |  | 	// sign up
 | 
					
						
							| 
									
										
										
										
											2015-01-16 18:54:19 +08:00
										 |  |  | 	r.Get("/signup", Index) | 
					
						
							| 
									
										
										
										
											2015-01-26 19:58:03 +08:00
										 |  |  | 	r.Post("/api/user/signup", bind(m.CreateUserCommand{}), SignUp) | 
					
						
							| 
									
										
										
										
											2014-12-16 04:25:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-21 20:53:16 +08:00
										 |  |  | 	// dashboard snapshots
 | 
					
						
							|  |  |  | 	r.Post("/api/snapshots/", bind(m.CreateDashboardSnapshotCommand{}), CreateDashboardSnapshot) | 
					
						
							| 
									
										
										
										
											2015-03-24 01:58:30 +08:00
										 |  |  | 	r.Get("/dashboard/snapshots/*", Index) | 
					
						
							| 
									
										
										
										
											2015-03-21 20:53:16 +08:00
										 |  |  | 	r.Get("/api/snapshots/:key", GetDashboardSnapshot) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-14 21:25:12 +08:00
										 |  |  | 	// authed api
 | 
					
						
							| 
									
										
										
										
											2015-01-16 18:54:19 +08:00
										 |  |  | 	r.Group("/api", func() { | 
					
						
							| 
									
										
										
										
											2015-01-20 01:01:04 +08:00
										 |  |  | 		// user
 | 
					
						
							|  |  |  | 		r.Group("/user", func() { | 
					
						
							|  |  |  | 			r.Get("/", GetUser) | 
					
						
							| 
									
										
										
										
											2015-01-26 19:58:03 +08:00
										 |  |  | 			r.Put("/", bind(m.UpdateUserCommand{}), UpdateUser) | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 			r.Post("/using/:id", UserSetUsingOrg) | 
					
						
							|  |  |  | 			r.Get("/orgs", GetUserOrgList) | 
					
						
							| 
									
										
										
										
											2015-02-02 18:32:32 +08:00
										 |  |  | 			r.Post("/stars/dashboard/:id", StarDashboard) | 
					
						
							|  |  |  | 			r.Delete("/stars/dashboard/:id", UnstarDashboard) | 
					
						
							| 
									
										
										
										
											2015-02-19 23:09:49 +08:00
										 |  |  | 			r.Put("/password", bind(m.ChangeUserPasswordCommand{}), ChangeUserPassword) | 
					
						
							| 
									
										
										
										
											2015-01-20 01:01:04 +08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-14 21:25:12 +08:00
										 |  |  | 		// account
 | 
					
						
							| 
									
										
										
										
											2015-02-24 01:29:01 +08:00
										 |  |  | 		r.Group("/org", func() { | 
					
						
							|  |  |  | 			r.Get("/", GetOrg) | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 			r.Post("/", bind(m.CreateOrgCommand{}), CreateOrg) | 
					
						
							|  |  |  | 			r.Put("/", bind(m.UpdateOrgCommand{}), UpdateOrg) | 
					
						
							|  |  |  | 			r.Post("/users", bind(m.AddOrgUserCommand{}), AddOrgUser) | 
					
						
							| 
									
										
										
										
											2015-02-24 01:29:01 +08:00
										 |  |  | 			r.Get("/users", GetOrgUsers) | 
					
						
							|  |  |  | 			r.Delete("/users/:id", RemoveOrgUser) | 
					
						
							| 
									
										
										
										
											2015-01-27 03:26:17 +08:00
										 |  |  | 		}, reqAccountAdmin) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | 		// auth api keys
 | 
					
						
							|  |  |  | 		r.Group("/auth/keys", func() { | 
					
						
							| 
									
										
										
										
											2015-02-09 20:30:04 +08:00
										 |  |  | 			r.Get("/", GetApiKeys) | 
					
						
							|  |  |  | 			r.Post("/", bind(m.AddApiKeyCommand{}), AddApiKey) | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | 			r.Delete("/:id", DeleteApiKey) | 
					
						
							| 
									
										
										
										
											2015-01-27 03:26:17 +08:00
										 |  |  | 		}, reqAccountAdmin) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-14 21:25:12 +08:00
										 |  |  | 		// Data sources
 | 
					
						
							| 
									
										
										
										
											2015-01-16 18:54:19 +08:00
										 |  |  | 		r.Group("/datasources", func() { | 
					
						
							| 
									
										
										
										
											2015-03-02 16:58:35 +08:00
										 |  |  | 			r.Combo("/"). | 
					
						
							|  |  |  | 				Get(GetDataSources). | 
					
						
							|  |  |  | 				Put(bind(m.AddDataSourceCommand{}), AddDataSource). | 
					
						
							|  |  |  | 				Post(bind(m.UpdateDataSourceCommand{}), UpdateDataSource) | 
					
						
							| 
									
										
										
										
											2015-01-16 18:54:19 +08:00
										 |  |  | 			r.Delete("/:id", DeleteDataSource) | 
					
						
							| 
									
										
										
										
											2015-02-14 17:04:27 +08:00
										 |  |  | 			r.Get("/:id", GetDataSourceById) | 
					
						
							| 
									
										
										
										
											2015-02-28 15:25:13 +08:00
										 |  |  | 			r.Get("/plugins", GetDataSourcePlugins) | 
					
						
							| 
									
										
										
										
											2015-01-27 03:26:17 +08:00
										 |  |  | 		}, reqAccountAdmin) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-18 21:06:44 +08:00
										 |  |  | 		r.Get("/frontend/settings/", GetFrontendSettings) | 
					
						
							| 
									
										
										
										
											2015-02-10 17:19:43 +08:00
										 |  |  | 		r.Any("/datasources/proxy/:id/*", reqSignedIn, ProxyDataSourceRequest) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-14 21:25:12 +08:00
										 |  |  | 		// Dashboard
 | 
					
						
							| 
									
										
										
										
											2015-02-03 22:04:35 +08:00
										 |  |  | 		r.Group("/dashboards", func() { | 
					
						
							|  |  |  | 			r.Combo("/db/:slug").Get(GetDashboard).Delete(DeleteDashboard) | 
					
						
							|  |  |  | 			r.Post("/db", reqEditorRole, bind(m.SaveDashboardCommand{}), PostDashboard) | 
					
						
							|  |  |  | 			r.Get("/home", GetHomeDashboard) | 
					
						
							| 
									
										
										
										
											2015-01-14 21:25:12 +08:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2015-01-27 03:26:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-14 21:25:12 +08:00
										 |  |  | 		// Search
 | 
					
						
							| 
									
										
										
										
											2015-01-16 18:54:19 +08:00
										 |  |  | 		r.Get("/search/", Search) | 
					
						
							| 
									
										
										
										
											2015-01-27 03:26:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-14 21:25:12 +08:00
										 |  |  | 		// metrics
 | 
					
						
							| 
									
										
										
										
											2015-01-16 18:54:19 +08:00
										 |  |  | 		r.Get("/metrics/test", GetTestMetrics) | 
					
						
							| 
									
										
										
										
											2015-01-15 19:16:54 +08:00
										 |  |  | 	}, reqSignedIn) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// admin api
 | 
					
						
							| 
									
										
										
										
											2015-01-16 18:54:19 +08:00
										 |  |  | 	r.Group("/api/admin", func() { | 
					
						
							| 
									
										
										
										
											2015-02-12 22:46:14 +08:00
										 |  |  | 		r.Get("/settings", AdminGetSettings) | 
					
						
							| 
									
										
										
										
											2015-01-20 01:01:04 +08:00
										 |  |  | 		r.Get("/users", AdminSearchUsers) | 
					
						
							| 
									
										
										
										
											2015-02-10 22:36:51 +08:00
										 |  |  | 		r.Get("/users/:id", AdminGetUser) | 
					
						
							|  |  |  | 		r.Post("/users", bind(dtos.AdminCreateUserForm{}), AdminCreateUser) | 
					
						
							| 
									
										
										
										
											2015-02-23 18:24:22 +08:00
										 |  |  | 		r.Put("/users/:id/details", bind(dtos.AdminUpdateUserForm{}), AdminUpdateUser) | 
					
						
							|  |  |  | 		r.Put("/users/:id/password", bind(dtos.AdminUpdateUserPasswordForm{}), AdminUpdateUserPassword) | 
					
						
							| 
									
										
										
										
											2015-02-26 22:43:48 +08:00
										 |  |  | 		r.Put("/users/:id/permissions", bind(dtos.AdminUpdateUserPermissionsForm{}), AdminUpdateUserPermissions) | 
					
						
							| 
									
										
										
										
											2015-02-11 23:47:22 +08:00
										 |  |  | 		r.Delete("/users/:id", AdminDeleteUser) | 
					
						
							| 
									
										
										
										
											2015-01-16 21:32:18 +08:00
										 |  |  | 	}, reqGrafanaAdmin) | 
					
						
							| 
									
										
										
										
											2014-12-16 04:25:02 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// rendering
 | 
					
						
							| 
									
										
										
										
											2015-01-16 18:54:19 +08:00
										 |  |  | 	r.Get("/render/*", reqSignedIn, RenderToPng) | 
					
						
							| 
									
										
										
										
											2015-01-06 16:11:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-16 18:54:19 +08:00
										 |  |  | 	r.NotFound(NotFound) | 
					
						
							| 
									
										
										
										
											2014-12-16 04:25:02 +08:00
										 |  |  | } |