-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraver.go
More file actions
51 lines (44 loc) · 1013 Bytes
/
graver.go
File metadata and controls
51 lines (44 loc) · 1013 Bytes
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
package main
import (
"io"
"log"
"net/http"
"net/url"
"os/exec"
)
func handler(w http.ResponseWriter, r *http.Request) {
plotter := exec.Command("dot", "-Tpng")
source, e := plotter.StdinPipe()
if e != nil {
http.Error(w, e.Error(), http.StatusInternalServerError)
return
}
png, e := plotter.StdoutPipe()
if e != nil {
http.Error(w, e.Error(), http.StatusInternalServerError)
return
}
if e := plotter.Start(); e != nil {
http.Error(w, e.Error(), http.StatusInternalServerError)
return
}
defer plotter.Wait()
go func() {
defer source.Close() // NOTE: closing the pipe to notify the end of input.
s, e := url.QueryUnescape(r.URL.RawQuery)
if e != nil {
log.Printf("Failed writing")
}
if _, e := source.Write([]byte(s)); e != nil {
log.Printf("Failed writing")
}
}()
if _, e := io.Copy(w, png); e != nil {
http.Error(w, e.Error(), http.StatusInternalServerError)
return
}
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}