2014-12-29 06:36:08 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
"net/url"
|
|
|
|
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2014-12-29 06:36:08 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewReverseProxy(ds *m.DataSource, proxyPath string) *httputil.ReverseProxy {
|
|
|
|
target, _ := url.Parse(ds.Url)
|
|
|
|
|
|
|
|
director := func(req *http.Request) {
|
|
|
|
req.URL.Scheme = target.Scheme
|
|
|
|
req.URL.Host = target.Host
|
|
|
|
|
|
|
|
reqQueryVals := req.URL.Query()
|
|
|
|
|
|
|
|
if ds.Type == m.DS_INFLUXDB {
|
2015-01-08 02:00:00 -06:00
|
|
|
req.URL.Path = util.JoinUrlFragments(target.Path, "db/"+ds.Database+"/"+proxyPath)
|
2014-12-29 06:36:08 -06:00
|
|
|
reqQueryVals.Add("u", ds.User)
|
|
|
|
reqQueryVals.Add("p", ds.Password)
|
|
|
|
req.URL.RawQuery = reqQueryVals.Encode()
|
|
|
|
} else {
|
2015-01-08 02:00:00 -06:00
|
|
|
req.URL.Path = util.JoinUrlFragments(target.Path, proxyPath)
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &httputil.ReverseProxy{Director: director}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: need to cache datasources
|
|
|
|
func ProxyDataSourceRequest(c *middleware.Context) {
|
|
|
|
id := c.ParamsInt64(":id")
|
|
|
|
|
|
|
|
query := m.GetDataSourceByIdQuery{
|
|
|
|
Id: id,
|
2015-01-19 11:01:04 -06:00
|
|
|
AccountId: c.AccountId,
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
err := bus.Dispatch(&query)
|
|
|
|
if err != nil {
|
|
|
|
c.JsonApiErr(500, "Unable to load datasource meta data", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
proxyPath := c.Params("*")
|
|
|
|
proxy := NewReverseProxy(&query.Result, proxyPath)
|
|
|
|
proxy.ServeHTTP(c.RW(), c.Req.Request)
|
|
|
|
}
|