mirror of https://github.com/grafana/grafana
parent
0bcda4a2eb
commit
cf89b565a6
@ -0,0 +1,75 @@ |
||||
package api |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
"github.com/Unknwon/macaron" |
||||
"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) { |
||||
/* |
||||
// 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 _, 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)) |
||||
} |
||||
} |
||||
} |
||||
|
||||
func ThirdParty(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) |
||||
return |
||||
} |
||||
log.Printf(string(ctx)) |
||||
targetUrl, _ := url.Parse(routeUrl) |
||||
proxy := NewThirdPartyProxy(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 { |
||||
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} |
||||
} |
@ -0,0 +1,31 @@ |
||||
package models |
||||
|
||||
type ThirdPartyRoute struct { |
||||
Path string `json:"path"` |
||||
Method string `json:"method"` |
||||
ReqSignedIn bool `json:"req_signed_in"` |
||||
ReqGrafanaAdmin bool `json:"req_grafana_admin"` |
||||
ReqRole RoleType `json:"req_role"` |
||||
Url string `json:"url"` |
||||
} |
||||
|
||||
type ThirdPartyJs struct { |
||||
src string `json:"src"` |
||||
} |
||||
|
||||
type ThirdPartyMenuItem struct { |
||||
Text string `json:"text"` |
||||
Icon string `json:"icon"` |
||||
Href string `json:"href"` |
||||
} |
||||
|
||||
type ThirdPartyCss 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"` |
||||
} |
@ -0,0 +1,40 @@ |
||||
{ |
||||
"pluginType": "thirdPartyIntegration", |
||||
"integration": { |
||||
"routes": [ |
||||
{ |
||||
"path": "/raintank/public/*", |
||||
"method": "*", |
||||
"req_signed_in": false, |
||||
"req_grafana_admin": false, |
||||
"req_role": "Admin", |
||||
"url": "http://localhost:3001/public" |
||||
}, |
||||
{ |
||||
"path": "/raintank/api/*", |
||||
"method": "*", |
||||
"req_signed_in": true, |
||||
"req_grafana_admin": false, |
||||
"req_role": "Admin", |
||||
"url": "http://localhost:3001/api" |
||||
} |
||||
], |
||||
"css": [ |
||||
{ |
||||
"href": "/path/to/file.css" |
||||
} |
||||
], |
||||
"js": [ |
||||
{ |
||||
"src": "/raintank/public/app.js" |
||||
} |
||||
], |
||||
"menu_items": [ |
||||
{ |
||||
"text": "Menu Text", |
||||
"icon": "fa fa-fw fa-database", |
||||
"href": "/raintank/test" |
||||
} |
||||
] |
||||
} |
||||
} |
Loading…
Reference in new issue