Browse Source

merge apiPlugins with appPlugins

Anthony Woods 10 years ago
parent
commit
f94599cd29

+ 1 - 1
pkg/api/api.go

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

+ 13 - 19
pkg/api/api_plugin.go → pkg/api/app_routes.go

@@ -19,10 +19,10 @@ import (
 	"github.com/grafana/grafana/pkg/util"
 	"github.com/grafana/grafana/pkg/util"
 )
 )
 
 
-func InitApiPluginRoutes(r *macaron.Macaron) {
-	for _, plugin := range plugins.ApiPlugins {
-		log.Info("Plugin: Adding proxy routes for api plugin")
+func InitAppPluginRoutes(r *macaron.Macaron) {
+	for _, plugin := range plugins.Apps {
 		for _, route := range plugin.Routes {
 		for _, route := range plugin.Routes {
+			log.Info("Plugin: Adding proxy route for app plugin")
 			url := util.JoinUrlFragments("/api/plugin-proxy/", route.Path)
 			url := util.JoinUrlFragments("/api/plugin-proxy/", route.Path)
 			handlers := make([]macaron.Handler, 0)
 			handlers := make([]macaron.Handler, 0)
 			if route.ReqSignedIn {
 			if route.ReqSignedIn {
@@ -38,24 +38,24 @@ func InitApiPluginRoutes(r *macaron.Macaron) {
 					handlers = append(handlers, middleware.RoleAuth(m.ROLE_EDITOR, m.ROLE_ADMIN))
 					handlers = append(handlers, middleware.RoleAuth(m.ROLE_EDITOR, m.ROLE_ADMIN))
 				}
 				}
 			}
 			}
-			handlers = append(handlers, ApiPlugin(route, plugin.IncludedInAppId))
+			handlers = append(handlers, AppPluginRoute(route, plugin.Id))
 			r.Route(url, route.Method, handlers...)
 			r.Route(url, route.Method, handlers...)
 			log.Info("Plugin: Adding route %s", url)
 			log.Info("Plugin: Adding route %s", url)
 		}
 		}
 	}
 	}
 }
 }
 
 
-func ApiPlugin(route *plugins.ApiPluginRoute, includedInAppId string) macaron.Handler {
+func AppPluginRoute(route *plugins.AppPluginRoute, appId string) macaron.Handler {
 	return func(c *middleware.Context) {
 	return func(c *middleware.Context) {
 		path := c.Params("*")
 		path := c.Params("*")
 
 
-		proxy := NewApiPluginProxy(c, path, route, includedInAppId)
+		proxy := NewApiPluginProxy(c, path, route, appId)
 		proxy.Transport = dataProxyTransport
 		proxy.Transport = dataProxyTransport
 		proxy.ServeHTTP(c.Resp, c.Req.Request)
 		proxy.ServeHTTP(c.Resp, c.Req.Request)
 	}
 	}
 }
 }
 
 
-func NewApiPluginProxy(ctx *middleware.Context, proxyPath string, route *plugins.ApiPluginRoute, includedInAppId string) *httputil.ReverseProxy {
+func NewApiPluginProxy(ctx *middleware.Context, proxyPath string, route *plugins.AppPluginRoute, appId string) *httputil.ReverseProxy {
 	targetUrl, _ := url.Parse(route.Url)
 	targetUrl, _ := url.Parse(route.Url)
 
 
 	director := func(req *http.Request) {
 	director := func(req *http.Request) {
@@ -87,21 +87,15 @@ func NewApiPluginProxy(ctx *middleware.Context, proxyPath string, route *plugins
 				return
 				return
 			}
 			}
 
 
-			jsonData := make(map[string]interface{})
+			//lookup appSettings
+			query := m.GetAppSettingByAppIdQuery{OrgId: ctx.OrgId, AppId: appId}
 
 
-			if includedInAppId != "" {
-				//lookup appSettings
-				query := m.GetAppSettingByAppIdQuery{OrgId: ctx.OrgId, AppId: includedInAppId}
-
-				if err := bus.Dispatch(&query); err != nil {
-					ctx.JsonApiErr(500, "failed to get AppSettings of includedAppId.", err)
-					return
-				}
-
-				jsonData = query.Result.JsonData
+			if err := bus.Dispatch(&query); err != nil {
+				ctx.JsonApiErr(500, "failed to get AppSettings.", err)
+				return
 			}
 			}
 
 
-			err = t.Execute(&contentBuf, jsonData)
+			err = t.Execute(&contentBuf, query.Result.JsonData)
 			if err != nil {
 			if err != nil {
 				ctx.JsonApiErr(500, fmt.Sprintf("failed to execute header content template for header %s.", header.Name), err)
 				ctx.JsonApiErr(500, fmt.Sprintf("failed to execute header content template for header %s.", header.Name), err)
 				return
 				return

+ 0 - 38
pkg/plugins/api_plugin.go

@@ -1,38 +0,0 @@
-package plugins
-
-import (
-	"encoding/json"
-
-	"github.com/grafana/grafana/pkg/models"
-)
-
-type ApiPluginRoute struct {
-	Path            string            `json:"path"`
-	Method          string            `json:"method"`
-	ReqSignedIn     bool              `json:"reqSignedIn"`
-	ReqGrafanaAdmin bool              `json:"reqGrafanaAdmin"`
-	ReqRole         models.RoleType   `json:"reqRole"`
-	Url             string            `json:"url"`
-	Headers         []ApiPluginHeader `json:"headers"`
-}
-
-type ApiPlugin struct {
-	PluginBase
-	Routes []*ApiPluginRoute `json:"routes"`
-}
-
-type ApiPluginHeader struct {
-	Name    string `json:"name"`
-	Content string `json:"content"`
-}
-
-func (app *ApiPlugin) Load(decoder *json.Decoder, pluginDir string) error {
-	if err := decoder.Decode(&app); err != nil {
-		return err
-	}
-
-	app.PluginDir = pluginDir
-
-	ApiPlugins[app.Id] = app
-	return nil
-}

+ 19 - 15
pkg/plugins/app_plugin.go

@@ -26,14 +26,30 @@ type AppIncludeInfo struct {
 
 
 type AppPlugin struct {
 type AppPlugin struct {
 	FrontendPluginBase
 	FrontendPluginBase
-	Css      *AppPluginCss    `json:"css"`
-	Pages    []AppPluginPage  `json:"pages"`
-	Includes []AppIncludeInfo `json:"-"`
+	Css      *AppPluginCss     `json:"css"`
+	Pages    []AppPluginPage   `json:"pages"`
+	Routes   []*AppPluginRoute `json:"routes"`
+	Includes []AppIncludeInfo  `json:"-"`
 
 
 	Pinned  bool `json:"-"`
 	Pinned  bool `json:"-"`
 	Enabled bool `json:"-"`
 	Enabled bool `json:"-"`
 }
 }
 
 
+type AppPluginRoute struct {
+	Path            string                 `json:"path"`
+	Method          string                 `json:"method"`
+	ReqSignedIn     bool                   `json:"reqSignedIn"`
+	ReqGrafanaAdmin bool                   `json:"reqGrafanaAdmin"`
+	ReqRole         models.RoleType        `json:"reqRole"`
+	Url             string                 `json:"url"`
+	Headers         []AppPluginRouteHeader `json:"headers"`
+}
+
+type AppPluginRouteHeader struct {
+	Name    string `json:"name"`
+	Content string `json:"content"`
+}
+
 func (app *AppPlugin) Load(decoder *json.Decoder, pluginDir string) error {
 func (app *AppPlugin) Load(decoder *json.Decoder, pluginDir string) error {
 	if err := decoder.Decode(&app); err != nil {
 	if err := decoder.Decode(&app); err != nil {
 		return err
 		return err
@@ -59,18 +75,6 @@ func (app *AppPlugin) Load(decoder *json.Decoder, pluginDir string) error {
 		}
 		}
 	}
 	}
 
 
-	// check if we have child apiPlugins
-	for _, plugin := range ApiPlugins {
-		if strings.HasPrefix(plugin.PluginDir, app.PluginDir) {
-			plugin.IncludedInAppId = app.Id
-			app.Includes = append(app.Includes, AppIncludeInfo{
-				Name: plugin.Name,
-				Id:   plugin.Id,
-				Type: plugin.Type,
-			})
-		}
-	}
-
 	Apps[app.Id] = app
 	Apps[app.Id] = app
 	return nil
 	return nil
 }
 }

+ 0 - 2
pkg/plugins/models.go

@@ -45,7 +45,6 @@ type PluginStaticRoute struct {
 type EnabledPlugins struct {
 type EnabledPlugins struct {
 	Panels      []*PanelPlugin
 	Panels      []*PanelPlugin
 	DataSources map[string]*DataSourcePlugin
 	DataSources map[string]*DataSourcePlugin
-	ApiList     []*ApiPlugin
 	Apps        []*AppPlugin
 	Apps        []*AppPlugin
 }
 }
 
 
@@ -53,7 +52,6 @@ func NewEnabledPlugins() EnabledPlugins {
 	return EnabledPlugins{
 	return EnabledPlugins{
 		Panels:      make([]*PanelPlugin, 0),
 		Panels:      make([]*PanelPlugin, 0),
 		DataSources: make(map[string]*DataSourcePlugin),
 		DataSources: make(map[string]*DataSourcePlugin),
-		ApiList:     make([]*ApiPlugin, 0),
 		Apps:        make([]*AppPlugin, 0),
 		Apps:        make([]*AppPlugin, 0),
 	}
 	}
 }
 }

+ 0 - 3
pkg/plugins/plugins.go

@@ -17,7 +17,6 @@ import (
 var (
 var (
 	DataSources  map[string]*DataSourcePlugin
 	DataSources  map[string]*DataSourcePlugin
 	Panels       map[string]*PanelPlugin
 	Panels       map[string]*PanelPlugin
-	ApiPlugins   map[string]*ApiPlugin
 	StaticRoutes []*PluginStaticRoute
 	StaticRoutes []*PluginStaticRoute
 	Apps         map[string]*AppPlugin
 	Apps         map[string]*AppPlugin
 	PluginTypes  map[string]interface{}
 	PluginTypes  map[string]interface{}
@@ -30,14 +29,12 @@ type PluginScanner struct {
 
 
 func Init() error {
 func Init() error {
 	DataSources = make(map[string]*DataSourcePlugin)
 	DataSources = make(map[string]*DataSourcePlugin)
-	ApiPlugins = make(map[string]*ApiPlugin)
 	StaticRoutes = make([]*PluginStaticRoute, 0)
 	StaticRoutes = make([]*PluginStaticRoute, 0)
 	Panels = make(map[string]*PanelPlugin)
 	Panels = make(map[string]*PanelPlugin)
 	Apps = make(map[string]*AppPlugin)
 	Apps = make(map[string]*AppPlugin)
 	PluginTypes = map[string]interface{}{
 	PluginTypes = map[string]interface{}{
 		"panel":      PanelPlugin{},
 		"panel":      PanelPlugin{},
 		"datasource": DataSourcePlugin{},
 		"datasource": DataSourcePlugin{},
-		"api":        ApiPlugin{},
 		"app":        AppPlugin{},
 		"app":        AppPlugin{},
 	}
 	}
 
 

+ 0 - 6
pkg/plugins/queries.go

@@ -68,11 +68,5 @@ func GetEnabledPlugins(orgId int64) (*EnabledPlugins, error) {
 		}
 		}
 	}
 	}
 
 
-	for _, api := range ApiPlugins {
-		if isPluginEnabled(api.IncludedInAppId) {
-			enabledPlugins.ApiList = append(enabledPlugins.ApiList, api)
-		}
-	}
-
 	return &enabledPlugins, nil
 	return &enabledPlugins, nil
 }
 }