mirror of
				https://github.com/grafana/grafana.git
				synced 2025-02-25 18:55:37 -06:00 
			
		
		
		
	Trying a different approach to providing frontend settings
This commit is contained in:
		
							
								
								
									
										2
									
								
								grafana
									
									
									
									
									
								
							
							
								
								
								
								
								
							
						
						
									
										2
									
								
								grafana
									
									
									
									
									
								
							 Submodule grafana updated: dede578c7d...cfabccc5f2
									
								
							@@ -54,7 +54,14 @@ func Register(m *macaron.Macaron) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Index(ctx *middleware.Context) {
 | 
			
		||||
	ctx.Data["User"] = dtos.NewCurrentUser(ctx.UserAccount)
 | 
			
		||||
	settings, err := getFrontendSettings(ctx.GetAccountId())
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		ctx.Handle(500, "Failed to get settings", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ctx.Data["user"] = dtos.NewCurrentUser(ctx.UserAccount)
 | 
			
		||||
	ctx.Data["settings"] = settings
 | 
			
		||||
	ctx.HTML(200, "index")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										61
									
								
								pkg/api/api_frontendsettings.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								pkg/api/api_frontendsettings.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,61 @@
 | 
			
		||||
package api
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"strconv"
 | 
			
		||||
 | 
			
		||||
	"github.com/torkelo/grafana-pro/pkg/bus"
 | 
			
		||||
	m "github.com/torkelo/grafana-pro/pkg/models"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func getFrontendSettings(accountId int64) (map[string]interface{}, error) {
 | 
			
		||||
	query := m.GetDataSourcesQuery{AccountId: accountId}
 | 
			
		||||
	err := bus.Dispatch(&query)
 | 
			
		||||
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	datasources := make(map[string]interface{})
 | 
			
		||||
 | 
			
		||||
	for i, ds := range query.Result {
 | 
			
		||||
		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,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return jsonObj, nil
 | 
			
		||||
}
 | 
			
		||||
@@ -46,7 +46,8 @@
 | 
			
		||||
 | 
			
		||||
	<script>
 | 
			
		||||
		window.grafanaBootData = {
 | 
			
		||||
		  user:[[.User]]
 | 
			
		||||
			user:[[.user]],
 | 
			
		||||
			settings: [[.settings]]
 | 
			
		||||
		};
 | 
			
		||||
	</script>
 | 
			
		||||
</html>
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user