-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
53 lines (44 loc) · 1.15 KB
/
handlers.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
package main
import (
"fmt"
"log"
"net/http"
)
var gsupportedTools = [...]string{"dot", "neato", "twopi", "circo", "fdp", "sfdp", "patchwork"}
func homePage(w http.ResponseWriter, r *http.Request, matches []string) {
http.ServeFile(w, r, "static/index.html")
}
func isSupportedTool(tool string) bool {
for _, val := range gsupportedTools {
if tool == val {
return true
}
}
return false
}
func generateHandler(w http.ResponseWriter, r *http.Request, matches []string) {
graph := r.FormValue("graphtext")
imgType := r.FormValue("imagetype")
tool := r.FormValue("tool")
if !isSupportedTool(tool) {
http.Error(w, fmt.Sprintf("Tool '%s' is not supported", tool), http.StatusBadRequest)
return
}
if len(imgType) == 0 {
http.Error(w, fmt.Sprintf("imagetype is not specified"), http.StatusBadRequest)
return
}
if len(graph) == 0 {
http.Error(w, "Empty input", http.StatusBadRequest)
return
}
result := runGraphviz(tool, graph, imgType)
if result.err != nil {
log.Print(result.err)
http.Error(w, result.err.Error(), http.StatusNotAcceptable)
return
}
w.WriteHeader(http.StatusCreated)
w.Write([]byte(result.fileName))
return
}