-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
68 lines (50 loc) · 1.36 KB
/
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
package main
import (
"flag"
"fmt"
"github.com/garyburd/redigo/redis"
"github.com/go-martini/martini"
"github.com/martini-contrib/render"
)
var (
redisAddress = flag.String("redis-address", ":6379", "Address to the Redis server")
maxConnections = flag.Int("max-connections", 10, "Max connections to Redis")
)
func main() {
flag.Parse()
redisPool := redis.NewPool(func() (redis.Conn, error) {
redisClient, err := redis.Dial("tcp", *redisAddress)
if err != nil {
return nil, err
}
return redisClient, err
}, *maxConnections)
defer redisPool.Close()
m := martini.Classic()
m.Map(redisPool)
m.Use(render.Renderer())
m.Get("/vote/:id", func(redisPool *redis.Pool, params martini.Params, r render.Render) {
if len(params["id"]) < 8 {
key := "votes/" + params["id"]
redisClient := redisPool.Get()
defer redisClient.Close()
votes, err := redisClient.Do("INCR", key)
if err != nil {
message := fmt.Sprintf("Unable to vote for: %s", key)
r.JSON(400, map[string]interface{}{
"status": "ERROR",
"message": message})
} else {
message := fmt.Sprintf("Voted for: %s, votes at: %d", key, votes)
r.JSON(200, map[string]interface{}{
"status": "SUCCESS",
"message": message})
}
} else {
r.JSON(400, map[string]interface{}{
"status": "ERROR",
"message": "Not a valid vote"})
}
})
m.Run()
}