mirror of
https://github.com/grafana/grafana.git
synced 2025-08-01 14:42:13 +08:00
refer to plugins are ExternalPlugin instead of thirdParty
This commit is contained in:
@ -185,7 +185,7 @@ func Register(r *macaron.Macaron) {
|
|||||||
// rendering
|
// rendering
|
||||||
r.Get("/render/*", reqSignedIn, RenderToPng)
|
r.Get("/render/*", reqSignedIn, RenderToPng)
|
||||||
|
|
||||||
InitThirdPartyRoutes(r)
|
InitExternalPluginRoutes(r)
|
||||||
|
|
||||||
r.NotFound(NotFoundHandler)
|
r.NotFound(NotFoundHandler)
|
||||||
}
|
}
|
||||||
|
@ -3,16 +3,16 @@ package api
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/Unknwon/macaron"
|
"github.com/Unknwon/macaron"
|
||||||
|
"github.com/grafana/grafana/pkg/log"
|
||||||
"github.com/grafana/grafana/pkg/middleware"
|
"github.com/grafana/grafana/pkg/middleware"
|
||||||
"github.com/grafana/grafana/pkg/plugins"
|
"github.com/grafana/grafana/pkg/plugins"
|
||||||
"github.com/grafana/grafana/pkg/util"
|
"github.com/grafana/grafana/pkg/util"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
"net/url"
|
"net/url"
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitThirdPartyRoutes(r *macaron.Macaron) {
|
func InitExternalPluginRoutes(r *macaron.Macaron) {
|
||||||
/*
|
/*
|
||||||
// Handle Auth and role requirements
|
// Handle Auth and role requirements
|
||||||
if route.ReqSignedIn {
|
if route.ReqSignedIn {
|
||||||
@ -30,34 +30,33 @@ func InitThirdPartyRoutes(r *macaron.Macaron) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
for _, integration := range plugins.Integrations {
|
for _, plugin := range plugins.ExternalPlugins {
|
||||||
log.Printf("adding routes for integration")
|
log.Info("adding routes for external plugin")
|
||||||
for _, route := range integration.Routes {
|
for _, route := range plugin.Settings.Routes {
|
||||||
log.Printf("adding route %s %s", route.Method, route.Path)
|
log.Info("adding route %s /plugins%s", route.Method, route.Path)
|
||||||
r.Route(util.JoinUrlFragments("/thirdparty/", route.Path), route.Method, ThirdParty(route.Url))
|
r.Route(util.JoinUrlFragments("/plugins/", route.Path), route.Method, ExternalPlugin(route.Url))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ThirdParty(routeUrl string) macaron.Handler {
|
func ExternalPlugin(routeUrl string) macaron.Handler {
|
||||||
return func(c *middleware.Context) {
|
return func(c *middleware.Context) {
|
||||||
path := c.Params("*")
|
path := c.Params("*")
|
||||||
|
|
||||||
//Create a HTTP header with the context in it.
|
//Create a HTTP header with the context in it.
|
||||||
ctx, err := json.Marshal(c.SignedInUser)
|
ctx, err := json.Marshal(c.SignedInUser)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JsonApiErr(500, "Not found", err)
|
c.JsonApiErr(500, "failed to marshal context to json.", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Printf(string(ctx))
|
|
||||||
targetUrl, _ := url.Parse(routeUrl)
|
targetUrl, _ := url.Parse(routeUrl)
|
||||||
proxy := NewThirdPartyProxy(string(ctx), path, targetUrl)
|
proxy := NewExternalPluginProxy(string(ctx), path, targetUrl)
|
||||||
proxy.Transport = dataProxyTransport
|
proxy.Transport = dataProxyTransport
|
||||||
proxy.ServeHTTP(c.RW(), c.Req.Request)
|
proxy.ServeHTTP(c.RW(), c.Req.Request)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewThirdPartyProxy(ctx string, proxyPath string, targetUrl *url.URL) *httputil.ReverseProxy {
|
func NewExternalPluginProxy(ctx string, proxyPath string, targetUrl *url.URL) *httputil.ReverseProxy {
|
||||||
director := func(req *http.Request) {
|
director := func(req *http.Request) {
|
||||||
req.URL.Scheme = targetUrl.Scheme
|
req.URL.Scheme = targetUrl.Scheme
|
||||||
req.URL.Host = targetUrl.Host
|
req.URL.Host = targetUrl.Host
|
@ -52,25 +52,25 @@ func setIndexViewData(c *middleware.Context) error {
|
|||||||
if setting.GoogleTagManagerId != "" {
|
if setting.GoogleTagManagerId != "" {
|
||||||
c.Data["GoogleTagManagerId"] = setting.GoogleTagManagerId
|
c.Data["GoogleTagManagerId"] = setting.GoogleTagManagerId
|
||||||
}
|
}
|
||||||
// This can be loaded from the DB/file to allow 3rdParty integration
|
|
||||||
thirdPartyJs := make([]string, 0)
|
externalPluginJs := make([]string, 0)
|
||||||
thirdPartyCss := make([]string, 0)
|
externalPluginCss := make([]string, 0)
|
||||||
thirdPartyMenu := make([]*plugins.ThirdPartyMenuItem, 0)
|
externalPluginMenu := make([]*plugins.ExternalPluginMenuItem, 0)
|
||||||
for _, integration := range plugins.Integrations {
|
for _, plugin := range plugins.ExternalPlugins {
|
||||||
for _, js := range integration.Js {
|
for _, js := range plugin.Settings.Js {
|
||||||
thirdPartyJs = append(thirdPartyJs, js.Src)
|
externalPluginJs = append(externalPluginJs, js.Src)
|
||||||
}
|
}
|
||||||
for _, css := range integration.Css {
|
for _, css := range plugin.Settings.Css {
|
||||||
thirdPartyCss = append(thirdPartyCss, css.Href)
|
externalPluginCss = append(externalPluginCss, css.Href)
|
||||||
}
|
}
|
||||||
for _, item := range integration.MenuItems {
|
for _, item := range plugin.Settings.MenuItems {
|
||||||
thirdPartyMenu = append(thirdPartyMenu, item)
|
externalPluginMenu = append(externalPluginMenu, item)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
c.Data["ThirdPartyJs"] = thirdPartyJs
|
c.Data["ExternalPluginJs"] = externalPluginJs
|
||||||
c.Data["ThirdPartyCss"] = thirdPartyCss
|
c.Data["ExternalPluginCss"] = externalPluginCss
|
||||||
c.Data["ThirdPartyMenu"] = thirdPartyMenu
|
c.Data["ExternalPluginMenu"] = externalPluginMenu
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
type ThirdPartyRoute struct {
|
type ExternalPluginRoute struct {
|
||||||
Path string `json:"path"`
|
Path string `json:"path"`
|
||||||
Method string `json:"method"`
|
Method string `json:"method"`
|
||||||
ReqSignedIn bool `json:"req_signed_in"`
|
ReqSignedIn bool `json:"req_signed_in"`
|
||||||
@ -9,23 +9,23 @@ type ThirdPartyRoute struct {
|
|||||||
Url string `json:"url"`
|
Url string `json:"url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ThirdPartyJs struct {
|
type ExternalPluginJs struct {
|
||||||
src string `json:"src"`
|
src string `json:"src"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ThirdPartyMenuItem struct {
|
type ExternalPluginMenuItem 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"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ThirdPartyCss struct {
|
type ExternalPluginCss struct {
|
||||||
Href string `json:"href"`
|
Href string `json:"href"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ThirdPartyIntegration struct {
|
type ExternalPluginIntegration struct {
|
||||||
Routes []*ThirdPartyRoute `json:"routes"`
|
Routes []*ExternalPluginRoute `json:"routes"`
|
||||||
Js []*ThirdPartyJs `json:"js"`
|
Js []*ExternalPluginJs `json:"js"`
|
||||||
Css []*ThirdPartyCss `json:"css"`
|
Css []*ExternalPluginCss `json:"css"`
|
||||||
MenuItems []*ThirdPartyMenuItem `json:"menu_items"`
|
MenuItems []*ExternalPluginMenuItem `json:"menu_items"`
|
||||||
}
|
}
|
@ -16,7 +16,7 @@ type PluginMeta struct {
|
|||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ThirdPartyRoute struct {
|
type ExternalPluginRoute struct {
|
||||||
Path string `json:"path"`
|
Path string `json:"path"`
|
||||||
Method string `json:"method"`
|
Method string `json:"method"`
|
||||||
ReqSignedIn bool `json:"req_signed_in"`
|
ReqSignedIn bool `json:"req_signed_in"`
|
||||||
@ -25,35 +25,35 @@ type ThirdPartyRoute struct {
|
|||||||
Url string `json:"url"`
|
Url string `json:"url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ThirdPartyJs struct {
|
type ExternalPluginJs struct {
|
||||||
Src string `json:"src"`
|
Src string `json:"src"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ThirdPartyMenuItem struct {
|
type ExternalPluginMenuItem 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"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ThirdPartyCss struct {
|
type ExternalPluginCss struct {
|
||||||
Href string `json:"href"`
|
Href string `json:"href"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ThirdPartyIntegration struct {
|
type ExternalPluginSettings struct {
|
||||||
Routes []*ThirdPartyRoute `json:"routes"`
|
Routes []*ExternalPluginRoute `json:"routes"`
|
||||||
Js []*ThirdPartyJs `json:"js"`
|
Js []*ExternalPluginJs `json:"js"`
|
||||||
Css []*ThirdPartyCss `json:"css"`
|
Css []*ExternalPluginCss `json:"css"`
|
||||||
MenuItems []*ThirdPartyMenuItem `json:"menu_items"`
|
MenuItems []*ExternalPluginMenuItem `json:"menu_items"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ThirdPartyPlugin struct {
|
type ExternalPlugin struct {
|
||||||
PluginType string `json:"pluginType"`
|
PluginType string `json:"pluginType"`
|
||||||
Integration ThirdPartyIntegration `json:"integration"`
|
Settings ExternalPluginSettings `json:"settings"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
DataSources map[string]interface{}
|
DataSources map[string]interface{}
|
||||||
Integrations []ThirdPartyIntegration
|
ExternalPlugins []ExternalPlugin
|
||||||
)
|
)
|
||||||
|
|
||||||
type PluginScanner struct {
|
type PluginScanner struct {
|
||||||
@ -67,7 +67,7 @@ func Init() {
|
|||||||
|
|
||||||
func scan(pluginDir string) error {
|
func scan(pluginDir string) error {
|
||||||
DataSources = make(map[string]interface{})
|
DataSources = make(map[string]interface{})
|
||||||
Integrations = make([]ThirdPartyIntegration, 0)
|
ExternalPlugins = make([]ExternalPlugin, 0)
|
||||||
|
|
||||||
scanner := &PluginScanner{
|
scanner := &PluginScanner{
|
||||||
pluginPath: pluginDir,
|
pluginPath: pluginDir,
|
||||||
@ -131,13 +131,13 @@ func (scanner *PluginScanner) loadPluginJson(path string) error {
|
|||||||
|
|
||||||
DataSources[datasourceType.(string)] = pluginJson
|
DataSources[datasourceType.(string)] = pluginJson
|
||||||
}
|
}
|
||||||
if pluginType == "thirdPartyIntegration" {
|
if pluginType == "externalPlugin" {
|
||||||
p := ThirdPartyPlugin{}
|
p := ExternalPlugin{}
|
||||||
reader.Seek(0, 0)
|
reader.Seek(0, 0)
|
||||||
if err := jsonParser.Decode(&p); err != nil {
|
if err := jsonParser.Decode(&p); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
Integrations = append(Integrations, p.Integration)
|
ExternalPlugins = append(ExternalPlugins, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -30,13 +30,15 @@ function (angular, _, $, config) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_.isArray(window.thirdParty.MainLinks)) {
|
if (_.isArray(window.externalPlugins.MainLinks)) {
|
||||||
_.forEach(window.thirdParty.MainLinks, function(item) {
|
_.forEach(window.externalPlugins.MainLinks, function(item) {
|
||||||
$scope.mainLinks.push({
|
if (!item.adminOnly || contextSrv.hasRole('Admin')) {
|
||||||
text: item.text,
|
$scope.mainLinks.push({
|
||||||
icon: item.icon,
|
text: item.text,
|
||||||
href: $scope.getUrl(item.href)
|
icon: item.icon,
|
||||||
});
|
href: $scope.getUrl(item.href)
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
3
public/app/plugins/externalPlugins/example/README.TXT
Normal file
3
public/app/plugins/externalPlugins/example/README.TXT
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
Example app is available at https://github.com/raintank/grafana-plugin-example
|
||||||
|
|
||||||
|
To use, download the example app from github and run it (requires python Flask). Then rename the "_plugin.json" file in this director to "plugin.json" and restart Grafana.
|
41
public/app/plugins/externalPlugins/example/_plugin.json
Normal file
41
public/app/plugins/externalPlugins/example/_plugin.json
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
{
|
||||||
|
"pluginType": "externalPlugin",
|
||||||
|
"settings": {
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"path": "/example/static/*",
|
||||||
|
"method": "*",
|
||||||
|
"req_signed_in": false,
|
||||||
|
"req_grafana_admin": false,
|
||||||
|
"req_role": "Admin",
|
||||||
|
"url": "http://localhost:5000/static"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "/example/api/*",
|
||||||
|
"method": "*",
|
||||||
|
"req_signed_in": true,
|
||||||
|
"req_grafana_admin": false,
|
||||||
|
"req_role": "Admin",
|
||||||
|
"url": "http://localhost:5000/api"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"css": [
|
||||||
|
{
|
||||||
|
"href": "/example/static/css/example.css"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"js": [
|
||||||
|
{
|
||||||
|
"src": "/example/static/js/app.js"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"menu_items": [
|
||||||
|
{
|
||||||
|
"text": "Example Plugin",
|
||||||
|
"icon": "fa fa-fw fa-smile-o",
|
||||||
|
"href": "/example/servers",
|
||||||
|
"adminOnly": false,
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
@ -1,40 +0,0 @@
|
|||||||
{
|
|
||||||
"pluginType": "thirdPartyIntegration",
|
|
||||||
"integration": {
|
|
||||||
"routes": [
|
|
||||||
{
|
|
||||||
"path": "/raintank/public/*",
|
|
||||||
"method": "*",
|
|
||||||
"req_signed_in": false,
|
|
||||||
"req_grafana_admin": false,
|
|
||||||
"req_role": "Admin",
|
|
||||||
"url": "http://localhost:3001/public"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"path": "/raintank/api/*",
|
|
||||||
"method": "*",
|
|
||||||
"req_signed_in": true,
|
|
||||||
"req_grafana_admin": false,
|
|
||||||
"req_role": "Admin",
|
|
||||||
"url": "http://localhost:3001/api"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"css": [
|
|
||||||
{
|
|
||||||
"href": "/path/to/file.css"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"js": [
|
|
||||||
{
|
|
||||||
"src": "/raintank/public/app.js"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"menu_items": [
|
|
||||||
{
|
|
||||||
"text": "Menu Text",
|
|
||||||
"icon": "fa fa-fw fa-database",
|
|
||||||
"href": "/raintank/test"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
@ -13,8 +13,8 @@
|
|||||||
[[else]]
|
[[else]]
|
||||||
<link rel="stylesheet" href="[[.AppSubUrl]]/css/grafana.dark.min.css">
|
<link rel="stylesheet" href="[[.AppSubUrl]]/css/grafana.dark.min.css">
|
||||||
[[end]]
|
[[end]]
|
||||||
[[ range $css := .ThirdPartyCss ]]
|
[[ range $css := .ExternalPluginCss ]]
|
||||||
<link rel="stylesheet" href="[[$.AppSubUrl]]/thirdparty[[ $css ]]">
|
<link rel="stylesheet" href="[[$.AppSubUrl]]/plugins[[ $css ]]">
|
||||||
[[ end ]]
|
[[ end ]]
|
||||||
|
|
||||||
<link rel="icon" type="image/png" href="[[.AppSubUrl]]/img/fav32.png">
|
<link rel="icon" type="image/png" href="[[.AppSubUrl]]/img/fav32.png">
|
||||||
@ -56,16 +56,16 @@
|
|||||||
settings: [[.Settings]],
|
settings: [[.Settings]],
|
||||||
};
|
};
|
||||||
|
|
||||||
window.thirdParty = {
|
window.externalPlugins = {
|
||||||
MainLinks: [[.ThirdPartyMenu]]
|
MainLinks: [[.ExternalPluginMenu]]
|
||||||
};
|
};
|
||||||
|
|
||||||
require(['app/app'], function (app) {
|
require(['app/app'], function (app) {
|
||||||
app.boot();
|
app.boot();
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
[[ range $js := .ThirdPartyJs]]
|
[[ range $js := .ExternalPluginJs]]
|
||||||
<script src="[[$.AppSubUrl]]/thirdparty[[ $js ]]"></script>
|
<script src="[[$.AppSubUrl]]/plugins[[ $js ]]"></script>
|
||||||
[[ end ]]
|
[[ end ]]
|
||||||
[[if .GoogleAnalyticsId]]
|
[[if .GoogleAnalyticsId]]
|
||||||
<script>
|
<script>
|
||||||
|
Reference in New Issue
Block a user