-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgraphviz.go
More file actions
73 lines (64 loc) · 1.95 KB
/
graphviz.go
File metadata and controls
73 lines (64 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// graphviz is an HTTP server which calls GraphViz's dot command to
// visualize a .dot file given in the `dot` parameter. For example,
// we can run this program as follows
//
// go run graphviz.go
//
// and access it from the Web browser with URL:
//
// http://localhost:8080/?dot=https://gist.githubusercontent.com/wangkuiyi/c4e0015211dd1b9bde2e20455a6cd38e/raw/4d5ec099f98a5f326cf6f108bcf510cadba1a0b4/ci-arch.dot
//
// then the visualization of ci-arch.dot should appear in the browser window.
//
package main
import (
"crypto/md5"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path"
"github.com/topicai/candy"
)
func main() {
dir := flag.String("dir", "/tmp", "The cache directory")
addr := flag.String("addr", ":8080", "The listening address")
flag.Parse()
http.HandleFunc("/",
makeSafeHandler(func(w http.ResponseWriter, r *http.Request) {
if source := r.FormValue("dot"); len(source) > 0 {
dot, e := candy.HTTPGet(source, 0)
candy.Must(e)
id := fmt.Sprintf("%015x", md5.Sum(dot))
dotFile := path.Join(*dir, id) + ".dot"
pngFile := path.Join(*dir, id) + ".png"
if _, e := os.Stat(pngFile); os.IsNotExist(e) {
// TODO(yi): Here we adopt an unlimted-size disk-based cache. We should
// either introduce LRU and limit the size, or periodically clean it.
log.Printf("Update cache for %s", pngFile)
candy.Must(ioutil.WriteFile(dotFile, dot, 0755))
png, e := exec.Command("dot", "-Tpng", dotFile).Output()
candy.Must(e)
candy.Must(ioutil.WriteFile(pngFile, png, 0755))
}
png, e := ioutil.ReadFile(pngFile)
candy.Must(e)
_, e = w.Write(png)
candy.Must(e)
}
}))
candy.Must(http.ListenAndServe(*addr, nil))
}
func makeSafeHandler(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer func() {
if e := recover(); e != nil {
http.Error(w, fmt.Sprint(e), http.StatusInternalServerError)
}
}()
h(w, r)
}
}