-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
69 lines (59 loc) · 1.46 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
package main
import (
"github.com/slashpai/voter-app/pkg/redis"
"html/template"
"log"
"net/http"
)
type Result struct {
DogVote int
CatVote int
NeutralVote int
Success bool
}
func main() {
indexTmpl := template.Must(template.ParseFiles("views/index.html"))
resultTmpl := template.Must(template.ParseFiles("views/results.html"))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.Print("Starting request")
if r.Method != http.MethodPost {
err := indexTmpl.Execute(w, nil)
if err != nil {
log.Fatal(err)
}
return
}
})
http.HandleFunc("/dog", func(w http.ResponseWriter, r *http.Request) {
log.Print("Vote for dog")
redis.VoteDog()
})
http.HandleFunc("/cat", func(w http.ResponseWriter, r *http.Request) {
log.Print("Vote for cat")
redis.VoteCat()
})
http.HandleFunc("/neutral", func(w http.ResponseWriter, r *http.Request) {
log.Print("Vote for neutral")
redis.VoteNeutral()
})
http.HandleFunc("/results", func(w http.ResponseWriter, r *http.Request) {
log.Print("Vote Results")
res := redis.VoteResult()
details := Result{
DogVote: res["dog"],
CatVote: res["cat"],
NeutralVote: res["neutral"],
Success: true,
}
log.Print("details", details)
err := resultTmpl.Execute(w, details)
if err != nil {
log.Fatal(err)
}
})
log.Print("Application is available at http://localhost:8080")
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal(err)
}
}