Compare commits

...

2 Commits

Author SHA1 Message Date
b342eef1ef add list command to list all shortlinks 2022-06-26 06:53:26 +01:00
a001b4c8da fix typo 2022-05-28 15:19:40 +01:00
3 changed files with 30 additions and 3 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

@@ -13,7 +13,7 @@ sus URL shortener
susmng [-s pls.cx] delete -l shortlink -v confirm susmng [-s pls.cx] delete -l shortlink -v confirm
`susmng` will assume the first server in the `secrets` section of the configuration file if the -s `susmng` will assume the first server in the `secrets` section of the configuration file if the `-s`
flag is not provided. flag is not provided.

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