mirror of
https://github.com/grafana/grafana.git
synced 2025-08-02 18:52:04 +08:00
InfluxDB now works in proxy mode, influxdb username and password is added in the backend and never exposed to frontend, #8
This commit is contained in:
53
pkg/api/dataproxy.go
Normal file
53
pkg/api/dataproxy.go
Normal file
@ -0,0 +1,53 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
|
||||
"github.com/torkelo/grafana-pro/pkg/bus"
|
||||
"github.com/torkelo/grafana-pro/pkg/middleware"
|
||||
m "github.com/torkelo/grafana-pro/pkg/models"
|
||||
"github.com/torkelo/grafana-pro/pkg/utils"
|
||||
)
|
||||
|
||||
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 {
|
||||
req.URL.Path = utils.JoinUrlFragments(target.Path, "db/"+ds.Database+"/"+proxyPath)
|
||||
reqQueryVals.Add("u", ds.User)
|
||||
reqQueryVals.Add("p", ds.Password)
|
||||
req.URL.RawQuery = reqQueryVals.Encode()
|
||||
} else {
|
||||
req.URL.Path = utils.JoinUrlFragments(target.Path, proxyPath)
|
||||
}
|
||||
}
|
||||
|
||||
return &httputil.ReverseProxy{Director: director}
|
||||
}
|
||||
|
||||
// TODO: need to cache datasources
|
||||
func ProxyDataSourceRequest(c *middleware.Context) {
|
||||
id := c.ParamsInt64(":id")
|
||||
|
||||
query := m.GetDataSourceByIdQuery{
|
||||
Id: id,
|
||||
AccountId: c.GetAccountId(),
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
Reference in New Issue
Block a user