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 i, 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. }
  29. if ds.Type == m.DS_INFLUXDB {
  30. if ds.Access == m.DS_ACCESS_DIRECT {
  31. dsMap["username"] = ds.User
  32. dsMap["password"] = ds.Password
  33. dsMap["url"] = url + "/db/" + ds.Database
  34. }
  35. }
  36. // temp hack, first is always default
  37. // TODO: implement default ds account setting
  38. if i == 0 {
  39. dsMap["default"] = true
  40. }
  41. datasources[ds.Name] = dsMap
  42. }
  43. // add grafana backend data source
  44. datasources["grafana"] = map[string]interface{}{
  45. "type": "grafana",
  46. "grafanaDB": true,
  47. }
  48. jsonObj := map[string]interface{}{
  49. "datasources": datasources,
  50. "appSubUrl": setting.AppSubUrl,
  51. }
  52. return jsonObj, nil
  53. }