index.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package api
  2. import (
  3. "github.com/grafana/grafana/pkg/api/dtos"
  4. "github.com/grafana/grafana/pkg/middleware"
  5. m "github.com/grafana/grafana/pkg/models"
  6. "github.com/grafana/grafana/pkg/plugins"
  7. "github.com/grafana/grafana/pkg/setting"
  8. )
  9. func setIndexViewData(c *middleware.Context) (*dtos.IndexViewData, error) {
  10. settings, err := getFrontendSettingsMap(c)
  11. if err != nil {
  12. return nil, err
  13. }
  14. var data = dtos.IndexViewData{
  15. User: &dtos.CurrentUser{
  16. Id: c.UserId,
  17. IsSignedIn: c.IsSignedIn,
  18. Login: c.Login,
  19. Email: c.Email,
  20. Name: c.Name,
  21. LightTheme: c.Theme == "light",
  22. OrgId: c.OrgId,
  23. OrgName: c.OrgName,
  24. OrgRole: c.OrgRole,
  25. GravatarUrl: dtos.GetGravatarUrl(c.Email),
  26. IsGrafanaAdmin: c.IsGrafanaAdmin,
  27. },
  28. Settings: settings,
  29. AppUrl: setting.AppUrl,
  30. AppSubUrl: setting.AppSubUrl,
  31. GoogleAnalyticsId: setting.GoogleAnalyticsId,
  32. GoogleTagManagerId: setting.GoogleTagManagerId,
  33. }
  34. if setting.DisableGravatar {
  35. data.User.GravatarUrl = setting.AppSubUrl + "/public/img/transparent.png"
  36. }
  37. if len(data.User.Name) == 0 {
  38. data.User.Name = data.User.Login
  39. }
  40. themeUrlParam := c.Query("theme")
  41. if themeUrlParam == "light" {
  42. data.User.LightTheme = true
  43. }
  44. data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
  45. Text: "Dashboards",
  46. Icon: "icon-gf icon-gf-dashboard",
  47. Url: setting.AppSubUrl + "/",
  48. Children: []*dtos.NavLink{
  49. {Text: "Home", Url: setting.AppSubUrl + "/"},
  50. {Text: "Playlists", Url: setting.AppSubUrl + "/playlists"},
  51. {Text: "Snapshots", Url: setting.AppSubUrl + "/dashboard/snapshots"},
  52. {Divider: true},
  53. {Text: "New", Url: setting.AppSubUrl + "/dashboard/new"},
  54. {Text: "Import", Url: setting.AppSubUrl + "/import/dashboard"},
  55. },
  56. })
  57. if c.OrgRole == m.ROLE_ADMIN {
  58. data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
  59. Text: "Data Sources",
  60. Icon: "icon-gf icon-gf-datasources",
  61. Url: setting.AppSubUrl + "/datasources",
  62. })
  63. data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
  64. Text: "Plugins",
  65. Icon: "icon-gf icon-gf-apps",
  66. Url: setting.AppSubUrl + "/apps",
  67. })
  68. }
  69. enabledPlugins, err := plugins.GetEnabledPlugins(c.OrgId)
  70. if err != nil {
  71. return nil, err
  72. }
  73. for _, plugin := range enabledPlugins.Apps {
  74. if plugin.Pinned {
  75. pageLink := &dtos.NavLink{
  76. Text: plugin.Name,
  77. Url: setting.AppSubUrl + "/apps/" + plugin.Id + "/edit",
  78. Img: plugin.Info.Logos.Small,
  79. }
  80. for _, page := range plugin.Pages {
  81. pageLink.Children = append(pageLink.Children, &dtos.NavLink{
  82. Url: setting.AppSubUrl + "/apps/" + plugin.Id + "/page/" + page.Slug,
  83. Text: page.Name,
  84. })
  85. }
  86. data.MainNavLinks = append(data.MainNavLinks, pageLink)
  87. }
  88. }
  89. if c.IsGrafanaAdmin {
  90. data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
  91. Text: "Admin",
  92. Icon: "fa fa-fw fa-cogs",
  93. Url: setting.AppSubUrl + "/admin",
  94. Children: []*dtos.NavLink{
  95. {Text: "Global Users", Icon: "fa fa-fw fa-cogs", Url: setting.AppSubUrl + "/admin/users"},
  96. {Text: "Global Orgs", Icon: "fa fa-fw fa-cogs", Url: setting.AppSubUrl + "/admin/orgs"},
  97. {Text: "Server Settings", Icon: "fa fa-fw fa-cogs", Url: setting.AppSubUrl + "/admin/settings"},
  98. {Text: "Server Stats", Icon: "fa-fw fa-cogs", Url: setting.AppSubUrl + "/admin/stats"},
  99. },
  100. })
  101. }
  102. return &data, nil
  103. }
  104. func Index(c *middleware.Context) {
  105. if data, err := setIndexViewData(c); err != nil {
  106. c.Handle(500, "Failed to get settings", err)
  107. return
  108. } else {
  109. c.HTML(200, "index", data)
  110. }
  111. }
  112. func NotFoundHandler(c *middleware.Context) {
  113. if c.IsApiRequest() {
  114. c.JsonApiErr(404, "Not found", nil)
  115. return
  116. }
  117. if data, err := setIndexViewData(c); err != nil {
  118. c.Handle(500, "Failed to get settings", err)
  119. return
  120. } else {
  121. c.HTML(404, "index", data)
  122. }
  123. }