-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
52 lines (43 loc) · 1.02 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
package main
import (
"fmt"
"gopkg.in/redis.v3"
"log"
"net/http"
"os"
)
var redisClient *Redis
type Redis struct {
*redis.Client
}
func (r Redis) getCount() (int64, error) {
if err := redisClient.Incr("ping").Err(); err != nil {
return -1, err
}
count, err := redisClient.Get("ping").Int64()
if err != nil {
return -1, err
}
return count, err
}
func pingServer(w http.ResponseWriter, req *http.Request) {
var message string
if count, err := redisClient.getCount(); err == nil {
message = fmt.Sprintf("ping! %d", count)
} else {
message = err.Error()
}
w.Write([]byte(message))
}
func main() {
uri := fmt.Sprintf("%s:6379", os.Getenv("REDIS_HOST"))
redisClient = &Redis{
redis.NewClient(&redis.Options{
Addr: uri,
Password: "", // no password set
DB: 0, // use default DB
}),
}
http.HandleFunc("/", pingServer)
log.Fatal(http.ListenAndServe(":8080", nil))
}