-
-
Notifications
You must be signed in to change notification settings - Fork 415
/
handler.go
68 lines (54 loc) · 1.37 KB
/
handler.go
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
package main
import (
"fmt"
"log"
"net/http"
"strings"
)
func handler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" && !strings.HasSuffix(r.URL.Path, ".svg") {
http.NotFound(w, r)
return
}
logf("----------------------")
logf(" => handling request: %v", r.URL)
logf("----------------------")
// set up cmdline default for analysis
Analysis.OptsSetup()
// .. and allow overriding by HTTP params
Analysis.OverrideByHTTP(r)
var img string
if img = Analysis.FindCachedImg(); img != "" {
log.Println("serving file:", img)
http.ServeFile(w, r, img)
return
}
// Convert list-style args to []string
if e := Analysis.ProcessListArgs(); e != nil {
http.Error(w, "invalid parameters", http.StatusBadRequest)
return
}
output, err := Analysis.Render()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if r.Form.Get("format") == "dot" {
log.Println("writing dot output..")
fmt.Fprint(w, string(output))
return
}
log.Printf("converting dot to %s..\n", *outputFormat)
img, err = dotToImage("", *outputFormat, output)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = Analysis.CacheImg(img)
if err != nil {
http.Error(w, "cache img error: "+err.Error(), http.StatusBadRequest)
return
}
log.Println("serving file:", img)
http.ServeFile(w, r, img)
}