frontendsettings.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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.IsSignedIn {
  12. query := m.GetDataSourcesQuery{AccountId: c.AccountId}
  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. if ds.Type == m.DS_ES {
  38. dsMap["index"] = ds.Database
  39. }
  40. datasources[ds.Name] = dsMap
  41. }
  42. // add grafana backend data source
  43. datasources["grafana"] = map[string]interface{}{
  44. "type": "grafana",
  45. "grafanaDB": true,
  46. }
  47. jsonObj := map[string]interface{}{
  48. "datasources": datasources,
  49. "appSubUrl": setting.AppSubUrl,
  50. "buildInfo": map[string]interface{}{
  51. "version": setting.BuildVersion,
  52. "commit": setting.BuildCommit,
  53. "buildstamp": setting.BuildStamp,
  54. },
  55. }
  56. return jsonObj, nil
  57. }