Graph REST API #116

This commit is contained in:
Sylvain Baubeau
2015-01-12 10:26:36 +01:00
parent 98ca0cdf33
commit 38cb6bd133
4 changed files with 186 additions and 117 deletions

56
api/graph.go Normal file
View File

@@ -0,0 +1,56 @@
package api
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"github.com/gin-gonic/gin"
"github.com/smira/aptly/graph"
)
// GET /api/graph
func apiGraph(c *gin.Context) {
var (
err error
output []byte
)
graph, err := graph.BuildGraph(context)
if err != nil {
c.JSON(500, err)
return
}
buf := bytes.NewBufferString(graph.String())
command := exec.Command("dot", "-Tpng")
command.Stderr = os.Stderr
stdin, err := command.StdinPipe()
if err != nil {
c.Fail(500, err)
return
}
_, err = io.Copy(stdin, buf)
if err != nil {
c.Fail(500, err)
return
}
err = stdin.Close()
if err != nil {
c.Fail(500, err)
return
}
output, err = command.Output()
if err != nil {
c.Fail(500, fmt.Errorf("unable to execute dot: %s (is graphviz package installed?)", err))
return
}
c.Data(200, "image/png", output)
}