The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
grafana/pkg/api/api.go

143 lines
3.6 KiB

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"
"github.com/torkelo/grafana-pro/pkg/setting"
)
// 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)
bind := binding.Bind
// not logged in views
r.Get("/", reqSignedIn, Index)
r.Post("/logout", LogoutPost)
r.Post("/login", LoginPost)
r.Get("/login/:name", OAuthLogin)
r.Get("/login", Index)
// authed views
10 years ago
r.Get("/profile/", reqSignedIn, Index)
r.Get("/account/", reqSignedIn, Index)
r.Get("/account/datasources/", reqSignedIn, Index)
10 years ago
r.Get("/account/users/", reqSignedIn, Index)
r.Get("/account/apikeys/", reqSignedIn, Index)
r.Get("/account/import/", reqSignedIn, Index)
10 years ago
r.Get("/admin/users", reqSignedIn, Index)
r.Get("/dashboard/*", reqSignedIn, Index)
// sign up
r.Get("/signup", Index)
r.Post("/api/account/signup", SignUp)
// authed api
r.Group("/api", func() {
// user
r.Group("/user", func() {
r.Get("/", GetUser)
r.Post("/", bind(m.UpdateUserCommand{}), UpdateUser)
r.Post("/using/:id", SetUsingAccount)
r.Get("/accounts", GetUserAccounts)
})
// account
r.Group("/account", func() {
r.Put("/", bind(m.CreateAccountCommand{}), CreateAccount)
r.Put("/users", bind(m.AddAccountUserCommand{}), AddAccountUser)
r.Get("/users", GetAccountUsers)
r.Delete("/users/:id", RemoveAccountUser)
})
// Token
r.Group("/tokens", func() {
r.Combo("/").
Get(GetTokens).
Put(bind(m.AddTokenCommand{}), AddToken).
Post(bind(m.UpdateTokenCommand{}), UpdateToken)
r.Delete("/:id", DeleteToken)
})
// Data sources
r.Group("/datasources", func() {
r.Combo("/").Get(GetDataSources).Put(AddDataSource).Post(UpdateDataSource)
r.Delete("/:id", DeleteDataSource)
r.Any("/proxy/:id/*", reqSignedIn, ProxyDataSourceRequest)
})
// Dashboard
r.Group("/dashboard", func() {
r.Combo("/:slug").Get(GetDashboard).Delete(DeleteDashboard)
r.Post("/", reqEditorRole, bind(m.SaveDashboardCommand{}), PostDashboard)
})
// Search
r.Get("/search/", Search)
// 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) {
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")
}