2014-12-15 14:25:02 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/Unknwon/macaron"
|
2015-01-16 04:54:19 -06:00
|
|
|
"github.com/macaron-contrib/binding"
|
2014-12-15 14:25:02 -06:00
|
|
|
"github.com/torkelo/grafana-pro/pkg/api/dtos"
|
|
|
|
"github.com/torkelo/grafana-pro/pkg/middleware"
|
2015-01-16 04:54:19 -06:00
|
|
|
m "github.com/torkelo/grafana-pro/pkg/models"
|
2015-01-04 14:03:40 -06:00
|
|
|
"github.com/torkelo/grafana-pro/pkg/setting"
|
2014-12-15 14:25:02 -06:00
|
|
|
)
|
|
|
|
|
2015-01-14 07:25:12 -06:00
|
|
|
// Register adds http routes
|
2015-01-16 04:54:19 -06:00
|
|
|
func Register(r *macaron.Macaron) {
|
2015-01-15 05:16:54 -06:00
|
|
|
reqSignedIn := middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true})
|
2015-01-16 07:32:18 -06:00
|
|
|
reqGrafanaAdmin := middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true, ReqGrafanaAdmin: true})
|
2015-01-19 11:01:04 -06:00
|
|
|
reqEditorRole := middleware.RoleAuth(m.ROLE_EDITOR, m.ROLE_ADMIN)
|
2015-01-26 13:26:17 -06:00
|
|
|
reqAccountAdmin := middleware.RoleAuth(m.ROLE_ADMIN)
|
2015-01-16 04:54:19 -06:00
|
|
|
bind := binding.Bind
|
2014-12-15 14:25:02 -06:00
|
|
|
|
2015-01-14 07:25:12 -06:00
|
|
|
// not logged in views
|
2015-01-16 04:54:19 -06:00
|
|
|
r.Get("/", reqSignedIn, Index)
|
|
|
|
r.Post("/logout", LogoutPost)
|
2015-01-21 02:52:40 -06:00
|
|
|
r.Post("/login", bind(dtos.LoginCommand{}), LoginPost)
|
2015-01-16 04:54:19 -06:00
|
|
|
r.Get("/login/:name", OAuthLogin)
|
2015-01-27 03:09:54 -06:00
|
|
|
r.Get("/login", LoginView)
|
2014-12-15 14:25:02 -06:00
|
|
|
|
2015-01-14 07:25:12 -06:00
|
|
|
// authed views
|
2015-01-19 12:10:29 -06:00
|
|
|
r.Get("/profile/", reqSignedIn, Index)
|
2015-01-16 04:54:19 -06:00
|
|
|
r.Get("/account/", reqSignedIn, Index)
|
|
|
|
r.Get("/account/datasources/", reqSignedIn, Index)
|
2015-01-19 12:10:29 -06:00
|
|
|
r.Get("/account/users/", reqSignedIn, Index)
|
2015-01-16 05:28:35 -06:00
|
|
|
r.Get("/account/apikeys/", reqSignedIn, Index)
|
2015-01-18 01:56:19 -06:00
|
|
|
r.Get("/account/import/", reqSignedIn, Index)
|
2015-01-19 12:10:29 -06:00
|
|
|
r.Get("/admin/users", reqSignedIn, Index)
|
2015-01-16 04:54:19 -06:00
|
|
|
r.Get("/dashboard/*", reqSignedIn, Index)
|
2014-12-16 05:04:08 -06:00
|
|
|
|
2015-01-14 07:25:12 -06:00
|
|
|
// sign up
|
2015-01-16 04:54:19 -06:00
|
|
|
r.Get("/signup", Index)
|
2015-01-26 05:58:03 -06:00
|
|
|
r.Post("/api/user/signup", bind(m.CreateUserCommand{}), SignUp)
|
2014-12-15 14:25:02 -06:00
|
|
|
|
2015-01-14 07:25:12 -06:00
|
|
|
// authed api
|
2015-01-16 04:54:19 -06:00
|
|
|
r.Group("/api", func() {
|
2015-01-19 11:01:04 -06:00
|
|
|
// user
|
|
|
|
r.Group("/user", func() {
|
|
|
|
r.Get("/", GetUser)
|
2015-01-26 05:58:03 -06:00
|
|
|
r.Put("/", bind(m.UpdateUserCommand{}), UpdateUser)
|
2015-01-19 11:01:04 -06:00
|
|
|
r.Post("/using/:id", SetUsingAccount)
|
|
|
|
r.Get("/accounts", GetUserAccounts)
|
|
|
|
})
|
|
|
|
|
2015-01-14 07:25:12 -06:00
|
|
|
// account
|
2015-01-16 04:54:19 -06:00
|
|
|
r.Group("/account", func() {
|
2015-01-26 08:42:22 -06:00
|
|
|
r.Get("/", GetAccount)
|
2015-01-26 05:58:03 -06:00
|
|
|
r.Post("/", bind(m.CreateAccountCommand{}), CreateAccount)
|
2015-01-26 08:42:22 -06:00
|
|
|
r.Put("/", bind(m.UpdateAccountCommand{}), UpdateAccount)
|
2015-01-26 05:58:03 -06:00
|
|
|
r.Post("/users", bind(m.AddAccountUserCommand{}), AddAccountUser)
|
2015-01-19 11:01:04 -06:00
|
|
|
r.Get("/users", GetAccountUsers)
|
|
|
|
r.Delete("/users/:id", RemoveAccountUser)
|
2015-01-26 13:26:17 -06:00
|
|
|
}, reqAccountAdmin)
|
|
|
|
|
2015-01-27 01:26:11 -06:00
|
|
|
// auth api keys
|
|
|
|
r.Group("/auth/keys", func() {
|
2015-01-16 05:06:24 -06:00
|
|
|
r.Combo("/").
|
2015-01-27 01:26:11 -06:00
|
|
|
Get(GetApiKeys).
|
|
|
|
Post(bind(m.AddApiKeyCommand{}), AddApiKey).
|
|
|
|
Put(bind(m.UpdateApiKeyCommand{}), UpdateApiKey)
|
|
|
|
r.Delete("/:id", DeleteApiKey)
|
2015-01-26 13:26:17 -06:00
|
|
|
}, reqAccountAdmin)
|
|
|
|
|
2015-01-14 07:25:12 -06:00
|
|
|
// Data sources
|
2015-01-16 04:54:19 -06:00
|
|
|
r.Group("/datasources", func() {
|
|
|
|
r.Combo("/").Get(GetDataSources).Put(AddDataSource).Post(UpdateDataSource)
|
|
|
|
r.Delete("/:id", DeleteDataSource)
|
|
|
|
r.Any("/proxy/:id/*", reqSignedIn, ProxyDataSourceRequest)
|
2015-01-26 13:26:17 -06:00
|
|
|
}, reqAccountAdmin)
|
|
|
|
|
2015-01-14 07:25:12 -06:00
|
|
|
// Dashboard
|
2015-01-16 04:54:19 -06:00
|
|
|
r.Group("/dashboard", func() {
|
|
|
|
r.Combo("/:slug").Get(GetDashboard).Delete(DeleteDashboard)
|
2015-01-16 08:28:44 -06:00
|
|
|
r.Post("/", reqEditorRole, bind(m.SaveDashboardCommand{}), PostDashboard)
|
2015-01-14 07:25:12 -06:00
|
|
|
})
|
2015-01-26 13:26:17 -06:00
|
|
|
|
2015-01-14 07:25:12 -06:00
|
|
|
// Search
|
2015-01-16 04:54:19 -06:00
|
|
|
r.Get("/search/", Search)
|
2015-01-26 13:26:17 -06:00
|
|
|
|
2015-01-14 07:25:12 -06:00
|
|
|
// metrics
|
2015-01-16 04:54:19 -06:00
|
|
|
r.Get("/metrics/test", GetTestMetrics)
|
2015-01-15 05:16:54 -06:00
|
|
|
}, reqSignedIn)
|
|
|
|
|
|
|
|
// admin api
|
2015-01-16 04:54:19 -06:00
|
|
|
r.Group("/api/admin", func() {
|
2015-01-19 11:01:04 -06:00
|
|
|
r.Get("/users", AdminSearchUsers)
|
2015-01-16 07:32:18 -06:00
|
|
|
}, reqGrafanaAdmin)
|
2014-12-15 14:25:02 -06:00
|
|
|
|
|
|
|
// rendering
|
2015-01-16 04:54:19 -06:00
|
|
|
r.Get("/render/*", reqSignedIn, RenderToPng)
|
2015-01-06 02:11:00 -06:00
|
|
|
|
2015-01-16 04:54:19 -06:00
|
|
|
r.NotFound(NotFound)
|
2014-12-15 14:25:02 -06:00
|
|
|
}
|
|
|
|
|
2015-01-08 02:23:34 -06:00
|
|
|
func setIndexViewData(c *middleware.Context) error {
|
|
|
|
settings, err := getFrontendSettings(c)
|
2014-12-28 11:37:39 -06:00
|
|
|
if err != nil {
|
2015-01-08 02:23:34 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-01-16 07:32:18 -06:00
|
|
|
currentUser := &dtos.CurrentUser{}
|
|
|
|
|
|
|
|
if c.IsSignedIn {
|
|
|
|
currentUser = &dtos.CurrentUser{
|
2015-01-19 11:01:04 -06:00
|
|
|
Login: c.Login,
|
|
|
|
Email: c.Email,
|
|
|
|
Name: c.Name,
|
|
|
|
UsingAccountName: c.AccountName,
|
|
|
|
GravatarUrl: dtos.GetGravatarUrl(c.Email),
|
2015-01-16 07:32:18 -06:00
|
|
|
IsGrafanaAdmin: c.IsGrafanaAdmin,
|
2015-01-19 11:01:04 -06:00
|
|
|
Role: c.AccountRole,
|
2015-01-16 07:32:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Data["User"] = currentUser
|
2015-01-08 02:23:34 -06:00
|
|
|
c.Data["Settings"] = settings
|
|
|
|
c.Data["AppUrl"] = setting.AppUrl
|
|
|
|
c.Data["AppSubUrl"] = setting.AppSubUrl
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Index(c *middleware.Context) {
|
|
|
|
if err := setIndexViewData(c); err != nil {
|
|
|
|
c.Handle(500, "Failed to get settings", err)
|
2014-12-28 11:37:39 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-08 02:23:34 -06:00
|
|
|
c.HTML(200, "index")
|
2014-12-15 14:25:02 -06:00
|
|
|
}
|
|
|
|
|
2015-01-08 02:23:34 -06:00
|
|
|
func NotFound(c *middleware.Context) {
|
2015-01-14 07:25:12 -06:00
|
|
|
if c.IsApiRequest() {
|
|
|
|
c.JsonApiErr(200, "Not found", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-08 02:23:34 -06:00
|
|
|
if err := setIndexViewData(c); err != nil {
|
|
|
|
c.Handle(500, "Failed to get settings", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.HTML(404, "index")
|
2014-12-15 14:25:02 -06:00
|
|
|
}
|