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