dataproxy.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package api
  2. import (
  3. "github.com/grafana/grafana/pkg/api/pluginproxy"
  4. "github.com/grafana/grafana/pkg/metrics"
  5. m "github.com/grafana/grafana/pkg/models"
  6. "github.com/grafana/grafana/pkg/plugins"
  7. )
  8. func (hs *HTTPServer) ProxyDataSourceRequest(c *m.ReqContext) {
  9. c.TimeRequest(metrics.M_DataSource_ProxyReq_Timer)
  10. dsId := c.ParamsInt64(":id")
  11. ds, err := hs.DatasourceCache.GetDatasource(dsId, c.SignedInUser, c.SkipCache)
  12. if err != nil {
  13. if err == m.ErrDataSourceAccessDenied {
  14. c.JsonApiErr(403, "Access denied to datasource", err)
  15. return
  16. }
  17. c.JsonApiErr(500, "Unable to load datasource meta data", err)
  18. return
  19. }
  20. // find plugin
  21. plugin, ok := plugins.DataSources[ds.Type]
  22. if !ok {
  23. c.JsonApiErr(500, "Unable to find datasource plugin", err)
  24. return
  25. }
  26. // macaron does not include trailing slashes when resolving a wildcard path
  27. proxyPath := ensureProxyPathTrailingSlash(c.Req.URL.Path, c.Params("*"))
  28. proxy := pluginproxy.NewDataSourceProxy(ds, plugin, c, proxyPath)
  29. proxy.HandleRequest()
  30. }
  31. // ensureProxyPathTrailingSlash Check for a trailing slash in original path and makes
  32. // sure that a trailing slash is added to proxy path, if not already exists.
  33. func ensureProxyPathTrailingSlash(originalPath, proxyPath string) string {
  34. if len(proxyPath) > 1 {
  35. if originalPath[len(originalPath)-1] == '/' && proxyPath[len(proxyPath)-1] != '/' {
  36. return proxyPath + "/"
  37. }
  38. }
  39. return proxyPath
  40. }