-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
105 lines (89 loc) · 2.39 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"fmt"
"log"
"math/rand"
"net/http"
"time"
"github.com/go-redis/redis"
)
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randSeq(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func main() {
rand.Seed(time.Now().UnixNano())
client := redis.NewClient(&redis.Options{
Addr: "127.0.0.1:6379",
Password: "",
DB: 0,
})
key := randSeq(8)
log.Printf("key set as %s", key)
pong, err := client.Ping().Result()
log.Println(pong, err)
handlers := CounterHandlers{
client: client,
key: key,
}
http.HandleFunc("/", hello)
http.HandleFunc("/increment", handlers.Increment)
http.HandleFunc("/decrement", handlers.Decrement)
http.HandleFunc("/count", handlers.Count)
log.Println("Starting http server on :8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}
}
type CounterHandlers struct {
client *redis.Client
key string
}
func (h CounterHandlers) Increment(w http.ResponseWriter, r *http.Request) {
val, err := h.client.Incr(h.key).Result()
if err != nil {
log.Printf("error incrementing %v", err)
http.Error(w, "error incrementing", http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "Incremented count to %d", val)
}
func (h CounterHandlers) Decrement(w http.ResponseWriter, r *http.Request) {
val, err := h.client.Decr(h.key).Result()
if err != nil {
log.Printf("error deccrementing %v", err)
http.Error(w, "error deccrementing", http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "Decremented count to %d", val)
}
func (h CounterHandlers) Count(w http.ResponseWriter, r *http.Request) {
val, err := h.client.Get(h.key).Result()
if err != nil {
log.Printf("error retreiving value %v", err)
http.Error(w, "error retrieving value", http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "Current count is %s", val)
}
func hello(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Welcome</title>
</head>
<body>
<h1>Welcome to the Redis Example</h1>
<p>You can increment the stored count at <a href="/increment">/increment</a></p>
<p>You can decrement the stored count at <a href="/decrement">/decrement</a></p>
<p>You can retrieve the stored count at <a href="/count">/count</a></p>
</body>
</html>
`)
}