-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.go
87 lines (68 loc) · 1.61 KB
/
app.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
package main
import (
"fmt"
"net/http"
"github.com/cenkalti/backoff"
"github.com/go-redis/redis"
"github.com/sirupsen/logrus"
)
type app struct {
Port int
Redis db `json:"database"`
}
type db struct {
Hostname string `json:"hostname"`
Port int `json:"port"`
Conn *redis.Client
}
func (a *app) Help(w http.ResponseWriter, r *http.Request) {
help := `PUT /incr
GET /count
GET /version`
fmt.Fprintf(w, help)
}
func (a *app) Version(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, version)
}
func (a *app) Count(w http.ResponseWriter, r *http.Request) {
val, err := a.Redis.Conn.Get("countme").Result()
if err != nil {
fmt.Fprintf(w, "error")
}
fmt.Fprintf(w, val)
}
func (a *app) Incr(w http.ResponseWriter, r *http.Request) {
_, err := a.Redis.Conn.Incr("countme").Result()
if err != nil {
fmt.Fprintf(w, "error")
}
a.Count(w, r)
}
func (a *app) Initialize() error {
cs := fmt.Sprintf("%s:%d", a.Redis.Hostname, a.Redis.Port)
a.Redis.Conn = redis.NewClient(&redis.Options{
Addr: cs,
})
pingRedis := func() error {
_, err := a.Redis.Conn.Ping().Result()
if err != nil {
logrus.WithFields(logrus.Fields{
"hostname": a.Redis.Hostname,
"port": a.Redis.Port,
}).Info("Redis Connection failed")
return err
}
return nil
}
b := backoff.NewExponentialBackOff()
err := backoff.Retry(pingRedis, b)
return err
}
func (a *app) Run() error {
http.HandleFunc("/", a.Help)
http.HandleFunc("/count", a.Count)
http.HandleFunc("/incr", a.Incr)
http.HandleFunc("/version", a.Version)
addr := fmt.Sprintf(":%d", a.Port)
return http.ListenAndServe(addr, nil)
}