Backend can now generate config.js, the very basic stuff, more work needed
This commit is contained in:
@@ -43,6 +43,9 @@ func Register(m *macaron.Macaron) {
|
||||
m.Post("/api/dashboard/", auth, PostDashboard)
|
||||
m.Delete("/api/dashboard/:slug", auth, DeleteDashboard)
|
||||
|
||||
// frontend config
|
||||
m.Get("/frontend/config", auth, GetConfigJS)
|
||||
|
||||
// rendering
|
||||
m.Get("/render/*", auth, RenderToPng)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/torkelo/grafana-pro/pkg/api/dtos"
|
||||
"github.com/torkelo/grafana-pro/pkg/middleware"
|
||||
"github.com/torkelo/grafana-pro/pkg/models"
|
||||
@@ -96,7 +95,7 @@ func SetUsingAccount(c *middleware.Context) {
|
||||
otherAccounts, err := models.GetOtherAccountsFor(c.UserAccount.Id)
|
||||
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{"message": err.Error()})
|
||||
c.JSON(500, utils.DynMap{"message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -116,7 +115,7 @@ func SetUsingAccount(c *middleware.Context) {
|
||||
account.UsingAccountId = usingAccountId
|
||||
err = models.SaveAccount(account)
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{"message": err.Error()})
|
||||
c.JSON(500, utils.DynMap{"message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"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
|
||||
}
|
||||
|
||||
func renderConfig(data *configJsTmplModel) string {
|
||||
datasources := make(map[string]interface{})
|
||||
|
||||
for _, ds := range data.DataSources {
|
||||
datasources[ds.Name] = map[string]interface{}{
|
||||
"type": ds.Type,
|
||||
"url": ds.Url,
|
||||
}
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
Reference in New Issue
Block a user