-
Notifications
You must be signed in to change notification settings - Fork 1
/
redis.go
66 lines (55 loc) · 1.51 KB
/
redis.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
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"strings"
"github.com/redis/go-redis/v9"
)
var (
ctx = context.Background()
)
func redisHandler(w http.ResponseWriter, r *http.Request) {
service := r.URL.Query().Get("service")
redisRoute := strings.ReplaceAll(service, "/", "")
redisConnectionStr := fmt.Sprintf("%s:6379", redisRoute)
log.Print(fmt.Sprintf("Using %s as the connstring", redisConnectionStr))
fmt.Fprintf(w, redisConnector(redisConnectionStr, redisRoute))
}
func cleanRedisOutput(r *redis.StringCmd) string {
redistoString := r.String()
cleanString := strings.ReplaceAll(redistoString, "get ", "")
redisVals := strings.ReplaceAll(cleanString, " ", "")
return redisVals
}
func redisConnector(connectionString string, version string) string {
client := redis.NewClient(&redis.Options{
Addr: connectionString,
Password: "",
DB: 0,
})
if err := client.Ping(ctx).Err(); err != nil {
panic(err)
}
for _, e := range os.Environ() {
pair := strings.SplitN(e, "=", 2)
err := client.Set(ctx, pair[0], pair[1], 0).Err()
if err != nil {
log.Print(err)
}
}
var cursor uint64
results, _, _ := client.Scan(ctx, cursor, "LAGOON_*", 100).Result()
var values []string
for _, res := range results {
redisKeyVals := client.Get(ctx, res)
redisVals := cleanRedisOutput(redisKeyVals)
values = append(values, redisVals)
}
keyVals := connectorKeyValues(values)
host := fmt.Sprintf(`"SERVICE_HOST=%s"`, version)
redisData := host + "\n" + keyVals
return redisData
}