api.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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("/admin", reqSignedIn, Index)
  28. r.Get("/dashboard/*", reqSignedIn, Index)
  29. // sign up
  30. r.Get("/signup", Index)
  31. r.Post("/api/account/signup", SignUp)
  32. // authed api
  33. r.Group("/api", func() {
  34. // account
  35. r.Group("/account", func() {
  36. r.Get("/", GetAccount)
  37. r.Post("/", bind(m.UpdateAccountCommand{}), UpdateAccount)
  38. r.Put("/collaborators", bind(m.AddCollaboratorCommand{}), AddCollaborator)
  39. r.Get("/collaborators", GetCollaborators)
  40. r.Delete("/collaborators/:id", RemoveCollaborator)
  41. r.Post("/using/:id", SetUsingAccount)
  42. r.Get("/others", GetOtherAccounts)
  43. })
  44. // Token
  45. r.Group("/tokens", func() {
  46. r.Combo("/").
  47. Get(GetTokens).
  48. Put(bind(m.AddTokenCommand{}), AddToken).
  49. Post(bind(m.UpdateTokenCommand{}), UpdateToken)
  50. r.Delete("/:id", DeleteToken)
  51. })
  52. // Data sources
  53. r.Group("/datasources", func() {
  54. r.Combo("/").Get(GetDataSources).Put(AddDataSource).Post(UpdateDataSource)
  55. r.Delete("/:id", DeleteDataSource)
  56. r.Any("/proxy/:id/*", reqSignedIn, ProxyDataSourceRequest)
  57. })
  58. // Dashboard
  59. r.Group("/dashboard", func() {
  60. r.Combo("/:slug").Get(GetDashboard).Delete(DeleteDashboard)
  61. r.Post("/", reqEditorRole, bind(m.SaveDashboardCommand{}), PostDashboard)
  62. })
  63. // Search
  64. r.Get("/search/", Search)
  65. // metrics
  66. r.Get("/metrics/test", GetTestMetrics)
  67. }, reqSignedIn)
  68. // admin api
  69. r.Group("/api/admin", func() {
  70. r.Get("/accounts", AdminSearchAccounts)
  71. }, reqGrafanaAdmin)
  72. // rendering
  73. r.Get("/render/*", reqSignedIn, RenderToPng)
  74. r.NotFound(NotFound)
  75. }
  76. func setIndexViewData(c *middleware.Context) error {
  77. settings, err := getFrontendSettings(c)
  78. if err != nil {
  79. return err
  80. }
  81. currentUser := &dtos.CurrentUser{}
  82. if c.IsSignedIn {
  83. currentUser = &dtos.CurrentUser{
  84. Login: c.UserLogin,
  85. Email: c.UserEmail,
  86. Name: c.UserName,
  87. UsingAccountName: c.UsingAccountName,
  88. GravatarUrl: dtos.GetGravatarUrl(c.UserEmail),
  89. IsGrafanaAdmin: c.IsGrafanaAdmin,
  90. Role: c.UserRole,
  91. }
  92. }
  93. c.Data["User"] = currentUser
  94. c.Data["Settings"] = settings
  95. c.Data["AppUrl"] = setting.AppUrl
  96. c.Data["AppSubUrl"] = setting.AppSubUrl
  97. return nil
  98. }
  99. func Index(c *middleware.Context) {
  100. if err := setIndexViewData(c); err != nil {
  101. c.Handle(500, "Failed to get settings", err)
  102. return
  103. }
  104. c.HTML(200, "index")
  105. }
  106. func NotFound(c *middleware.Context) {
  107. if c.IsApiRequest() {
  108. c.JsonApiErr(200, "Not found", nil)
  109. return
  110. }
  111. if err := setIndexViewData(c); err != nil {
  112. c.Handle(500, "Failed to get settings", err)
  113. return
  114. }
  115. c.HTML(404, "index")
  116. }