feat(external plugins): worked on supporting external plugins better

This commit is contained in:
Torkel Ödegaard
2015-11-19 18:44:07 +01:00
parent 65a7fa320a
commit 69daede583
13 changed files with 58 additions and 60 deletions
+8 -7
View File
@@ -2,14 +2,15 @@ package api
import (
"encoding/json"
"net/http"
"net/http/httputil"
"net/url"
"github.com/Unknwon/macaron"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/middleware"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/util"
"net/http"
"net/http/httputil"
"net/url"
)
func InitExternalPluginRoutes(r *macaron.Macaron) {
@@ -31,10 +32,10 @@ func InitExternalPluginRoutes(r *macaron.Macaron) {
}
*/
for _, plugin := range plugins.ExternalPlugins {
log.Info("adding routes for external plugin")
for _, route := range plugin.Settings.Routes {
log.Info("adding route %s /plugins%s", route.Method, route.Path)
r.Route(util.JoinUrlFragments("/plugins/", route.Path), route.Method, ExternalPlugin(route.Url))
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))
}
}
}
+4 -5
View File
@@ -57,16 +57,15 @@ func setIndexViewData(c *middleware.Context) error {
externalPluginCss := make([]string, 0)
externalPluginMenu := make([]*plugins.ExternalPluginMenuItem, 0)
for _, plugin := range plugins.ExternalPlugins {
for _, js := range plugin.Settings.Js {
externalPluginJs = append(externalPluginJs, js.Src)
for _, js := range plugin.Js {
externalPluginJs = append(externalPluginJs, js.Module)
}
for _, css := range plugin.Settings.Css {
for _, css := range plugin.Css {
externalPluginCss = append(externalPluginCss, css.Href)
}
for _, item := range plugin.Settings.MenuItems {
for _, item := range plugin.MenuItems {
externalPluginMenu = append(externalPluginMenu, item)
}
}
c.Data["ExternalPluginJs"] = externalPluginJs
c.Data["ExternalPluginCss"] = externalPluginCss
+6 -5
View File
@@ -29,6 +29,12 @@ func newMacaron() *macaron.Macaron {
m.Use(middleware.Gziper())
}
for _, route := range plugins.StaticRoutes {
pluginRoute := path.Join("/public/plugins/", route.Url)
log.Info("Plugin: Adding static route %s -> %s", pluginRoute, route.Path)
mapStatic(m, route.Path, "", pluginRoute)
}
mapStatic(m, setting.StaticRootPath, "", "public")
mapStatic(m, setting.StaticRootPath, "app", "app")
mapStatic(m, setting.StaticRootPath, "css", "css")
@@ -36,11 +42,6 @@ func newMacaron() *macaron.Macaron {
mapStatic(m, setting.StaticRootPath, "fonts", "fonts")
mapStatic(m, setting.StaticRootPath, "robots.txt", "robots.txt")
for _, route := range plugins.StaticRoutes {
log.Info("Adding plugin static route %s -> %s", route.Url, route.Path)
mapStatic(m, route.Path, "", route.Url)
}
m.Use(macaron.Renderer(macaron.RenderOptions{
Directory: path.Join(setting.StaticRootPath, "views"),
IndentJSON: macaron.Env != macaron.PROD,
+11 -16
View File
@@ -16,22 +16,21 @@ type DataSourcePlugin struct {
}
type StaticRootConfig struct {
Url string `json:"url"`
Path string `json:"path"`
PluginRoot string `json:"-"`
Url string `json:"url"`
Path string `json:"path"`
}
type ExternalPluginRoute struct {
Path string `json:"path"`
Method string `json:"method"`
ReqSignedIn bool `json:"req_signed_in"`
ReqGrafanaAdmin bool `json:"req_grafana_admin"`
ReqRole models.RoleType `json:"req_role"`
ReqSignedIn bool `json:"reqSignedIn"`
ReqGrafanaAdmin bool `json:"reqGrafanaAdmin"`
ReqRole models.RoleType `json:"reqRole"`
Url string `json:"url"`
}
type ExternalPluginJs struct {
Src string `json:"src"`
Module string `json:"module"`
}
type ExternalPluginMenuItem struct {
@@ -44,14 +43,10 @@ type ExternalPluginCss struct {
Href string `json:"href"`
}
type ExternalPluginSettings struct {
Routes []*ExternalPluginRoute `json:"routes"`
Js []*ExternalPluginJs `json:"js"`
Css []*ExternalPluginCss `json:"css"`
MenuItems []*ExternalPluginMenuItem `json:"menu_items"`
}
type ExternalPlugin struct {
PluginType string `json:"pluginType"`
Settings ExternalPluginSettings `json:"settings"`
Routes []*ExternalPluginRoute `json:"routes"`
Js []*ExternalPluginJs `json:"js"`
Css []*ExternalPluginCss `json:"css"`
MenuItems []*ExternalPluginMenuItem `json:"menuItems"`
StaticRootConfig *StaticRootConfig `json:"staticRoot"`
}
+11 -7
View File
@@ -38,7 +38,7 @@ func checkExternalPluginPaths() error {
if strings.HasPrefix(section.Name(), "plugin.") {
path := section.Key("path").String()
if path != "" {
log.Info("Plugin: scaning specific dir %s", path)
log.Info("Plugin: Scaning dir %s", path)
scan(path)
}
}
@@ -81,6 +81,13 @@ func (scanner *PluginScanner) walker(currentPath string, f os.FileInfo, err erro
return nil
}
func addStaticRoot(staticRootConfig *StaticRootConfig, currentDir string) {
if staticRootConfig != nil {
staticRootConfig.Path = path.Join(currentDir, staticRootConfig.Path)
StaticRoutes = append(StaticRoutes, staticRootConfig)
}
}
func (scanner *PluginScanner) loadPluginJson(pluginJsonFilePath string) error {
currentDir := filepath.Dir(pluginJsonFilePath)
reader, err := os.Open(pluginJsonFilePath)
@@ -114,20 +121,17 @@ func (scanner *PluginScanner) loadPluginJson(pluginJsonFilePath string) error {
}
DataSources[p.Type] = p
if p.StaticRootConfig != nil {
p.StaticRootConfig.Path = path.Join(currentDir, p.StaticRootConfig.Path)
StaticRoutes = append(StaticRoutes, p.StaticRootConfig)
}
addStaticRoot(p.StaticRootConfig, currentDir)
}
if pluginType == "externalPlugin" {
if pluginType == "external" {
p := ExternalPlugin{}
reader.Seek(0, 0)
if err := jsonParser.Decode(&p); err != nil {
return err
}
ExternalPlugins = append(ExternalPlugins, p)
addStaticRoot(p.StaticRootConfig, currentDir)
}
return nil