frontendsettings.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package api
  2. import (
  3. "strconv"
  4. "github.com/grafana/grafana/pkg/bus"
  5. "github.com/grafana/grafana/pkg/middleware"
  6. m "github.com/grafana/grafana/pkg/models"
  7. "github.com/grafana/grafana/pkg/setting"
  8. )
  9. func getFrontendSettingsMap(c *middleware.Context) (map[string]interface{}, error) {
  10. orgDataSources := make([]*m.DataSource, 0)
  11. if c.IsSignedIn {
  12. query := m.GetDataSourcesQuery{OrgId: c.OrgId}
  13. err := bus.Dispatch(&query)
  14. if err != nil {
  15. return nil, err
  16. }
  17. orgDataSources = query.Result
  18. }
  19. datasources := make(map[string]interface{})
  20. for _, ds := range orgDataSources {
  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_08 {
  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_INFLUXDB {
  38. if ds.Access == m.DS_ACCESS_DIRECT {
  39. dsMap["username"] = ds.User
  40. dsMap["password"] = ds.Password
  41. dsMap["database"] = ds.Database
  42. dsMap["url"] = url
  43. }
  44. }
  45. if ds.Type == m.DS_ES {
  46. dsMap["index"] = ds.Database
  47. }
  48. datasources[ds.Name] = dsMap
  49. }
  50. // add grafana backend data source
  51. datasources["grafana"] = map[string]interface{}{
  52. "type": "grafana",
  53. "grafanaDB": true,
  54. }
  55. jsonObj := map[string]interface{}{
  56. "datasources": datasources,
  57. "appSubUrl": setting.AppSubUrl,
  58. "buildInfo": map[string]interface{}{
  59. "version": setting.BuildVersion,
  60. "commit": setting.BuildCommit,
  61. "buildstamp": setting.BuildStamp,
  62. },
  63. }
  64. return jsonObj, nil
  65. }
  66. func GetFrontendSettings(c *middleware.Context) {
  67. settings, err := getFrontendSettingsMap(c)
  68. if err != nil {
  69. c.JsonApiErr(400, "Failed to get frontend settings", err)
  70. return
  71. }
  72. c.JSON(200, settings)
  73. }