grafana/pkg/api/dataproxy.go

49 lines
1.4 KiB
Go
Raw Normal View History

package api
import (
"github.com/grafana/grafana/pkg/api/pluginproxy"
"github.com/grafana/grafana/pkg/infra/metrics"
2015-02-05 03:37:13 -06:00
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/plugins"
)
2018-03-22 16:13:46 -05:00
func (hs *HTTPServer) ProxyDataSourceRequest(c *m.ReqContext) {
c.TimeRequest(metrics.MDataSourceProxyReqTimer)
dsId := c.ParamsInt64(":id")
2018-10-26 03:40:33 -05:00
ds, err := hs.DatasourceCache.GetDatasource(dsId, c.SignedInUser, c.SkipCache)
2015-10-08 10:30:13 -05:00
if err != nil {
2018-10-26 03:40:33 -05:00
if err == m.ErrDataSourceAccessDenied {
2018-10-26 11:34:10 -05:00
c.JsonApiErr(403, "Access denied to datasource", err)
2018-10-26 03:40:33 -05:00
return
}
c.JsonApiErr(500, "Unable to load datasource meta data", err)
return
}
// find plugin
plugin, ok := plugins.DataSources[ds.Type]
if !ok {
c.JsonApiErr(500, "Unable to find datasource plugin", err)
return
}
2018-09-19 12:02:04 -05:00
// macaron does not include trailing slashes when resolving a wildcard path
proxyPath := ensureProxyPathTrailingSlash(c.Req.URL.Path, c.Params("*"))
proxy := pluginproxy.NewDataSourceProxy(ds, plugin, c, proxyPath, hs.Cfg)
proxy.HandleRequest()
}
// ensureProxyPathTrailingSlash Check for a trailing slash in original path and makes
// sure that a trailing slash is added to proxy path, if not already exists.
func ensureProxyPathTrailingSlash(originalPath, proxyPath string) string {
2018-09-18 13:16:09 -05:00
if len(proxyPath) > 1 {
if originalPath[len(originalPath)-1] == '/' && proxyPath[len(proxyPath)-1] != '/' {
return proxyPath + "/"
2018-09-18 13:16:09 -05:00
}
}
return proxyPath
}