-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
49 lines (39 loc) · 913 Bytes
/
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
package main
import (
"facemash/algo"
"facemash/db"
"facemash/models"
"fmt"
"html/template"
"net/http"
)
func serveHome(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("html/index.html")
if err != nil {
fmt.Println(err)
}
data := db.FetchRandoms()
t.Execute(w, data)
}
func serveVote(w http.ResponseWriter, r *http.Request) {
winner := r.Header.Get("w")
loser := r.Header.Get("l")
db.UpdateRanking(winner, loser)
}
func serveRanking(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("html/rankings.html")
if err != nil {
fmt.Println(err)
}
data := new(models.RankingData)
data.Sources = db.GetRankings()
t.Execute(w, data)
}
func main() {
algo.SetParams(10, 400, 1)
db.ConnectDB()
http.HandleFunc("/", serveHome)
http.HandleFunc("/vote", serveVote)
http.HandleFunc("/ranking", serveRanking)
http.ListenAndServe(":8080", nil)
}