refer to plugins are ExternalPlugin instead of thirdParty

This commit is contained in:
woodsaj
2015-11-11 14:30:07 +08:00
parent cf89b565a6
commit d503c5d83d
10 changed files with 112 additions and 107 deletions

View File

@@ -185,7 +185,7 @@ func Register(r *macaron.Macaron) {
// rendering
r.Get("/render/*", reqSignedIn, RenderToPng)
InitThirdPartyRoutes(r)
InitExternalPluginRoutes(r)
r.NotFound(NotFoundHandler)
}

View File

@@ -3,16 +3,16 @@ package api
import (
"encoding/json"
"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"
"log"
"net/http"
"net/http/httputil"
"net/url"
)
func InitThirdPartyRoutes(r *macaron.Macaron) {
func InitExternalPluginRoutes(r *macaron.Macaron) {
/*
// Handle Auth and role requirements
if route.ReqSignedIn {
@@ -30,34 +30,33 @@ func InitThirdPartyRoutes(r *macaron.Macaron) {
}
}
*/
for _, integration := range plugins.Integrations {
log.Printf("adding routes for integration")
for _, route := range integration.Routes {
log.Printf("adding route %s %s", route.Method, route.Path)
r.Route(util.JoinUrlFragments("/thirdparty/", route.Path), route.Method, ThirdParty(route.Url))
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))
}
}
}
func ThirdParty(routeUrl string) macaron.Handler {
func ExternalPlugin(routeUrl string) macaron.Handler {
return func(c *middleware.Context) {
path := c.Params("*")
//Create a HTTP header with the context in it.
ctx, err := json.Marshal(c.SignedInUser)
if err != nil {
c.JsonApiErr(500, "Not found", err)
c.JsonApiErr(500, "failed to marshal context to json.", err)
return
}
log.Printf(string(ctx))
targetUrl, _ := url.Parse(routeUrl)
proxy := NewThirdPartyProxy(string(ctx), path, targetUrl)
proxy := NewExternalPluginProxy(string(ctx), path, targetUrl)
proxy.Transport = dataProxyTransport
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) {
req.URL.Scheme = targetUrl.Scheme
req.URL.Host = targetUrl.Host

View File

@@ -52,25 +52,25 @@ func setIndexViewData(c *middleware.Context) error {
if setting.GoogleTagManagerId != "" {
c.Data["GoogleTagManagerId"] = setting.GoogleTagManagerId
}
// This can be loaded from the DB/file to allow 3rdParty integration
thirdPartyJs := make([]string, 0)
thirdPartyCss := make([]string, 0)
thirdPartyMenu := make([]*plugins.ThirdPartyMenuItem, 0)
for _, integration := range plugins.Integrations {
for _, js := range integration.Js {
thirdPartyJs = append(thirdPartyJs, js.Src)
externalPluginJs := make([]string, 0)
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 _, css := range integration.Css {
thirdPartyCss = append(thirdPartyCss, css.Href)
for _, css := range plugin.Settings.Css {
externalPluginCss = append(externalPluginCss, css.Href)
}
for _, item := range integration.MenuItems {
thirdPartyMenu = append(thirdPartyMenu, item)
for _, item := range plugin.Settings.MenuItems {
externalPluginMenu = append(externalPluginMenu, item)
}
}
c.Data["ThirdPartyJs"] = thirdPartyJs
c.Data["ThirdPartyCss"] = thirdPartyCss
c.Data["ThirdPartyMenu"] = thirdPartyMenu
c.Data["ExternalPluginJs"] = externalPluginJs
c.Data["ExternalPluginCss"] = externalPluginCss
c.Data["ExternalPluginMenu"] = externalPluginMenu
return nil
}

View File

@@ -1,6 +1,6 @@
package models
type ThirdPartyRoute struct {
type ExternalPluginRoute struct {
Path string `json:"path"`
Method string `json:"method"`
ReqSignedIn bool `json:"req_signed_in"`
@@ -9,23 +9,23 @@ type ThirdPartyRoute struct {
Url string `json:"url"`
}
type ThirdPartyJs struct {
type ExternalPluginJs struct {
src string `json:"src"`
}
type ThirdPartyMenuItem struct {
type ExternalPluginMenuItem struct {
Text string `json:"text"`
Icon string `json:"icon"`
Href string `json:"href"`
}
type ThirdPartyCss struct {
type ExternalPluginCss struct {
Href string `json:"href"`
}
type ThirdPartyIntegration struct {
Routes []*ThirdPartyRoute `json:"routes"`
Js []*ThirdPartyJs `json:"js"`
Css []*ThirdPartyCss `json:"css"`
MenuItems []*ThirdPartyMenuItem `json:"menu_items"`
type ExternalPluginIntegration struct {
Routes []*ExternalPluginRoute `json:"routes"`
Js []*ExternalPluginJs `json:"js"`
Css []*ExternalPluginCss `json:"css"`
MenuItems []*ExternalPluginMenuItem `json:"menu_items"`
}

View File

@@ -16,7 +16,7 @@ type PluginMeta struct {
Name string `json:"name"`
}
type ThirdPartyRoute struct {
type ExternalPluginRoute struct {
Path string `json:"path"`
Method string `json:"method"`
ReqSignedIn bool `json:"req_signed_in"`
@@ -25,35 +25,35 @@ type ThirdPartyRoute struct {
Url string `json:"url"`
}
type ThirdPartyJs struct {
type ExternalPluginJs struct {
Src string `json:"src"`
}
type ThirdPartyMenuItem struct {
type ExternalPluginMenuItem struct {
Text string `json:"text"`
Icon string `json:"icon"`
Href string `json:"href"`
}
type ThirdPartyCss struct {
type ExternalPluginCss struct {
Href string `json:"href"`
}
type ThirdPartyIntegration struct {
Routes []*ThirdPartyRoute `json:"routes"`
Js []*ThirdPartyJs `json:"js"`
Css []*ThirdPartyCss `json:"css"`
MenuItems []*ThirdPartyMenuItem `json:"menu_items"`
type ExternalPluginSettings struct {
Routes []*ExternalPluginRoute `json:"routes"`
Js []*ExternalPluginJs `json:"js"`
Css []*ExternalPluginCss `json:"css"`
MenuItems []*ExternalPluginMenuItem `json:"menu_items"`
}
type ThirdPartyPlugin struct {
PluginType string `json:"pluginType"`
Integration ThirdPartyIntegration `json:"integration"`
type ExternalPlugin struct {
PluginType string `json:"pluginType"`
Settings ExternalPluginSettings `json:"settings"`
}
var (
DataSources map[string]interface{}
Integrations []ThirdPartyIntegration
DataSources map[string]interface{}
ExternalPlugins []ExternalPlugin
)
type PluginScanner struct {
@@ -67,7 +67,7 @@ func Init() {
func scan(pluginDir string) error {
DataSources = make(map[string]interface{})
Integrations = make([]ThirdPartyIntegration, 0)
ExternalPlugins = make([]ExternalPlugin, 0)
scanner := &PluginScanner{
pluginPath: pluginDir,
@@ -131,13 +131,13 @@ func (scanner *PluginScanner) loadPluginJson(path string) error {
DataSources[datasourceType.(string)] = pluginJson
}
if pluginType == "thirdPartyIntegration" {
p := ThirdPartyPlugin{}
if pluginType == "externalPlugin" {
p := ExternalPlugin{}
reader.Seek(0, 0)
if err := jsonParser.Decode(&p); err != nil {
return err
}
Integrations = append(Integrations, p.Integration)
ExternalPlugins = append(ExternalPlugins, p)
}
return nil