-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
70 lines (60 loc) · 1.8 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
package main
import (
"fmt"
"io"
"net/http"
"time"
redis "gopkg.in/redis.v3"
)
var redisClient = redis.NewClient(&redis.Options{
Addr: "redis:6379",
Password: "", // no password set
DB: 0,
})
func main() {
start()
http.HandleFunc("/index", indexHandler)
http.HandleFunc("/healthz", healthHandler)
http.HandleFunc("/play", playHandler)
http.HandleFunc("/ready", readyHandler)
http.ListenAndServe(":8080", nil)
}
func start() {
// call service that gets and stores songs
topTracks := getSongs()
time.Sleep(60 * time.Second)
redisClient.Set("example song", "exampleid", 0)
for _, track := range topTracks.Tracks {
redisClient.Set(track.Title, track.Id, 0)
}
}
func indexHandler(res http.ResponseWriter, req *http.Request) {
io.WriteString(res, "Hello world from Puffy!\n")
fmt.Println("Puffy said hello")
}
func healthHandler(res http.ResponseWriter, req *http.Request) {
io.WriteString(res, "Puffy is OK!\n")
fmt.Println("A health check was performed")
}
func playHandler(res http.ResponseWriter, req *http.Request) {
song, _ := redisClient.RandomKey().Result()
io.WriteString(res, "Play \""+song+"\" by One Direction!")
fmt.Printf("\nPlay %s by One Direction for us!\n", song)
}
func readyHandler(res http.ResponseWriter, req *http.Request) {
fmt.Println("\nA readiness check was performed at" + time.Now().String())
song, err := redisClient.RandomKey().Result()
if err != nil {
fmt.Println("Looks like there was a problem getting stuff from redis")
res.WriteHeader(http.StatusServiceUnavailable)
io.WriteString(res, "Problem with redis command.")
}
if song == "" {
fmt.Println("No songs in redis to find")
res.WriteHeader(http.StatusServiceUnavailable)
io.WriteString(res, "No songs!")
}
res.WriteHeader(http.StatusOK)
io.WriteString(res, "JAM ON!")
fmt.Println("JAM ON BRO")
}