Merge branch 'externalPlugin' of https://github.com/raintank/grafana into external-plugins
This commit is contained in:
@@ -187,5 +187,7 @@ func Register(r *macaron.Macaron) {
|
||||
// rendering
|
||||
r.Get("/render/*", reqSignedIn, RenderToPng)
|
||||
|
||||
InitExternalPluginRoutes(r)
|
||||
|
||||
r.NotFound(NotFoundHandler)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
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"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
func InitExternalPluginRoutes(r *macaron.Macaron) {
|
||||
/*
|
||||
// Handle Auth and role requirements
|
||||
if route.ReqSignedIn {
|
||||
c.Invoke(middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true}))
|
||||
}
|
||||
if route.ReqGrafanaAdmin {
|
||||
c.Invoke(middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true, ReqGrafanaAdmin: true}))
|
||||
}
|
||||
if route.ReqRole != nil {
|
||||
if *route.ReqRole == m.ROLE_EDITOR {
|
||||
c.Invoke(middleware.RoleAuth(m.ROLE_EDITOR, m.ROLE_ADMIN))
|
||||
}
|
||||
if *route.ReqRole == m.ROLE_ADMIN {
|
||||
c.Invoke(middleware.RoleAuth(m.ROLE_ADMIN))
|
||||
}
|
||||
}
|
||||
*/
|
||||
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 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, "failed to marshal context to json.", err)
|
||||
return
|
||||
}
|
||||
targetUrl, _ := url.Parse(routeUrl)
|
||||
proxy := NewExternalPluginProxy(string(ctx), path, targetUrl)
|
||||
proxy.Transport = dataProxyTransport
|
||||
proxy.ServeHTTP(c.RW(), c.Req.Request)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
req.Host = targetUrl.Host
|
||||
|
||||
req.URL.Path = util.JoinUrlFragments(targetUrl.Path, proxyPath)
|
||||
|
||||
// clear cookie headers
|
||||
req.Header.Del("Cookie")
|
||||
req.Header.Del("Set-Cookie")
|
||||
req.Header.Add("Grafana-Context", ctx)
|
||||
}
|
||||
|
||||
return &httputil.ReverseProxy{Director: director}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package api
|
||||
import (
|
||||
"github.com/grafana/grafana/pkg/api/dtos"
|
||||
"github.com/grafana/grafana/pkg/middleware"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
@@ -52,6 +53,25 @@ func setIndexViewData(c *middleware.Context) error {
|
||||
c.Data["GoogleTagManagerId"] = setting.GoogleTagManagerId
|
||||
}
|
||||
|
||||
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 plugin.Settings.Css {
|
||||
externalPluginCss = append(externalPluginCss, css.Href)
|
||||
}
|
||||
for _, item := range plugin.Settings.MenuItems {
|
||||
externalPluginMenu = append(externalPluginMenu, item)
|
||||
}
|
||||
|
||||
}
|
||||
c.Data["ExternalPluginJs"] = externalPluginJs
|
||||
c.Data["ExternalPluginCss"] = externalPluginCss
|
||||
c.Data["ExternalPluginMenu"] = externalPluginMenu
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user