-
Notifications
You must be signed in to change notification settings - Fork 1
/
insecure.go
46 lines (40 loc) · 910 Bytes
/
insecure.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
package main
import (
"log"
"net/http"
)
var votes chan string
func MainServer(w http.ResponseWriter, req *http.Request) {
http.ServeFile(w, req, "insecure.html")
}
func VoteServer(w http.ResponseWriter, req *http.Request) {
val := req.FormValue("vote")
log.Printf("Observed vote %s from User Agent %s\n", val, req.UserAgent())
votes <- val
w.WriteHeader(http.StatusOK)
}
func collect() {
tea := 0
coffee := 0
for vote := range votes {
if vote == "tea" {
tea++
} else if vote == "coffee" {
coffee++
}
log.Println("tea: ", tea, "coffe: ", coffee)
}
}
func main() {
votes = make(chan string)
go collect()
http.HandleFunc("/", MainServer)
http.Handle("/static/",
http.StripPrefix("/static/",
http.FileServer(http.Dir("static/"))))
http.HandleFunc("/vote/", VoteServer)
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("Listen and Serve: ", err)
}
}