Commit 1a708da1 by Torkel Ödegaard

Merge pull request #3821 from raintank/apiPlugin

merge apiPlugins with appPlugins
parents d9d4e6c8 f94599cd
......@@ -215,7 +215,7 @@ func Register(r *macaron.Macaron) {
// rendering
r.Get("/render/*", reqSignedIn, RenderToPng)
InitApiPluginRoutes(r)
InitAppPluginRoutes(r)
r.NotFound(NotFoundHandler)
}
......@@ -19,10 +19,10 @@ import (
"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 {
log.Info("Plugin: Adding proxy route for app plugin")
url := util.JoinUrlFragments("/api/plugin-proxy/", route.Path)
handlers := make([]macaron.Handler, 0)
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, ApiPlugin(route, plugin.IncludedInAppId))
handlers = append(handlers, AppPluginRoute(route, plugin.Id))
r.Route(url, route.Method, handlers...)
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) {
path := c.Params("*")
proxy := NewApiPluginProxy(c, path, route, includedInAppId)
proxy := NewApiPluginProxy(c, path, route, appId)
proxy.Transport = dataProxyTransport
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)
director := func(req *http.Request) {
......@@ -87,21 +87,15 @@ func NewApiPluginProxy(ctx *middleware.Context, proxyPath string, route *plugins
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 {
ctx.JsonApiErr(500, fmt.Sprintf("failed to execute header content template for header %s.", header.Name), err)
return
......
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
}
......@@ -26,14 +26,30 @@ type AppIncludeInfo struct {
type AppPlugin struct {
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:"-"`
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 {
if err := decoder.Decode(&app); err != nil {
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
return nil
}
......@@ -45,7 +45,6 @@ type PluginStaticRoute struct {
type EnabledPlugins struct {
Panels []*PanelPlugin
DataSources map[string]*DataSourcePlugin
ApiList []*ApiPlugin
Apps []*AppPlugin
}
......@@ -53,7 +52,6 @@ func NewEnabledPlugins() EnabledPlugins {
return EnabledPlugins{
Panels: make([]*PanelPlugin, 0),
DataSources: make(map[string]*DataSourcePlugin),
ApiList: make([]*ApiPlugin, 0),
Apps: make([]*AppPlugin, 0),
}
}
......@@ -17,7 +17,6 @@ import (
var (
DataSources map[string]*DataSourcePlugin
Panels map[string]*PanelPlugin
ApiPlugins map[string]*ApiPlugin
StaticRoutes []*PluginStaticRoute
Apps map[string]*AppPlugin
PluginTypes map[string]interface{}
......@@ -30,14 +29,12 @@ type PluginScanner struct {
func Init() error {
DataSources = make(map[string]*DataSourcePlugin)
ApiPlugins = make(map[string]*ApiPlugin)
StaticRoutes = make([]*PluginStaticRoute, 0)
Panels = make(map[string]*PanelPlugin)
Apps = make(map[string]*AppPlugin)
PluginTypes = map[string]interface{}{
"panel": PanelPlugin{},
"datasource": DataSourcePlugin{},
"api": ApiPlugin{},
"app": AppPlugin{},
}
......
......@@ -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
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment