-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgraph_server.go
122 lines (106 loc) · 2.66 KB
/
graph_server.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"encoding/json"
"html/template"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
)
const (
PREFIX = "graph-"
UPLOAD_DIR = "./graphs"
)
type GraphList struct {
Graphs []Graph `json:"graphs"`
}
type Graph struct {
ID string `json:"id"`
Label string `json:"label"`
}
var errorTemplate, _ = template.ParseFiles("error.html")
func check(err error) {
if err != nil {
panic(err)
}
}
func uploadErrorHandler(fn http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer func() {
if e, ok := recover().(error); ok {
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"success": false, "error": "` + e.Error() + `"}`))
}
}()
fn(w, r)
}
}
func errorHandler(fn http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer func() {
if e, ok := recover().(error); ok {
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"success": false, "error": "` + e.Error() + `"}`))
}
}()
fn(w, r)
}
}
func Basename(fileName string) string {
return strings.TrimSuffix(filepath.Base(fileName), filepath.Ext(fileName))
}
func listDirectory(w http.ResponseWriter) {
list := make([]Graph, 0)
files, err := ioutil.ReadDir(UPLOAD_DIR)
check(err)
for _, f := range files {
if f.IsDir() == false && filepath.Ext(f.Name()) == ".json" {
fileName := f.Name()
g := Graph{fileName, Basename(fileName)}
list = append(list, g)
}
}
gl := GraphList{list}
enc := json.NewEncoder(w)
enc.Encode(gl)
}
func upload(w http.ResponseWriter, r *http.Request) {
f, fh, err := r.FormFile("graph")
fileName := fh.Filename
log.Println("Filename: ", fileName)
check(err)
defer f.Close()
t, err := os.Create(filepath.Join(UPLOAD_DIR, fileName))
check(err)
defer t.Close()
_, err = io.Copy(t, f)
uuid := Basename(fileName)
check(err)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"success": true, "newUUID": "` + uuid + `"}`))
}
func view(w http.ResponseWriter, r *http.Request) {
id := r.FormValue("id")
if id != "" {
fileStr := filepath.Join(UPLOAD_DIR, r.FormValue("id")) + ".json"
log.Println("Serve graph file: ", fileStr)
w.Header().Set("Content-Type", "application/json")
http.ServeFile(w, r, fileStr)
} else {
listDirectory(w)
}
}
func main() {
log.Println("starting graph server...")
http.HandleFunc("/upload", uploadErrorHandler(upload))
http.HandleFunc("/view", errorHandler(view))
http.Handle("/", http.FileServer(http.Dir("../dist/")))
if err := http.ListenAndServe(":9401", nil); err != nil {
log.Fatalln(err)
}
}