api.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package api
  2. import (
  3. "github.com/Unknwon/macaron"
  4. "github.com/torkelo/grafana-pro/pkg/api/dtos"
  5. "github.com/torkelo/grafana-pro/pkg/middleware"
  6. "github.com/torkelo/grafana-pro/pkg/setting"
  7. )
  8. // Register adds http routes
  9. func Register(m *macaron.Macaron) {
  10. auth := middleware.Auth()
  11. // not logged in views
  12. m.Get("/", auth, Index)
  13. m.Post("/logout", LogoutPost)
  14. m.Post("/login", LoginPost)
  15. m.Get("/login/:name", OAuthLogin)
  16. m.Get("/login", Index)
  17. // authed views
  18. m.Get("/account/", auth, Index)
  19. m.Get("/account/datasources/", auth, Index)
  20. m.Get("/admin", auth, Index)
  21. m.Get("/dashboard/*", auth, Index)
  22. // sign up
  23. m.Get("/signup", Index)
  24. m.Post("/api/account/signup", SignUp)
  25. // authed api
  26. m.Group("/api", func() {
  27. // account
  28. m.Group("/account", func() {
  29. m.Get("/", GetAccount)
  30. m.Put("/collaborators", AddCollaborator)
  31. m.Delete("/collaborators/:id", RemoveCollaborator)
  32. m.Post("/using/:id", SetUsingAccount)
  33. m.Get("/others", GetOtherAccounts)
  34. })
  35. // Token
  36. m.Group("/tokens", func() {
  37. m.Combo("/").Get(GetTokens).Put(AddToken).Post(UpdateToken)
  38. m.Delete("/:id", DeleteToken)
  39. })
  40. // Data sources
  41. m.Group("/datasources", func() {
  42. m.Combo("/").Get(GetDataSources).Put(AddDataSource).Post(UpdateDataSource)
  43. m.Delete("/:id", DeleteDataSource)
  44. m.Any("/proxy/:id/*", auth, ProxyDataSourceRequest)
  45. })
  46. // Dashboard
  47. m.Group("/dashboard", func() {
  48. m.Combo("/:slug").Get(GetDashboard).Delete(DeleteDashboard)
  49. m.Post("/", PostDashboard)
  50. })
  51. // Search
  52. m.Get("/search/", Search)
  53. // metrics
  54. m.Get("/metrics/test", auth, GetTestMetrics)
  55. }, auth)
  56. // rendering
  57. m.Get("/render/*", auth, RenderToPng)
  58. m.NotFound(NotFound)
  59. }
  60. func setIndexViewData(c *middleware.Context) error {
  61. settings, err := getFrontendSettings(c)
  62. if err != nil {
  63. return err
  64. }
  65. c.Data["User"] = dtos.NewCurrentUser(c.UserAccount)
  66. c.Data["Settings"] = settings
  67. c.Data["AppUrl"] = setting.AppUrl
  68. c.Data["AppSubUrl"] = setting.AppSubUrl
  69. return nil
  70. }
  71. func Index(c *middleware.Context) {
  72. if err := setIndexViewData(c); err != nil {
  73. c.Handle(500, "Failed to get settings", err)
  74. return
  75. }
  76. c.HTML(200, "index")
  77. }
  78. func NotFound(c *middleware.Context) {
  79. if c.IsApiRequest() {
  80. c.JsonApiErr(200, "Not found", nil)
  81. return
  82. }
  83. if err := setIndexViewData(c); err != nil {
  84. c.Handle(500, "Failed to get settings", err)
  85. return
  86. }
  87. c.HTML(404, "index")
  88. }