2014-12-29 06:36:08 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2015-01-29 05:10:34 -06:00
|
|
|
"fmt"
|
2014-12-29 06:36:08 -06:00
|
|
|
"net/http"
|
|
|
|
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/components/renderer"
|
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
2015-02-06 07:17:40 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2014-12-29 06:36:08 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func RenderToPng(c *middleware.Context) {
|
2015-01-08 02:00:00 -06:00
|
|
|
queryReader := util.NewUrlQueryReader(c.Req.URL)
|
2015-03-20 18:14:13 -05:00
|
|
|
queryParams := fmt.Sprintf("?%s", c.Req.URL.RawQuery)
|
2015-04-08 01:59:12 -05:00
|
|
|
sessionId := c.Session.ID()
|
|
|
|
|
|
|
|
// Handle api calls authenticated without session
|
|
|
|
if sessionId == "" && c.ApiKeyId != 0 {
|
|
|
|
c.Session.Start(c)
|
|
|
|
c.Session.Set(middleware.SESS_KEY_APIKEY, c.ApiKeyId)
|
|
|
|
// release will make sure the new session is persisted before
|
|
|
|
// we spin up phantomjs
|
|
|
|
c.Session.Release()
|
|
|
|
// cleanup session after render is complete
|
|
|
|
defer func() { c.Session.Destory(c) }()
|
|
|
|
}
|
2014-12-29 06:36:08 -06:00
|
|
|
|
|
|
|
renderOpts := &renderer.RenderOpts{
|
2015-03-20 18:14:13 -05:00
|
|
|
Url: c.Params("*") + queryParams,
|
|
|
|
Width: queryReader.Get("width", "800"),
|
|
|
|
Height: queryReader.Get("height", "400"),
|
|
|
|
SessionId: c.Session.ID(),
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
2015-02-06 07:17:40 -06:00
|
|
|
renderOpts.Url = setting.ToAbsUrl(renderOpts.Url)
|
2014-12-29 06:36:08 -06:00
|
|
|
pngPath, err := renderer.RenderToPng(renderOpts)
|
2015-02-05 05:23:24 -06:00
|
|
|
|
2014-12-29 06:36:08 -06:00
|
|
|
if err != nil {
|
2015-02-05 05:23:24 -06:00
|
|
|
c.Handle(500, "Failed to render to png", err)
|
|
|
|
return
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Resp.Header().Set("Content-Type", "image/png")
|
|
|
|
http.ServeFile(c.Resp, c.Req.Request, pngPath)
|
|
|
|
}
|