/
scoreServer.go
86 lines (70 loc) · 1.81 KB
/
scoreServer.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
package main
import "io"
import "fmt"
import "time"
import "strconv"
import "net/http"
import "encoding/json"
import "github.com/garyburd/redigo/redis"
var pool redis.Pool
type HighScore struct {
Score float64
Time int64
}
// Http request handler for posting and getting high scores.
func HandleScore(w http.ResponseWriter, req *http.Request) {
var out string
// Connect to the data source.
conn := pool.Get()
defer conn.Close()
outVal, err := redis.Bytes(conn.Do("GET", "highScore:value"))
if err != nil {
return
}
highScore, err := strconv.ParseFloat(string(outVal), 64)
if err != nil {
return
}
if req.Method == "GET" {
// Retrieve the high score from the server and format it as json.
if err == nil {
currentHigh := HighScore{float64(highScore), 0}
marshaled, _ := json.Marshal(currentHigh)
out = string(marshaled)
}
} else if req.Method == "POST" {
postedScore, err := strconv.ParseFloat(req.FormValue("scorePosted"), 64)
if err == nil && postedScore > highScore {
conn.Do("SET", "highScore:value", postedScore)
}
}
io.WriteString(w, out)
}
func setupConnectionPool() redis.Pool {
dialFunc := func() (redis.Conn, error) {
c, err := redis.Dial("tcp", "127.0.0.1:6379")
if err != nil {
return nil, err
}
return c, err
}
testOnBorrow := func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
}
// Initialize the redis connection pool used by the score handler.
pool := redis.Pool{
MaxIdle: 250,
IdleTimeout: 300 * time.Second,
Dial: dialFunc,
TestOnBorrow: testOnBorrow,
}
return pool
}
func main() {
pool = setupConnectionPool()
http.Handle("/", http.FileServer(http.Dir("./public")))
http.HandleFunc("/scores", HandleScore)
fmt.Println("Began serving requests at: 127.0.0.1:4000")
http.ListenAndServe(":4000", nil)
}