2014-12-16 04:25:02 +08:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/Unknwon/macaron"
|
|
|
|
"github.com/torkelo/grafana-pro/pkg/api/dtos"
|
|
|
|
"github.com/torkelo/grafana-pro/pkg/middleware"
|
2015-01-05 04:03:40 +08:00
|
|
|
"github.com/torkelo/grafana-pro/pkg/setting"
|
2014-12-16 04:25:02 +08:00
|
|
|
)
|
|
|
|
|
2015-01-14 21:25:12 +08:00
|
|
|
// Register adds http routes
|
2014-12-16 04:25:02 +08:00
|
|
|
func Register(m *macaron.Macaron) {
|
|
|
|
auth := middleware.Auth()
|
|
|
|
|
2015-01-14 21:25:12 +08:00
|
|
|
// not logged in views
|
2014-12-16 04:25:02 +08:00
|
|
|
m.Get("/", auth, Index)
|
|
|
|
m.Post("/logout", LogoutPost)
|
|
|
|
m.Post("/login", LoginPost)
|
|
|
|
m.Get("/login/:name", OAuthLogin)
|
2015-01-09 00:34:41 +08:00
|
|
|
m.Get("/login", Index)
|
2014-12-16 04:25:02 +08:00
|
|
|
|
2015-01-14 21:25:12 +08:00
|
|
|
// authed views
|
2014-12-16 04:25:02 +08:00
|
|
|
m.Get("/account/", auth, Index)
|
2015-01-14 21:25:12 +08:00
|
|
|
m.Get("/account/datasources/", auth, Index)
|
2015-01-11 19:12:29 +08:00
|
|
|
m.Get("/admin", auth, Index)
|
2015-01-14 21:25:12 +08:00
|
|
|
m.Get("/dashboard/*", auth, Index)
|
2014-12-16 19:04:08 +08:00
|
|
|
|
2015-01-14 21:25:12 +08:00
|
|
|
// sign up
|
2015-01-14 17:19:45 +08:00
|
|
|
m.Get("/signup", Index)
|
|
|
|
m.Post("/api/account/signup", SignUp)
|
2014-12-16 04:25:02 +08:00
|
|
|
|
2015-01-14 21:25:12 +08:00
|
|
|
// authed api
|
|
|
|
m.Group("/api", func() {
|
|
|
|
// account
|
|
|
|
m.Group("/account", func() {
|
|
|
|
m.Get("/", GetAccount)
|
|
|
|
m.Post("/collaborators/add", AddCollaborator)
|
|
|
|
m.Post("/using/:id", SetUsingAccount)
|
|
|
|
m.Get("/others", GetOtherAccounts)
|
|
|
|
})
|
|
|
|
// Token
|
|
|
|
m.Group("/tokens", func() {
|
|
|
|
m.Combo("/").Get(GetTokens).Put(AddToken).Post(UpdateToken)
|
|
|
|
m.Delete("/:id", DeleteToken)
|
|
|
|
})
|
|
|
|
// Data sources
|
|
|
|
m.Group("/datasources", func() {
|
|
|
|
m.Combo("/").Get(GetDataSources).Put(AddDataSource).Post(UpdateDataSource)
|
|
|
|
m.Delete("/:id", DeleteDataSource)
|
|
|
|
m.Any("/proxy/:id/*", auth, ProxyDataSourceRequest)
|
|
|
|
})
|
|
|
|
// Dashboard
|
|
|
|
m.Group("/dashboard", func() {
|
|
|
|
m.Combo("/:slug").Get(GetDashboard).Delete(DeleteDashboard)
|
|
|
|
m.Post("/", PostDashboard)
|
|
|
|
})
|
|
|
|
// Search
|
|
|
|
m.Get("/search/", Search)
|
|
|
|
// metrics
|
|
|
|
m.Get("/metrics/test", auth, GetTestMetrics)
|
|
|
|
}, auth)
|
2014-12-16 04:25:02 +08:00
|
|
|
|
|
|
|
// rendering
|
|
|
|
m.Get("/render/*", auth, RenderToPng)
|
2015-01-06 16:11:00 +08:00
|
|
|
|
2015-01-08 16:23:34 +08:00
|
|
|
m.NotFound(NotFound)
|
2014-12-16 04:25:02 +08:00
|
|
|
}
|
|
|
|
|
2015-01-08 16:23:34 +08:00
|
|
|
func setIndexViewData(c *middleware.Context) error {
|
|
|
|
settings, err := getFrontendSettings(c)
|
2014-12-29 01:37:39 +08:00
|
|
|
if err != nil {
|
2015-01-08 16:23:34 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Data["User"] = dtos.NewCurrentUser(c.UserAccount)
|
|
|
|
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-29 01:37:39 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-08 16:23:34 +08:00
|
|
|
c.HTML(200, "index")
|
2014-12-16 04:25:02 +08:00
|
|
|
}
|
|
|
|
|
2015-01-08 16:23:34 +08:00
|
|
|
func NotFound(c *middleware.Context) {
|
2015-01-14 21:25:12 +08:00
|
|
|
if c.IsApiRequest() {
|
|
|
|
c.JsonApiErr(200, "Not found", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-08 16:23:34 +08:00
|
|
|
if err := setIndexViewData(c); err != nil {
|
|
|
|
c.Handle(500, "Failed to get settings", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.HTML(404, "index")
|
2014-12-16 04:25:02 +08:00
|
|
|
}
|