-
Notifications
You must be signed in to change notification settings - Fork 3
/
hello-app.go
63 lines (55 loc) · 1.46 KB
/
hello-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
package main
import (
"fmt"
"github.com/go-redis/redis"
"log"
"net/http"
"os"
"strconv"
"strings"
)
// global variables
var defaultKey string = getEnv("HELLO_MSG", "world")
var rdb *redis.Client
func handler(w http.ResponseWriter, r *http.Request) {
log.Print("Request received")
key := defaultKey
uriSegments := strings.Split(r.URL.Path, "/")
if uriSegments[1] != "" {
key = uriSegments[1]
}
counter, err := rdb.Incr(key).Result()
if err != nil {
fmt.Fprintf(w, "<h1>Hello, stranger!</h1>")
log.Println("Error: " + err.Error())
} else {
fmt.Fprintf(w, "<h1>Hello %s %s!</h1>", key, strconv.FormatInt(counter, 10))
}
}
func main() {
log.Print("Server starting...")
address := getEnv("REDIS_ADDRESS", "localhost:6379")
password := getEnv("REDIS_PASSWORD", "")
db, _ := strconv.Atoi(getEnv("REDIS_DB", "0"))
rdb = redis.NewClient(&redis.Options{
Addr: address,
Password: password,
DB: db,
})
_, err := rdb.Ping().Result()
if err != nil {
panic(err)
}
log.Print("Connected to Redis: " + address)
log.Print("Server started.")
mux := http.NewServeMux()
mux.HandleFunc("/", handler)
mux.HandleFunc("/*", handler)
log.Fatal(http.ListenAndServe(":8080", mux))
}
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}