add list command to list all shortlinks
This commit is contained in:
parent
a001b4c8da
commit
b342eef1ef
26
main.go
26
main.go
@ -139,6 +139,32 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("500 Internal Server Error"))
|
||||
}
|
||||
}
|
||||
|
||||
if command == "list" {
|
||||
ctx := context.Background()
|
||||
keys, err := client.Keys(ctx, "*").Result()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(500)
|
||||
w.Write([]byte("500 Internal Server Error"))
|
||||
return
|
||||
}
|
||||
|
||||
resp := ""
|
||||
for _, key := range keys {
|
||||
value, err = client.Get(ctx, key).Result()
|
||||
if err == redis.Nil {
|
||||
w.WriteHeader(500)
|
||||
w.Write([]byte("500 Internal Server Error"))
|
||||
return
|
||||
}
|
||||
resp += key + ":" + value + "\n"
|
||||
}
|
||||
|
||||
w.WriteHeader(200)
|
||||
w.Write([]byte(resp))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func shortlinkHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
Loading…
Reference in New Issue
Block a user