grafana/pkg/api/index.go

128 lines
3.5 KiB
Go
Raw Normal View History

package api
import (
2015-02-05 03:37:13 -06:00
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/middleware"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/plugins"
2015-02-05 03:37:13 -06:00
"github.com/grafana/grafana/pkg/setting"
)
func setIndexViewData(c *middleware.Context) (*dtos.IndexViewData, error) {
settings, err := getFrontendSettingsMap(c)
if err != nil {
return nil, err
}
var data = dtos.IndexViewData{
User: &dtos.CurrentUser{
Id: c.UserId,
IsSignedIn: c.IsSignedIn,
Login: c.Login,
Email: c.Email,
Name: c.Name,
LightTheme: c.Theme == "light",
OrgId: c.OrgId,
OrgName: c.OrgName,
OrgRole: c.OrgRole,
GravatarUrl: dtos.GetGravatarUrl(c.Email),
IsGrafanaAdmin: c.IsGrafanaAdmin,
},
Settings: settings,
AppUrl: setting.AppUrl,
AppSubUrl: setting.AppSubUrl,
GoogleAnalyticsId: setting.GoogleAnalyticsId,
GoogleTagManagerId: setting.GoogleTagManagerId,
}
if setting.DisableGravatar {
data.User.GravatarUrl = setting.AppSubUrl + "/public/img/user_profile.png"
}
if len(data.User.Name) == 0 {
data.User.Name = data.User.Login
2015-02-03 03:46:52 -06:00
}
themeUrlParam := c.Query("theme")
if themeUrlParam == "light" {
data.User.LightTheme = true
}
data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
Text: "Dashboards",
2016-02-11 08:53:48 -06:00
Icon: "icon-gf icon-gf-dashboard",
Url: setting.AppSubUrl + "/",
Children: []*dtos.NavLink{
{Text: "Home dashboard", Icon: "fa fa-fw fa-list", Url: setting.AppSubUrl + "/"},
{Text: "Playlists", Icon: "fa fa-fw fa-list", Url: setting.AppSubUrl + "/playlists"},
{Text: "Snapshots", Icon: "fa-fw icon-gf icon-gf-snapshot", Url: setting.AppSubUrl + "/dashboard/snapshots"},
},
})
// data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{Text: "Playlists", Icon: "fa fa-fw fa-list", Url: setting.AppSubUrl + "/playlists"})
// data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{Text: "Snapshots", Icon: "fa-fw icon-gf icon-gf-snapshot", Url: setting.AppSubUrl + "/dashboard/snapshots"})
if c.OrgRole == m.ROLE_ADMIN {
data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
Text: "Data Sources",
2016-02-11 08:53:48 -06:00
Icon: "icon-gf icon-gf-datasources",
Url: setting.AppSubUrl + "/datasources",
})
2016-01-08 05:45:12 -06:00
data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
2016-02-11 08:53:48 -06:00
Text: "Plugins",
Icon: "icon-gf icon-gf-apps",
Url: setting.AppSubUrl + "/apps",
2016-01-08 05:45:12 -06:00
})
}
enabledPlugins, err := plugins.GetEnabledPlugins(c.OrgId)
if err != nil {
return nil, err
}
2015-12-21 16:09:27 -06:00
for _, plugin := range enabledPlugins.Apps {
2016-01-11 11:03:08 -06:00
if plugin.Pinned {
pageLink := &dtos.NavLink{
2016-01-11 11:03:08 -06:00
Text: plugin.Name,
Url: setting.AppSubUrl + "/apps/" + plugin.Id + "/edit",
2016-01-11 11:03:08 -06:00
Img: plugin.Info.Logos.Small,
}
for _, page := range plugin.Pages {
pageLink.Children = append(pageLink.Children, &dtos.NavLink{
Url: setting.AppSubUrl + "/apps/" + plugin.Id + "/page/" + page.Slug,
Text: page.Name,
})
}
data.MainNavLinks = append(data.MainNavLinks, pageLink)
}
}
return &data, nil
}
func Index(c *middleware.Context) {
if data, err := setIndexViewData(c); err != nil {
c.Handle(500, "Failed to get settings", err)
return
} else {
c.HTML(200, "index", data)
}
}
func NotFoundHandler(c *middleware.Context) {
if c.IsApiRequest() {
2015-03-22 14:14:00 -05:00
c.JsonApiErr(404, "Not found", nil)
return
}
if data, err := setIndexViewData(c); err != nil {
c.Handle(500, "Failed to get settings", err)
return
} else {
c.HTML(404, "index", data)
}
}