From 4a73e2d0e9f1c3787c78cf59d2744c3cd3914b57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Wed, 1 Oct 2014 13:20:30 +0200 Subject: [PATCH] Solo panel and phantom rendering work --- _vendor/phantomjs/render.js | 6 ++++-- pkg/api/api_auth.go | 30 ++++++++++++++++++++++++++---- pkg/api/api_render.go | 10 +++++++--- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/_vendor/phantomjs/render.js b/_vendor/phantomjs/render.js index ff11bd667c9..79b2be8c12c 100644 --- a/_vendor/phantomjs/render.js +++ b/_vendor/phantomjs/render.js @@ -1,10 +1,12 @@ var page = require('webpage').create(); var args = require('system').args; var params = {}; +var regexp = /^([^=]+)=([^$]+)/; args.forEach(function(arg) { - var parts = arg.split('='); - params[parts[0]] = parts[1]; + var parts = arg.match(regexp); + if (!parts) { return; } + params[parts[1]] = parts[2]; }); var usage = "url= png= width= height="; diff --git a/pkg/api/api_auth.go b/pkg/api/api_auth.go index 8ff49d9ec43..69ed3872748 100644 --- a/pkg/api/api_auth.go +++ b/pkg/api/api_auth.go @@ -1,8 +1,13 @@ package api import ( - "github.com/gin-gonic/gin" + "errors" + "strconv" + "github.com/torkelo/grafana-pro/pkg/models" + + "github.com/gin-gonic/gin" + "github.com/gorilla/sessions" ) type authContext struct { @@ -19,16 +24,34 @@ func (self *HttpServer) authDenied(c *gin.Context) { c.Abort(302) } +func authGetRequestAccountId(c *gin.Context, session *sessions.Session) (int, error) { + accountId := session.Values["accountId"] + + urlQuery := c.Request.URL.Query() + if len(urlQuery["render"]) > 0 { + accId, _ := strconv.Atoi(urlQuery["accountId"][0]) + session.Values["accountId"] = accId + accountId = accId + } + + if accountId == nil { + return -1, errors.New("Auth: session account id not found") + } + + return accountId.(int), nil +} + func (self *HttpServer) auth() gin.HandlerFunc { return func(c *gin.Context) { session, _ := sessionStore.Get(c.Request, "grafana-session") + accountId, err := authGetRequestAccountId(c, session) - if c.Request.URL.Path != "/login" && session.Values["accountId"] == nil { + if err != nil && c.Request.URL.Path != "/login" { self.authDenied(c) return } - account, err := self.store.GetAccount(session.Values["accountId"].(int)) + account, err := self.store.GetAccount(accountId) if err != nil { self.authDenied(c) return @@ -42,7 +65,6 @@ func (self *HttpServer) auth() gin.HandlerFunc { c.Set("userAccount", account) c.Set("usingAccount", usingAccount) - session.Save(c.Request, c.Writer) } } diff --git a/pkg/api/api_render.go b/pkg/api/api_render.go index 79911b7e09b..ee2d56d3572 100644 --- a/pkg/api/api_render.go +++ b/pkg/api/api_render.go @@ -1,20 +1,24 @@ package api import ( + "strconv" + log "github.com/alecthomas/log4go" "github.com/gin-gonic/gin" ) func init() { addRoutes(func(self *HttpServer) { - self.router.GET("/api/render/*url", self.renderToPng) + self.addRoute("GET", "/api/render/*url", self.renderToPng) }) } -func (self *HttpServer) renderToPng(c *gin.Context) { +func (self *HttpServer) renderToPng(c *gin.Context, auth *authContext) { url := c.Params.ByName("url") + accountId := auth.getAccountId() + log.Info("Rendering url %v", url) - pngPath, err := self.renderer.RenderToPng("http://localhost:3000/" + url) + pngPath, err := self.renderer.RenderToPng("http://localhost:3000" + url + "?render&accountId=" + strconv.Itoa(accountId)) if err != nil { c.HTML(500, "error.html", nil) }