grafana/pkg/api/api.go

151 lines
3.9 KiB
Go
Raw Normal View History

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