mirror of https://github.com/grafana/grafana
parent
f3132b4513
commit
ec98c201e4
@ -1 +1 @@ |
||||
Subproject commit cfabccc5f29579680dcd186307c431945900c7ce |
||||
Subproject commit 47f226be3b480e037692f30a320c6fcff2b9e01c |
||||
@ -1,99 +0,0 @@ |
||||
package api |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
"strconv" |
||||
"strings" |
||||
|
||||
"github.com/torkelo/grafana-pro/pkg/bus" |
||||
"github.com/torkelo/grafana-pro/pkg/middleware" |
||||
m "github.com/torkelo/grafana-pro/pkg/models" |
||||
) |
||||
|
||||
const configTemplate = ` |
||||
define(['settings'], |
||||
function (Settings) { |
||||
"use strict"; |
||||
return new Settings(%json%); |
||||
}); |
||||
` |
||||
|
||||
type configJsTmplModel struct { |
||||
DataSources []*m.DataSource |
||||
} |
||||
|
||||
// TODO: cleanup this ugly code
|
||||
func renderConfig(data *configJsTmplModel) string { |
||||
datasources := make(map[string]interface{}) |
||||
|
||||
for i, ds := range data.DataSources { |
||||
url := ds.Url |
||||
|
||||
if ds.Access == m.DS_ACCESS_PROXY { |
||||
url = "/api/datasources/proxy/" + strconv.FormatInt(ds.Id, 10) |
||||
} |
||||
|
||||
var dsMap = map[string]interface{}{ |
||||
"type": ds.Type, |
||||
"url": url, |
||||
} |
||||
|
||||
if ds.Type == m.DS_INFLUXDB { |
||||
if ds.Access == m.DS_ACCESS_DIRECT { |
||||
dsMap["username"] = ds.User |
||||
dsMap["password"] = ds.Password |
||||
dsMap["url"] = url + "/db/" + ds.Database |
||||
} |
||||
} |
||||
|
||||
// temp hack, first is always default
|
||||
// TODO: implement default ds account setting
|
||||
if i == 0 { |
||||
dsMap["default"] = true |
||||
} |
||||
|
||||
datasources[ds.Name] = dsMap |
||||
} |
||||
|
||||
// add grafana backend data source
|
||||
datasources["grafana"] = map[string]interface{}{ |
||||
"type": "grafana", |
||||
"url": "", |
||||
"grafanaDB": true, |
||||
} |
||||
|
||||
jsonObj := map[string]interface{}{ |
||||
"datasources": datasources, |
||||
} |
||||
|
||||
buff, _ := json.Marshal(jsonObj) |
||||
|
||||
return strings.Replace(configTemplate, "%json%", string(buff), 1) |
||||
} |
||||
|
||||
func GetConfigJS(c *middleware.Context) { |
||||
|
||||
query := m.GetDataSourcesQuery{AccountId: c.GetAccountId()} |
||||
err := bus.Dispatch(&query) |
||||
|
||||
if err != nil { |
||||
c.Handle(500, "cold not load data sources", err) |
||||
return |
||||
} |
||||
|
||||
vm := configJsTmplModel{DataSources: query.Result} |
||||
configStr := renderConfig(&vm) |
||||
|
||||
if err != nil { |
||||
c.Handle(500, "Failed to generate config.js", err) |
||||
return |
||||
} |
||||
|
||||
c.Header().Set("Content-Type", "text/javascript; charset=UTF-8") |
||||
c.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") |
||||
c.Header().Set("Pragma", "no-cache") |
||||
c.Header().Set("Expires", "0") |
||||
c.WriteHeader(200) |
||||
|
||||
c.Write([]byte(configStr)) |
||||
} |
||||
Loading…
Reference in new issue