This repository has been archived by the owner on Oct 18, 2021. It is now read-only.
/
frontend.go
214 lines (175 loc) · 5.67 KB
/
frontend.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package main;
import "dispatcher"
import "net/http"
//import _ "github.com/mattn/go-sqlite3"
//import "database/sql"
import "log"
/* import "time" */
import "fmt"
import "bytes"
import "encoding/json"
import "strconv"
import "os"
import "encoding/csv"
var g_version string;
type ReceiveMessageHandle struct {
//database *sql.DB;
columns []string;
file *os.File;
};
func valueToString(in interface{}) string {
switch t := in.(type) {
default:
return "nil";
case string:
return t;
case int:
return strconv.FormatInt(int64(t), 10);
case int64:
return strconv.FormatInt(t, 10);
case float64:
return strconv.FormatFloat(t, 'f', 6, 64);
case bool:
if t == true {
return "true";
}else{
return "false";
}
}
}
func decodeData(jsontext string) (out []map[string]string, ok bool){
var strarr []interface{};
err := json.Unmarshal(bytes.NewBufferString(jsontext).Bytes(), &strarr);
if err != nil {
fmt.Println("error:", err)
return nil, false;
}
if(strarr[0] == "magic v0.2") {
//var record []string;
for i := 1; i < len(strarr); i++ {
outmap := make(map[string]string);
m, isok := strarr[i].(map[string]interface{});
if(!isok) {
// All of these should work
return nil, false;
}
for key, value := range m {
//fmt.Println(key + ", " + valueToString(value));
outmap[key] = valueToString(value);
}
out = append(out, outmap);
}
}
return out, true;
}
func writeCsvRecords(wr *os.File, columns []string, records []map[string]string) (ok bool) {
csvwrite := csv.NewWriter(wr);
// Loop over the records
for _, record := range records {
// Find the keys we're using
outslice := make([]string, len(columns));
for i, keyname := range columns {
col, ok := record[keyname];
if(ok) {
outslice[i] = col;
} else {
outslice[i] = "nil";
}
}
csvwrite.Write(outslice);
}
csvwrite.Flush();
if(csvwrite.Error() != nil) {
return false;
}
return true;
}
func (wh ReceiveMessageHandle) ServeHTTP(wr http.ResponseWriter, req *http.Request) {
/* message = req.FormValue("message");
from = req.FormValue("from");
to = req.FormValue("to");
timestamp = time.Now(); */
//wr.Header().Add("Content-Type", "text/html");
/* wr.Header().Add("Access-Control-Allow-Origin", "http://scoutdb.frc3512.com");
wr.Header().Add("Access-Control-Allow-Headers", "Content-Type"); */
wr.WriteHeader(200);
buf := new(bytes.Buffer);
buf.ReadFrom(req.Body);
s := buf.String();
records, ok := decodeData(s);
if(!ok) {
fmt.Fprintln(wr, "Failed");
return;
}
ok = writeCsvRecords(wh.file, wh.columns, records);
if(!ok) {
fmt.Fprintln(wr, "Failed");
return;
}
fmt.Fprintln(wr, "OK");
return;
fmt.Fprintln(wr, "Failed");
}
func formHandle(wr http.ResponseWriter, req *http.Request) {
http.ServeFile(wr, req, "html/scouting-form.html");
}
func versionHandle(wr http.ResponseWriter, req *http.Request) {
wr.Header().Add("Content-Type", "text/javascript");
wr.WriteHeader(200);
fmt.Fprintln(wr, "var g_versionString = '" + g_version + "';");
}
func csvHandle(wr http.ResponseWriter, req *http.Request) {
http.ServeFile(wr, req, "output.csv");
}
func mainHandle(wr http.ResponseWriter, req *http.Request) {
http.ServeFile(wr, req, "html/index.html");
}
func main() {
var d *dispatcher.Dispatcher;
/* Initialize the database */
//db, err := sql.Open("sqlite3", "scouting.db");
//if(err != nil) {
// log.Fatal(err);
//}
//defer db.Close();
fmt.Println("Version: " + g_version);
// Open the file
file, err := os.OpenFile("output.csv", os.O_RDWR | os.O_APPEND, 0660);
if(err != nil) {
fmt.Println(err);
return;
}
defer file.Close();
// Write the list of columns to the CSV file
/* columns := []string{"timestamp", "f_matchNumber", "f_teamNumber", "f_numAutoZone", "f_numStageBin", "f_numStepBin", "f_robotAuton", "f_toteAuton", "f_numOnStep", "f_numOnTop", "f_fouls", "f_stacks", "f_dead", "f_tipped", "f_tippedOtherRobot", "f_morePlayerStation", "f_notes"}
for i := 1; i < 5; i++ {
columns = append(columns, "f_" + strconv.Itoa(i) + "_litter");
columns = append(columns, "f_" + strconv.Itoa(i) + "_can");
columns = append(columns, "f_" + strconv.Itoa(i) + "_ntotes");
}
writer := csv.NewWriter(file);
writer.Write(columns);
writer.Flush(); */
// Create our list of columns from the first line of the CSV file
reader := csv.NewReader(file);
columns, err := reader.Read();
if(err != nil) {
fmt.Println(err);
return;
}
// Seek to the end so we can begin writing
file.Seek(0, 2);
msgHandle := new(ReceiveMessageHandle);
msgHandle.columns = columns;
msgHandle.file = file;
//msgHandle.database = db;
/* Initialize the HTTP server */
d = dispatcher.NewDispatcher();
d.RegisterExpr("^/$", http.HandlerFunc(mainHandle));
d.RegisterExpr("^/form$", http.HandlerFunc(formHandle));
d.RegisterExpr("^/version.js$", http.HandlerFunc(versionHandle));
d.RegisterExpr("^/output.csv$", http.HandlerFunc(csvHandle));
d.RegisterExpr("^/write$", http.StripPrefix("/", msgHandle));
d.RegisterExpr("^/res", http.StripPrefix("/res/", http.FileServer(http.Dir("html/res/"))));
log.Fatal(http.ListenAndServe("127.0.0.1:8088", d))
}