-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphase_two_server.go
195 lines (161 loc) · 4.59 KB
/
phase_two_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
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
package main
import (
"bufio"
"encoding/csv"
"fmt"
"github.com/docopt/docopt-go"
"html/template"
"log"
"net/http"
"os"
"strings"
)
type Comments struct {
Negative []string
Positive []string
}
func readCSVFile(filePath string) [][]string {
f, err := os.Open(filePath)
if err != nil {
log.Fatal("Unable to read input file "+filePath, err)
}
defer f.Close()
csvReader := csv.NewReader(f)
records, err := csvReader.ReadAll()
if err != nil {
log.Fatal("Unable to parse file as CSV for "+filePath, err)
}
return records
}
func filterResponses(records [][]string) Comments {
var negComments []string
var posComments []string
for _, row := range records {
if strings.HasPrefix(row[2], "neg_") {
if row[3] != "" {
negComments = append(negComments, row[3])
}
} else {
if row[3] != "" {
posComments = append(posComments, row[3])
}
}
}
return Comments{
Negative: negComments,
Positive: posComments,
}
}
// App creates a mux and binds the root route for processing
// static files.
func startServing() http.Handler {
// Create a new mux for this service.
m := http.NewServeMux()
// m.Handle("/", http.FileServer(http.Dir("./static")))
m.HandleFunc("/", displayCollector)
m.HandleFunc("/reply", collectResponses)
return m
}
func appendTo(file string, data []string) {
fileHandle, err := os.OpenFile(file, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
if err != nil {
log.Println(err)
}
writer := bufio.NewWriter(fileHandle)
defer fileHandle.Close()
dataStr := strings.Join(data, ",")
csvLine := fmt.Sprintf("%s", dataStr)
fmt.Fprintln(writer, csvLine)
writer.Flush()
}
func displayCollector(w http.ResponseWriter, r *http.Request) {
indexHTMLTemplate := `<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdn.rawgit.com/Chalarangelo/mini.css/v3.0.1/dist/mini-default.min.css">
</head>
<body>
<h2>Do you agree on your class-mates' statements?</h2>
<p>Please select (set a checkmark) on each comment that you agree on.</p>
<form action="/reply">
<h3>Negative Comments</h3>
<ul>
{{range $index, $element := .Negative}}
<li><input type="checkbox" name="neg_{{$index}}" value="{{$element}}"> {{$element}}</li>
{{end}}
</ul>
<hr>
<h3>Positive Comments</h3>
<ul>
{{range $index, $element := .Positive}}
<li><input type="checkbox" name="pos_{{$index}}" value="{{$element}}"> {{$element}}</li>
{{end}}
</ul>
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
`
t, err := template.New("comments").Parse(indexHTMLTemplate)
if err != nil {
// TODO: check how to handle this precisely
panic(err)
}
// the global variable comments of type Comments
t.Execute(w, comments)
}
// Inspired by https://www.veracode.com/blog/2013/12/golangs-context-aware-html-templates
func collectResponses(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, "Error parsing form values!", http.StatusInternalServerError)
return
}
// get the form values
for key, value := range r.Form {
commentStr := value[0]
commentStr = strings.Replace(commentStr, "\"", "\"\"", -1)
commentStr = fmt.Sprintf("\"%s\"", commentStr)
data := []string{key, commentStr}
appendTo("rated_responses.txt", data)
}
thankYouHTML := `<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdn.rawgit.com/Chalarangelo/mini.css/v3.0.1/dist/mini-default.min.css">
</head>
<body>
<h1>Thank you for your feedback!</h1>
</body>
</html>
`
fmt.Fprintf(w, thankYouHTML)
}
var comments Comments
func main() {
usage := `Delphi Evaluation Phase Two Server.
Start me for example like:
$ ./phase_two_server --addr=0.0.0.0 --port=9999 >> log/phase_two.log 2>&1 &
Usage:
phase_two_server [--addr=<addr>] [--port=<port>]
phase_two_server -h | --help
phase_two_server --version
Options:
-h --help Show this screen.
--version Show version.
--addr=<addr> Address for serving [default: 127.0.0.1].
--port=<port> Port for serving [default: 8080].
Hint:
To quickly count the for the most rated comments run:
$ sort rated_responses.txt | uniq -c | sort -r
`
arguments, _ := docopt.Parse(usage, nil, true, "Delphi Evaluation Phase Two Server 1.0", false)
port := arguments["--port"].(string)
addr := arguments["--addr"].(string)
// Be aware of that the responses file is read on server startup! That is
// do not start the server before that file is in a state that you like
responses := readCSVFile("./responses.txt")
comments = filterResponses(responses)
log.Print(fmt.Sprintf("Serving on http://%s:%s", addr, port))
log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%s", addr, port), startServing()))
}