api.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package api
  2. import (
  3. "github.com/Unknwon/macaron"
  4. "github.com/macaron-contrib/binding"
  5. "github.com/torkelo/grafana-pro/pkg/api/dtos"
  6. "github.com/torkelo/grafana-pro/pkg/middleware"
  7. m "github.com/torkelo/grafana-pro/pkg/models"
  8. "github.com/torkelo/grafana-pro/pkg/setting"
  9. )
  10. // Register adds http routes
  11. func Register(r *macaron.Macaron) {
  12. reqSignedIn := middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true})
  13. reqGrafanaAdmin := middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true, ReqGrafanaAdmin: true})
  14. reqEditorRole := middleware.RoleAuth(m.ROLE_EDITOR, m.ROLE_OWNER)
  15. bind := binding.Bind
  16. // not logged in views
  17. r.Get("/", reqSignedIn, Index)
  18. r.Post("/logout", LogoutPost)
  19. r.Post("/login", LoginPost)
  20. r.Get("/login/:name", OAuthLogin)
  21. r.Get("/login", Index)
  22. // authed views
  23. r.Get("/account/", reqSignedIn, Index)
  24. r.Get("/account/datasources/", reqSignedIn, Index)
  25. r.Get("/account/collaborators/", reqSignedIn, Index)
  26. r.Get("/account/apikeys/", reqSignedIn, Index)
  27. r.Get("/account/import/", reqSignedIn, Index)
  28. r.Get("/admin", reqSignedIn, Index)
  29. r.Get("/dashboard/*", reqSignedIn, Index)
  30. // sign up
  31. r.Get("/signup", Index)
  32. r.Post("/api/account/signup", SignUp)
  33. // authed api
  34. r.Group("/api", func() {
  35. // account
  36. r.Group("/account", func() {
  37. r.Get("/", GetAccount)
  38. r.Post("/", bind(m.UpdateAccountCommand{}), UpdateAccount)
  39. r.Put("/collaborators", bind(m.AddCollaboratorCommand{}), AddCollaborator)
  40. r.Get("/collaborators", GetCollaborators)
  41. r.Delete("/collaborators/:id", RemoveCollaborator)
  42. r.Post("/using/:id", SetUsingAccount)
  43. r.Get("/others", GetOtherAccounts)
  44. })
  45. // Token
  46. r.Group("/tokens", func() {
  47. r.Combo("/").
  48. Get(GetTokens).
  49. Put(bind(m.AddTokenCommand{}), AddToken).
  50. Post(bind(m.UpdateTokenCommand{}), UpdateToken)
  51. r.Delete("/:id", DeleteToken)
  52. })
  53. // Data sources
  54. r.Group("/datasources", func() {
  55. r.Combo("/").Get(GetDataSources).Put(AddDataSource).Post(UpdateDataSource)
  56. r.Delete("/:id", DeleteDataSource)
  57. r.Any("/proxy/:id/*", reqSignedIn, ProxyDataSourceRequest)
  58. })
  59. // Dashboard
  60. r.Group("/dashboard", func() {
  61. r.Combo("/:slug").Get(GetDashboard).Delete(DeleteDashboard)
  62. r.Post("/", reqEditorRole, bind(m.SaveDashboardCommand{}), PostDashboard)
  63. })
  64. // Search
  65. r.Get("/search/", Search)
  66. // metrics
  67. r.Get("/metrics/test", GetTestMetrics)
  68. }, reqSignedIn)
  69. // admin api
  70. r.Group("/api/admin", func() {
  71. r.Get("/accounts", AdminSearchAccounts)
  72. }, reqGrafanaAdmin)
  73. // rendering
  74. r.Get("/render/*", reqSignedIn, RenderToPng)
  75. r.NotFound(NotFound)
  76. }
  77. func setIndexViewData(c *middleware.Context) error {
  78. settings, err := getFrontendSettings(c)
  79. if err != nil {
  80. return err
  81. }
  82. currentUser := &dtos.CurrentUser{}
  83. if c.IsSignedIn {
  84. currentUser = &dtos.CurrentUser{
  85. Login: c.UserLogin,
  86. Email: c.UserEmail,
  87. Name: c.UserName,
  88. UsingAccountName: c.UsingAccountName,
  89. GravatarUrl: dtos.GetGravatarUrl(c.UserEmail),
  90. IsGrafanaAdmin: c.IsGrafanaAdmin,
  91. Role: c.UserRole,
  92. }
  93. }
  94. c.Data["User"] = currentUser
  95. c.Data["Settings"] = settings
  96. c.Data["AppUrl"] = setting.AppUrl
  97. c.Data["AppSubUrl"] = setting.AppSubUrl
  98. return nil
  99. }
  100. func Index(c *middleware.Context) {
  101. if err := setIndexViewData(c); err != nil {
  102. c.Handle(500, "Failed to get settings", err)
  103. return
  104. }
  105. c.HTML(200, "index")
  106. }
  107. func NotFound(c *middleware.Context) {
  108. if c.IsApiRequest() {
  109. c.JsonApiErr(200, "Not found", nil)
  110. return
  111. }
  112. if err := setIndexViewData(c); err != nil {
  113. c.Handle(500, "Failed to get settings", err)
  114. return
  115. }
  116. c.HTML(404, "index")
  117. }