-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
74 lines (56 loc) · 1.76 KB
/
Copy pathmain.go
File metadata and controls
74 lines (56 loc) · 1.76 KB
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
package main
import (
"fmt"
"net/http"
"os"
"time"
"strconv"
"gopkg.in/redis.v4"
)
func redisConnect() (connected bool, countval int) {
// connect to redis server, set by redis variable in docker-compose file!
client := redis.NewClient(&redis.Options{
Addr: "redis:6379",
Password: "", // no password set
DB: 0, // use default DB
})
// validate if connection worked
pong, err := client.Ping().Result()
fmt.Println(pong, err)
// set return value to indicate connectivity
if err == nil {
connected = true
// get value from redis
tempval, err := client.Get("counter").Result()
if err == redis.Nil {
fmt.Println("counter does not exists, set to 1")
countval = 1
client.Set("counter", countval, 0)
} else if err != nil {
panic(err)
} else {
cval, err2 := strconv.Atoi(tempval)
fmt.Printf("current counter value: %v Error: %s\n",cval, err2)
countval = cval + 1
client.Set("counter", strconv.Itoa(countval), 0)
}
} else {
connected = false
countval = 0
}
return connected, countval
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
// get current hostname
hostname, _ := os.Hostname()
redisstatus, counter := redisConnect()
// generate the HTML output
fmt.Fprintf(w, "<h1>Hello DBG IT Days</h1><br><i>Environment: </i><b>%s</b><br><i>Hostname: </i><b>%s</b><br><i>Redis Status: </i><b>%v</b><br><i>Redis Counter: </i><b>%v</b><br>", os.Getenv("NAME"), hostname, redisstatus, counter)
fmt.Println(hostname, "handled HTTP REQUEST at", time.Now())
}
func main() {
// start web server and listening on port 80
http.HandleFunc("/", indexHandler)
fmt.Println("Listening on port 80 for requests...")
http.ListenAndServe(":80", nil)
}