forked from arafatkatze/DataViz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
188 lines (174 loc) · 5.39 KB
/
main.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"strings"
"github.com/gin-gonic/gin"
"github.com/pennz/DataViz/viz"
)
func setupRouter() *gin.Engine {
r := gin.Default()
r.Use(gin.Logger())
r.GET("/ping", func(c *gin.Context) {
c.String(200, "pong")
})
r.Static("/Viz", "GraphVizOnline")
r.POST("/compile", compileHandler)
r.POST("/compile_debug", compileHandler_debug)
//r.LoadHTMLGlob("GraphVizOnline/*.html")
//r.GET("/", func(c *gin.Context) {
// c.HTML(http.StatusOK, "index.html", nil)
//})
return r
}
func main() {
r := setupRouter()
port := os.Getenv("PORT")
if port == "" {
log.Fatal("$PORT must be set")
}
r.Run(":" + port)
}
func readCloser2String(rc io.ReadCloser) string {
buf := new(bytes.Buffer)
buf.ReadFrom(rc)
newStr := buf.String()
return newStr
}
// runGoAsync(goSRCcode).then((d) => {
// if (d.Events == null) {
// show_status("compiling failed: " + d.Errors, 5500);
// }
// for (let i = d.Events.length - 1; i >= 1; i--) { // reverse
// setGraphSteps(d.Events[i].Message, goSRCcode);
// }
// let ss0 = splitGraph(d.Events[0].Message)
// graphSRC.setValue(ss0[0]);
// for (let i = ss0.length; i >= 0; i--) { // reverse
// let s = ss0[i];
// updateStateHistory(
// encodeURIComponent(s),
// encodeURIComponent(goSRCcode));
// }
// });
// {"Errors":"","Events":[{"Message":"digraph graphname
// {\n\tbgcolor=white\n\tsubgraph cluster_0 {style=filled color=lightgrey
// node [style=filled color=white shape=\"Msquare\"] -1 0 1 -2
// 3}\n}\ndigraph graphname {\n\tbgcolor=white\n\tsubgraph cluster_0
// {style=filled color=lightgrey node [style=filled color=white
// shape=\"Msquare\"] -1 0 1 -2 3 4}\n}\ndigraph graphname
// {\n\tbgcolor=white\n\tsubgraph cluster_0 {style=filled color=lightgrey
// node [style=filled color=white shape=\"Msquare\"] -1 0 1 -2
// 3 4 5}\n}\ndigraph graphname {\n\tbgcolor=white\n\tsubgraph cluster_0
// {style=filled color=lightgrey node [style=filled color=white
// shape=\"Msquare\"] -1 [color=red style=filled fillcolor=red]
// 0 [color=blue style=filled fillcolor=blue] 1 -2 3 4 5}\n}\ndigraph
// graphname {\n\tbgcolor=white\n\tsubgraph cluster_0 {style=filled
// color=lightgrey node [style=filled color=white shape=\"Msquare\"]
// 0 [color=red style=filled fillcolor=red] -1 [color=blue style=filled
// fillcolor=blue] 1 -2
// 3 4 5}\n}\n","Kind":"stdout","Delay":0}],"Status":0,"IsTest":false,"TestsFailed":0,"VetOK":true}]}}]}}]}}
type RunResult struct {
Errors string
Events []Event
Status int
IsTest bool
TestsFailed int
VetOK bool
}
type Event struct {
Message string
Kind string
Delay int
}
func splitDotGraph(multiGraph string) []string {
//var splitGraph = function (s) {
// let ss = s.split("digraph");
// for (let i = 0; i < ss.length; i++) {
// if (ss[i].trim() == "") {
// ss.splice(i, 1);
// }
// }
// for (let i = 0; i < ss.length; i++) {
// ss[i] = "digraph" + ss[i];
// }
// return ss;
//};
ss := strings.Split(multiGraph, "digraph")
gs := make([]string, 0)
for _, s := range ss {
//log.Println(s)
if len(strings.TrimSpace(s)) != 0 {
gs = append(gs, "digraph"+s)
}
}
//log.Println(gs)
return gs
}
func readCloser2SVG(rc io.ReadCloser) string {
buf := new(bytes.Buffer)
buf.ReadFrom(rc)
log.Println("Converting dot to SVG...")
//log.Println("json: ", buf.String())
var result RunResult
json.Unmarshal(buf.Bytes(), &result)
//log.Println("json parsed:", result)
if result.Errors == "" {
newStr := new(bytes.Buffer)
// log.Println("Events parsed:", result)
//newStr.WriteString(viz.Dot2SVG(result["Events"][0]["Message"][0]))
message := result.Events[0].Message
// need to parse dia subgraph
for _, oneDot := range splitDotGraph(message) {
//log.Println("dot: ", oneDot)
svg := viz.Dot2SVG(oneDot)
//log.Println("svg: ", svg)
newStr.WriteString(svg)
}
//log.Println("SVG parsed:")
return newStr.String()
}
log.Printf("Error when parsing result from playground:\n%s\n", result.Errors)
return ""
}
func read2buf(rc io.ReadCloser) *bytes.Buffer {
buf := new(bytes.Buffer)
buf.ReadFrom(rc)
return buf
}
func compileHandler_debug(c *gin.Context) {
version := c.PostForm("version")
body := c.PostForm("body")
withVet := c.PostForm("withVet")
log.Println(version, body, withVet)
}
// compileHandler will relay the request to play.golang.org
func compileHandler(c *gin.Context) {
//log.Printf("%v\n", readCloser2String(c.Request.Body))
// https://github.com/gin-gonic/gin#try-to-bind-body-into-different-structs
// The normal methods for binding request body consumes c.Request.Body and
// they cannot be called multiple times.
//buf := read2buf(c.Request.Body)
// we can change the body in the go
body := c.PostForm("body")
s := fmt.Sprintf("version=%d&body=%s&withVet=%s", 2, url.QueryEscape(body), "true")
//log.Println(s)
buf := bytes.NewBufferString(s)
var relay io.Reader = bytes.NewReader(buf.Bytes())
response, err := http.Post("https://play.golang.org/compile", "application/x-www-form-urlencoded; charset=UTF-8", relay)
if err == nil {
if response.StatusCode == 200 && response.Body != nil {
c.String(response.StatusCode, readCloser2SVG(response.Body))
} else {
c.String(response.StatusCode, "Error or cannot get response from play.golang.org.")
}
} else {
c.String(404, "Cannot access play.golang.org.")
}
}