mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
feat(external_plugin): lots of refactoring for side menu link extensions and view data, #3185
This commit is contained in:
parent
63b50ab9b1
commit
df0bc7bbc4
20
pkg/api/dtos/index.go
Normal file
20
pkg/api/dtos/index.go
Normal file
@ -0,0 +1,20 @@
|
||||
package dtos
|
||||
|
||||
type IndexViewData struct {
|
||||
User *CurrentUser
|
||||
Settings map[string]interface{}
|
||||
AppUrl string
|
||||
AppSubUrl string
|
||||
GoogleAnalyticsId string
|
||||
GoogleTagManagerId string
|
||||
|
||||
PluginCss []string
|
||||
PluginJs []string
|
||||
MainNavLinks []*NavLink
|
||||
}
|
||||
|
||||
type NavLink struct {
|
||||
Text string `json:"text"`
|
||||
Icon string `json:"icon"`
|
||||
Href string `json:"href"`
|
||||
}
|
@ -34,8 +34,9 @@ func InitExternalPluginRoutes(r *macaron.Macaron) {
|
||||
for _, plugin := range plugins.ExternalPlugins {
|
||||
log.Info("Plugin: Adding proxy routes for backend plugin")
|
||||
for _, route := range plugin.Routes {
|
||||
log.Info("Plugin: Adding route %s /api/plugin-proxy/%s", route.Method, route.Path)
|
||||
r.Route(util.JoinUrlFragments("/api/plugin-proxy/", route.Path), route.Method, ExternalPlugin(route.Url))
|
||||
url := util.JoinUrlFragments("/api/plugin-proxy/", route.Path)
|
||||
r.Route(url, route.Method, ExternalPlugin(route.Url))
|
||||
log.Info("Plugin: Adding route %s", url)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,84 +3,87 @@ package api
|
||||
import (
|
||||
"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"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
func setIndexViewData(c *middleware.Context) error {
|
||||
func setIndexViewData(c *middleware.Context) (*dtos.IndexViewData, error) {
|
||||
settings, err := getFrontendSettingsMap(c)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
currentUser := &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,
|
||||
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 {
|
||||
currentUser.GravatarUrl = setting.AppSubUrl + "/img/user_profile.png"
|
||||
data.User.GravatarUrl = setting.AppSubUrl + "/img/user_profile.png"
|
||||
}
|
||||
|
||||
if len(currentUser.Name) == 0 {
|
||||
currentUser.Name = currentUser.Login
|
||||
if len(data.User.Name) == 0 {
|
||||
data.User.Name = data.User.Login
|
||||
}
|
||||
|
||||
themeUrlParam := c.Query("theme")
|
||||
if themeUrlParam == "light" {
|
||||
currentUser.LightTheme = true
|
||||
data.User.LightTheme = true
|
||||
}
|
||||
|
||||
c.Data["User"] = currentUser
|
||||
c.Data["Settings"] = settings
|
||||
c.Data["AppUrl"] = setting.AppUrl
|
||||
c.Data["AppSubUrl"] = setting.AppSubUrl
|
||||
data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
|
||||
Text: "Dashboards",
|
||||
Icon: "fa fa-fw fa-th-large",
|
||||
Href: "/",
|
||||
})
|
||||
|
||||
if setting.GoogleAnalyticsId != "" {
|
||||
c.Data["GoogleAnalyticsId"] = setting.GoogleAnalyticsId
|
||||
if c.OrgRole == m.ROLE_ADMIN {
|
||||
data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
|
||||
Text: "Data Sources",
|
||||
Icon: "fa fa-fw fa-database",
|
||||
Href: "/datasources",
|
||||
})
|
||||
}
|
||||
|
||||
if setting.GoogleTagManagerId != "" {
|
||||
c.Data["GoogleTagManagerId"] = setting.GoogleTagManagerId
|
||||
}
|
||||
|
||||
externalPluginJs := make([]string, 0)
|
||||
externalPluginCss := make([]string, 0)
|
||||
externalPluginMenu := make([]*plugins.ExternalPluginMenuItem, 0)
|
||||
for _, plugin := range plugins.ExternalPlugins {
|
||||
for _, js := range plugin.Js {
|
||||
externalPluginJs = append(externalPluginJs, js.Module)
|
||||
data.PluginJs = append(data.PluginJs, js.Module)
|
||||
}
|
||||
for _, css := range plugin.Css {
|
||||
externalPluginCss = append(externalPluginCss, css.Href)
|
||||
data.PluginCss = append(data.PluginCss, css.Href)
|
||||
}
|
||||
for _, item := range plugin.MenuItems {
|
||||
externalPluginMenu = append(externalPluginMenu, item)
|
||||
for _, item := range plugin.MainNavLinks {
|
||||
data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{Text: item.Text, Href: item.Href, Icon: item.Icon})
|
||||
}
|
||||
}
|
||||
c.Data["ExternalPluginJs"] = externalPluginJs
|
||||
c.Data["ExternalPluginCss"] = externalPluginCss
|
||||
c.Data["ExternalPluginMenu"] = externalPluginMenu
|
||||
|
||||
return nil
|
||||
return &data, nil
|
||||
}
|
||||
|
||||
func Index(c *middleware.Context) {
|
||||
if err := setIndexViewData(c); err != nil {
|
||||
if data, err := setIndexViewData(c); err != nil {
|
||||
c.Handle(500, "Failed to get settings", err)
|
||||
return
|
||||
} else {
|
||||
c.HTML(200, "index", data)
|
||||
}
|
||||
|
||||
c.HTML(200, "index")
|
||||
}
|
||||
|
||||
func NotFoundHandler(c *middleware.Context) {
|
||||
@ -89,10 +92,10 @@ func NotFoundHandler(c *middleware.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := setIndexViewData(c); err != nil {
|
||||
if data, err := setIndexViewData(c); err != nil {
|
||||
c.Handle(500, "Failed to get settings", err)
|
||||
return
|
||||
} else {
|
||||
c.HTML(404, "index", data)
|
||||
}
|
||||
|
||||
c.HTML(404, "index")
|
||||
}
|
||||
|
@ -19,18 +19,18 @@ const (
|
||||
)
|
||||
|
||||
func LoginView(c *middleware.Context) {
|
||||
if err := setIndexViewData(c); err != nil {
|
||||
viewData, err := setIndexViewData(c)
|
||||
if err != nil {
|
||||
c.Handle(500, "Failed to get settings", err)
|
||||
return
|
||||
}
|
||||
|
||||
settings := c.Data["Settings"].(map[string]interface{})
|
||||
settings["googleAuthEnabled"] = setting.OAuthService.Google
|
||||
settings["githubAuthEnabled"] = setting.OAuthService.GitHub
|
||||
settings["disableUserSignUp"] = !setting.AllowUserSignUp
|
||||
viewData.Settings["googleAuthEnabled"] = setting.OAuthService.Google
|
||||
viewData.Settings["githubAuthEnabled"] = setting.OAuthService.GitHub
|
||||
viewData.Settings["disableUserSignUp"] = !setting.AllowUserSignUp
|
||||
|
||||
if !tryLoginUsingRememberCookie(c) {
|
||||
c.HTML(200, VIEW_INDEX)
|
||||
c.HTML(200, VIEW_INDEX, viewData)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -131,8 +131,8 @@ func (a *ldapAuther) getGrafanaUserFor(ldapUser *ldapUserInfo) (*m.User, error)
|
||||
}
|
||||
|
||||
return userQuery.Result, nil
|
||||
}
|
||||
|
||||
}
|
||||
func (a *ldapAuther) createGrafanaUser(ldapUser *ldapUserInfo) (*m.User, error) {
|
||||
cmd := m.CreateUserCommand{
|
||||
Login: ldapUser.Username,
|
||||
|
@ -33,7 +33,7 @@ type ExternalPluginJs struct {
|
||||
Module string `json:"module"`
|
||||
}
|
||||
|
||||
type ExternalPluginMenuItem struct {
|
||||
type ExternalPluginNavLink struct {
|
||||
Text string `json:"text"`
|
||||
Icon string `json:"icon"`
|
||||
Href string `json:"href"`
|
||||
@ -44,9 +44,9 @@ type ExternalPluginCss struct {
|
||||
}
|
||||
|
||||
type ExternalPlugin struct {
|
||||
Routes []*ExternalPluginRoute `json:"routes"`
|
||||
Js []*ExternalPluginJs `json:"js"`
|
||||
Css []*ExternalPluginCss `json:"css"`
|
||||
MenuItems []*ExternalPluginMenuItem `json:"menuItems"`
|
||||
StaticRootConfig *StaticRootConfig `json:"staticRoot"`
|
||||
Routes []*ExternalPluginRoute `json:"routes"`
|
||||
Js []*ExternalPluginJs `json:"js"`
|
||||
Css []*ExternalPluginCss `json:"css"`
|
||||
MainNavLinks []*ExternalPluginNavLink `json:"mainNavLinks"`
|
||||
StaticRootConfig *StaticRootConfig `json:"staticRoot"`
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ define([
|
||||
'angular',
|
||||
'jquery',
|
||||
'lodash',
|
||||
'app/core/config',
|
||||
'require',
|
||||
'bootstrap',
|
||||
'angular-route',
|
||||
@ -12,7 +13,7 @@ define([
|
||||
'bindonce',
|
||||
'app/core/core',
|
||||
],
|
||||
function (angular, $, _, appLevelRequire) {
|
||||
function (angular, $, _, config, appLevelRequire) {
|
||||
"use strict";
|
||||
|
||||
var app = angular.module('grafana', []);
|
||||
@ -69,7 +70,7 @@ function (angular, $, _, appLevelRequire) {
|
||||
});
|
||||
|
||||
var preBootRequires = ['app/features/all'];
|
||||
var pluginModules = window.grafanaBootData.pluginModules || [];
|
||||
var pluginModules = config.bootData.pluginModules || [];
|
||||
|
||||
// add plugin modules
|
||||
for (var i = 0; i < pluginModules.length; i++) {
|
||||
|
@ -6,6 +6,7 @@ function (Settings) {
|
||||
|
||||
var bootData = window.grafanaBootData || { settings: {} };
|
||||
var options = bootData.settings;
|
||||
options.bootData = bootData;
|
||||
|
||||
return new Settings(options);
|
||||
|
||||
|
@ -15,31 +15,13 @@ function (angular, _, $, coreModule, config) {
|
||||
};
|
||||
|
||||
$scope.setupMainNav = function() {
|
||||
$scope.mainLinks.push({
|
||||
text: "Dashboards",
|
||||
icon: "fa fa-fw fa-th-large",
|
||||
href: $scope.getUrl("/"),
|
||||
});
|
||||
|
||||
if (contextSrv.hasRole('Admin')) {
|
||||
_.each(config.bootData.mainNavLinks, function(item) {
|
||||
$scope.mainLinks.push({
|
||||
text: "Data Sources",
|
||||
icon: "fa fa-fw fa-database",
|
||||
href: $scope.getUrl("/datasources"),
|
||||
text: item.text,
|
||||
icon: item.icon,
|
||||
href: $scope.getUrl(item.href)
|
||||
});
|
||||
}
|
||||
|
||||
if (_.isArray(window.externalPlugins.mainLinks)) {
|
||||
_.forEach(window.externalPlugins.mainLinks, function(item) {
|
||||
if (!item.adminOnly || contextSrv.hasRole('Admin')) {
|
||||
$scope.mainLinks.push({
|
||||
text: item.text,
|
||||
icon: item.icon,
|
||||
href: $scope.getUrl(item.href)
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
$scope.loadOrgs = function() {
|
||||
|
@ -12,8 +12,8 @@ function (angular, _, coreModule, store, config) {
|
||||
var self = this;
|
||||
|
||||
function User() {
|
||||
if (window.grafanaBootData.user) {
|
||||
_.extend(this, window.grafanaBootData.user);
|
||||
if (config.bootData.user) {
|
||||
_.extend(this, config.bootData.user);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -95,7 +95,7 @@ function file2moduleName(filePath) {
|
||||
.replace(/\.\w*$/, '');
|
||||
}
|
||||
|
||||
window.grafanaBootData = {};
|
||||
window.grafanaBootData = {settings: {}};
|
||||
|
||||
require([
|
||||
'lodash',
|
||||
|
@ -14,7 +14,7 @@
|
||||
<link rel="stylesheet" href="[[.AppSubUrl]]/css/grafana.dark.min.css">
|
||||
[[end]]
|
||||
|
||||
[[ range $css := .ExternalPluginCss ]]
|
||||
[[ range $css := .PluginCss ]]
|
||||
<link rel="stylesheet" href="[[$.AppSubUrl]]/[[ $css ]]">
|
||||
[[ end ]]
|
||||
|
||||
@ -54,11 +54,8 @@
|
||||
window.grafanaBootData = {
|
||||
user:[[.User]],
|
||||
settings: [[.Settings]],
|
||||
pluginModules: [[.ExternalPluginJs]],
|
||||
};
|
||||
|
||||
window.externalPlugins = {
|
||||
mainLinks: [[.ExternalPluginMenu]]
|
||||
pluginModules: [[.PluginJs]],
|
||||
mainNavLinks: [[.MainNavLinks]]
|
||||
};
|
||||
|
||||
require(['app/app'], function (app) {
|
||||
|
Loading…
Reference in New Issue
Block a user