frontendsettings.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package api
  2. import (
  3. "strconv"
  4. "github.com/torkelo/grafana-pro/pkg/bus"
  5. "github.com/torkelo/grafana-pro/pkg/middleware"
  6. m "github.com/torkelo/grafana-pro/pkg/models"
  7. "github.com/torkelo/grafana-pro/pkg/setting"
  8. )
  9. func getFrontendSettings(c *middleware.Context) (map[string]interface{}, error) {
  10. accountDataSources := make([]*m.DataSource, 0)
  11. if c.Account != nil {
  12. query := m.GetDataSourcesQuery{AccountId: c.Account.Id}
  13. err := bus.Dispatch(&query)
  14. if err != nil {
  15. return nil, err
  16. }
  17. accountDataSources = query.Result
  18. }
  19. datasources := make(map[string]interface{})
  20. for _, ds := range accountDataSources {
  21. url := ds.Url
  22. if ds.Access == m.DS_ACCESS_PROXY {
  23. url = setting.AppSubUrl + "/api/datasources/proxy/" + strconv.FormatInt(ds.Id, 10)
  24. }
  25. var dsMap = map[string]interface{}{
  26. "type": ds.Type,
  27. "url": url,
  28. "default": ds.IsDefault,
  29. }
  30. if ds.Type == m.DS_INFLUXDB {
  31. if ds.Access == m.DS_ACCESS_DIRECT {
  32. dsMap["username"] = ds.User
  33. dsMap["password"] = ds.Password
  34. dsMap["url"] = url + "/db/" + ds.Database
  35. }
  36. }
  37. datasources[ds.Name] = dsMap
  38. }
  39. // add grafana backend data source
  40. datasources["grafana"] = map[string]interface{}{
  41. "type": "grafana",
  42. "grafanaDB": true,
  43. }
  44. jsonObj := map[string]interface{}{
  45. "datasources": datasources,
  46. "appSubUrl": setting.AppSubUrl,
  47. "buildInfo": map[string]interface{}{
  48. "version": setting.BuildVersion,
  49. "commit": setting.BuildCommit,
  50. "buildstamp": setting.BuildStamp,
  51. },
  52. }
  53. return jsonObj, nil
  54. }