forked from golang-book/bootcamp-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.go
104 lines (89 loc) · 2.24 KB
/
routes.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
package csvexample
import (
"encoding/json"
"fmt"
"io"
"net/http"
"google.golang.org/appengine"
)
func init() {
http.HandleFunc("/bernie", handleInput)
http.HandleFunc("/madoff", handleOutput)
}
func handleInput(res http.ResponseWriter, req *http.Request) {
io.WriteString(res, `<!DOCTYPE html>
<html>
<head></head>
<body>
<form method="GET" action="/madoff">
<label>Symbol #1:
<input type="text" name="symbol1">
</label>
<label>Symbol #2:
<input type="text" name="symbol2">
</label>
<input type="submit">
</form>
</body>
</html>`)
}
func handleOutput(res http.ResponseWriter, req *http.Request) {
ctx := appengine.NewContext(req)
symbol1, symbol2 := req.FormValue("symbol1"), req.FormValue("symbol2")
data1, err := getData(ctx, symbol1)
if err != nil {
http.Error(res, err.Error(), 500)
return
}
data2, err := getData(ctx, symbol2)
if err != nil {
http.Error(res, err.Error(), 500)
return
}
data1json, _ := json.Marshal(data1)
data2json, _ := json.Marshal(data2)
c := correlation(data1, data2)
io.WriteString(res, `<!DOCTYPE html>
<html>
<head>
<script src="//cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.js"></script>
</head>
<body>
<canvas id="myChart" width="400" height="400"></canvas>
SYMBOL 1: `+symbol1+`<br>
SYMBOL 2: `+symbol2+`<br>
CORRELATION: <strong>`+fmt.Sprintf("%.4f", c)+`</strong>
<script>
var ctx = document.getElementById("myChart").getContext("2d");
var myNewChart = new Chart(ctx).Line({
labels: [
"January","February"
],
datasets: [
{
label: "`+symbol1+`",
fillColor: "rgba(220,220,220,0.2)",
strokeColor: "rgba(220,220,220,1)",
pointColor: "rgba(220,220,220,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(220,220,220,1)",
data: `+string(data1json)+`
},
{
label: "`+symbol2+`",
fillColor: "rgba(151,187,205,0.2)",
strokeColor: "rgba(151,187,205,1)",
pointColor: "rgba(151,187,205,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(151,187,205,1)",
data: `+string(data2json)+`
}
]
}, null);
</script>
</body>
</html>
`)
}