merge apiPlugins with appPlugins

This commit is contained in:
Anthony Woods 2016-01-22 01:15:04 +08:00
parent 28fabadeae
commit f94599cd29
7 changed files with 33 additions and 84 deletions

View File

@ -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)
} }

View File

@ -19,10 +19,10 @@ import (
"github.com/grafana/grafana/pkg/util" "github.com/grafana/grafana/pkg/util"
) )
func InitApiPluginRoutes(r *macaron.Macaron) { func InitAppPluginRoutes(r *macaron.Macaron) {
for _, plugin := range plugins.ApiPlugins { for _, plugin := range plugins.Apps {
log.Info("Plugin: Adding proxy routes for api plugin")
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{})
if includedInAppId != "" {
//lookup appSettings //lookup appSettings
query := m.GetAppSettingByAppIdQuery{OrgId: ctx.OrgId, AppId: includedInAppId} query := m.GetAppSettingByAppIdQuery{OrgId: ctx.OrgId, AppId: appId}
if err := bus.Dispatch(&query); err != nil { if err := bus.Dispatch(&query); err != nil {
ctx.JsonApiErr(500, "failed to get AppSettings of includedAppId.", err) ctx.JsonApiErr(500, "failed to get AppSettings.", err)
return return
} }
jsonData = query.Result.JsonData err = t.Execute(&contentBuf, query.Result.JsonData)
}
err = t.Execute(&contentBuf, 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

View File

@ -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
}

View File

@ -28,12 +28,28 @@ type AppPlugin struct {
FrontendPluginBase FrontendPluginBase
Css *AppPluginCss `json:"css"` Css *AppPluginCss `json:"css"`
Pages []AppPluginPage `json:"pages"` Pages []AppPluginPage `json:"pages"`
Routes []*AppPluginRoute `json:"routes"`
Includes []AppIncludeInfo `json:"-"` 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
} }

View File

@ -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),
} }
} }

View File

@ -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{},
} }

View File

@ -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
} }