Add prefixes to redis keys

This commit is contained in:
Amarpreet Minhas 2023-02-04 20:43:38 -05:00
parent dd22f01dac
commit 314f0a6cd7

View file

@ -31,14 +31,14 @@ func RequestNostrAddr(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "html/nostr_form.html") http.ServeFile(w, r, "html/nostr_form.html")
case "POST": case "POST":
r.ParseForm() r.ParseForm()
rKey := getRkey(r.FormValue("Name"), getHostname(r.Host)) rKey := getRkey("verified", r.FormValue("Name"), getHostname(r.Host))
// check if the user already exists // check if the user already exists
redis, err := redis.New("localhost:6379", "", redis.NostrDb) redisCli, err := redis.New("localhost:6379", "", redis.NostrDb)
if err != nil { if err != nil {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
return return
} }
exists := redis.Client.Exists(rKey) exists := redisCli.Client.Exists(rKey)
if exists.Val() == 1 { if exists.Val() == 1 {
w.WriteHeader(http.StatusConflict) w.WriteHeader(http.StatusConflict)
return return
@ -71,6 +71,11 @@ func RequestNostrAddr(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusServiceUnavailable) w.WriteHeader(http.StatusServiceUnavailable)
return return
} }
requestKey := getRkey("requested", r.FormValue("Name"), getHostname(r.Host))
err = redisCli.Client.Set(requestKey, jsonUser, redis.NostrDb).Err()
if err != nil {
fmt.Println("FAILED")
}
spew.Dump(paymentReq) spew.Dump(paymentReq)
spew.Dump(exp) spew.Dump(exp)
@ -82,7 +87,7 @@ func GetNostrAddr(w http.ResponseWriter, r *http.Request) {
r.ParseForm() r.ParseForm()
// connect to redis // connect to redis
redis, err := redis.New("localhost:6379", "", redis.NostrDb) redisCli, err := redis.New("localhost:6379", "", redis.NostrDb)
if err != nil { if err != nil {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
return return
@ -90,8 +95,8 @@ func GetNostrAddr(w http.ResponseWriter, r *http.Request) {
requestedName := r.FormValue("name") requestedName := r.FormValue("name")
// search for user@domain // search for user@domain
search := getRkey(requestedName, getHostname(r.Host)) search := getRkey("verified", requestedName, getHostname(r.Host))
addr, err := redis.Client.Get(search).Result() addr, err := redisCli.Client.Get(search).Result()
if err != nil { if err != nil {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
return return
@ -124,7 +129,7 @@ func AddNostrAddr(n NostrRequest) {
if err != nil { if err != nil {
fmt.Println("Failed to connect to redis") fmt.Println("Failed to connect to redis")
} }
nameKey := getRkey(n.Name, n.Hostname) nameKey := getRkey("verified", n.Name, n.Hostname)
err = r.Client.Set(nameKey, jsonUser, redis.NostrDb).Err() err = r.Client.Set(nameKey, jsonUser, redis.NostrDb).Err()
if err != nil { if err != nil {
fmt.Println("FAILED") fmt.Println("FAILED")
@ -149,9 +154,9 @@ func convertNpubToHex(npub string) (string, error) {
} }
func getRkey(user string, hostname string) string { func getRkey(prefix string, user string, hostname string) string {
if strings.Contains(hostname, ":") { if strings.Contains(hostname, ":") {
hostname = strings.Split(hostname, ":")[0] hostname = strings.Split(hostname, ":")[0]
} }
return fmt.Sprintf("%s@%s", user, hostname) return fmt.Sprintf("%s:%s@%s", prefix, user, hostname)
} }