add list command to list all shortlinks

This commit is contained in:
Akbar Rahman 2022-06-26 06:53:26 +01:00
parent a001b4c8da
commit b342eef1ef
Signed by: alvierahman90
GPG Key ID: 20609519444A1269
2 changed files with 29 additions and 2 deletions

26
main.go
View File

@ -139,6 +139,32 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("500 Internal Server Error")) 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) { func shortlinkHandler(w http.ResponseWriter, r *http.Request) {

View File

@ -7,6 +7,7 @@ import hashlib
import pathlib import pathlib
import os import os
import json import json
import sys
def get_args(): def get_args():
@ -64,8 +65,8 @@ def main(args):
).hexdigest() ).hexdigest()
} }
) )
print(r) print(r, file=sys.stderr)
print(r.content) print(r.content.decode().strip())
return 0 return 0