mirror of
				https://github.com/grafana/grafana.git
				synced 2025-02-25 18:55:37 -06:00 
			
		
		
		
	rendering
This commit is contained in:
		@@ -3,22 +3,29 @@ package api
 | 
			
		||||
import (
 | 
			
		||||
	"strconv"
 | 
			
		||||
 | 
			
		||||
	log "github.com/alecthomas/log4go"
 | 
			
		||||
	"github.com/gin-gonic/gin"
 | 
			
		||||
	"github.com/torkelo/grafana-pro/pkg/components"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	addRoutes(func(self *HttpServer) {
 | 
			
		||||
		self.addRoute("GET", "/api/render/*url", self.renderToPng)
 | 
			
		||||
		self.addRoute("GET", "/render/*url", self.renderToPng)
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (self *HttpServer) renderToPng(c *gin.Context, auth *authContext) {
 | 
			
		||||
	url := c.Params.ByName("url")
 | 
			
		||||
	accountId := auth.getAccountId()
 | 
			
		||||
	query := c.Request.URL.Query()
 | 
			
		||||
	queryParams := "?render&accountId=" + strconv.Itoa(accountId) + "&" + c.Request.URL.RawQuery
 | 
			
		||||
	renderOpts := &components.RenderOpts{
 | 
			
		||||
		Url:    c.Params.ByName("url") + queryParams,
 | 
			
		||||
		Width:  query["width"][0],
 | 
			
		||||
		Height: query["height"][0],
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	log.Info("Rendering url %v", url)
 | 
			
		||||
	pngPath, err := self.renderer.RenderToPng("http://localhost:3000" + url + "?render&accountId=" + strconv.Itoa(accountId))
 | 
			
		||||
	renderOpts.Url = "http://localhost:3000" + renderOpts.Url
 | 
			
		||||
 | 
			
		||||
	pngPath, err := self.renderer.RenderToPng(renderOpts)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		c.HTML(500, "error.html", nil)
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -17,14 +17,20 @@ type PhantomRenderer struct {
 | 
			
		||||
	PhantomDir string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (self *PhantomRenderer) RenderToPng(url string) (string, error) {
 | 
			
		||||
	log.Info("PhantomRenderer::renderToPng url %v", url)
 | 
			
		||||
type RenderOpts struct {
 | 
			
		||||
	Url    string
 | 
			
		||||
	Width  string
 | 
			
		||||
	Height string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (self *PhantomRenderer) RenderToPng(params *RenderOpts) (string, error) {
 | 
			
		||||
	log.Info("PhantomRenderer::renderToPng url %v", params.Url)
 | 
			
		||||
	binPath, _ := filepath.Abs(filepath.Join(self.PhantomDir, "phantomjs"))
 | 
			
		||||
	scriptPath, _ := filepath.Abs(filepath.Join(self.PhantomDir, "render.js"))
 | 
			
		||||
	pngPath, _ := filepath.Abs(filepath.Join(self.ImagesDir, getHash(url)))
 | 
			
		||||
	pngPath, _ := filepath.Abs(filepath.Join(self.ImagesDir, getHash(params.Url)))
 | 
			
		||||
	pngPath = pngPath + ".png"
 | 
			
		||||
 | 
			
		||||
	cmd := exec.Command(binPath, scriptPath, "url="+url, "width=100", "height=100", "png="+pngPath)
 | 
			
		||||
	cmd := exec.Command(binPath, scriptPath, "url="+params.Url, "width="+params.Width, "height="+params.Height, "png="+pngPath)
 | 
			
		||||
	stdout, err := cmd.StdoutPipe()
 | 
			
		||||
 | 
			
		||||
	if err != nil {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user