From 25440558e9b0799ff3291d5565ce2f6439cbed8b Mon Sep 17 00:00:00 2001 From: Asara Date: Mon, 1 May 2023 18:41:28 -0400 Subject: [PATCH] Clean up redis connections --- main.go | 13 ++++++++++++- nostr/nostr.go | 22 ++++------------------ redis/redis.go | 9 +++++++-- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/main.go b/main.go index d2ad93d..09b1831 100644 --- a/main.go +++ b/main.go @@ -7,9 +7,21 @@ import ( "git.minhas.io/asara/well-goknown/logger" "git.minhas.io/asara/well-goknown/matrix" "git.minhas.io/asara/well-goknown/nostr" + "git.minhas.io/asara/well-goknown/redis" ) func main() { + var err error + l := logger.Get() + // connect to redis + redis.NostrRedisConn, err = redis.New("localhost:6379", "", redis.NostrKeyspace) + if err != nil { + l.Fatal().Msg("unable to connect to redis") + } + redis.LndRedisConn, err = redis.New("localhost:6379", "", redis.LndKeyspace) + if err != nil { + l.Fatal().Msg("unable to connect to redis") + } /* nostrTest := nostr.NostrRequest{ Name: "asara", @@ -19,7 +31,6 @@ func main() { nostr.AddNostrAddr(nostrTest) */ // matrix endpoints - l := logger.Get() l.Debug().Msg("enabling matrix server endpoint") http.HandleFunc("/.well-known/matrix/server", matrix.MatrixServer) l.Debug().Msg("enabling matrix client endpoint") diff --git a/nostr/nostr.go b/nostr/nostr.go index 9d60e2d..d2f721d 100644 --- a/nostr/nostr.go +++ b/nostr/nostr.go @@ -37,12 +37,7 @@ func RequestNostrAddr(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "html/nostr_form.html") case "POST": r.ParseForm() - redisCli, err := redis.New("localhost:6379", "", redis.NostrDb) - if err != nil { - l.Error().Msg("unable to connect to redis") - w.WriteHeader(http.StatusInternalServerError) - return - } + redisCli := redis.NostrRedisConn // check if the user already exists rKey := getRkey("verified", r.FormValue("Name"), getHostname(r.Host)) exists := redisCli.Client.Exists(ctx, rKey) @@ -98,19 +93,13 @@ func RequestNostrAddr(w http.ResponseWriter, r *http.Request) { func GetNostrAddr(w http.ResponseWriter, r *http.Request) { ctx := context.TODO() - l := logger.Get() // get query string for username r.ParseForm() + requestedName := r.FormValue("name") // connect to redis - redisCli, err := redis.New("localhost:6379", "", redis.NostrDb) - if err != nil { - l.Error().Msg("unable to connect to redis") - w.WriteHeader(http.StatusInternalServerError) - return - } + redisCli := redis.NostrRedisConn - requestedName := r.FormValue("name") // search for user@domain search := getRkey("verified", requestedName, getHostname(r.Host)) addr, err := redisCli.Client.Get(ctx, search).Result() @@ -149,10 +138,7 @@ func AddNostrAddr(n NostrRequest) { } jsonUser, _ := json.Marshal(user) - redisCli, err := redis.New("localhost:6379", "", redis.NostrDb) - if err != nil { - l.Error().Msg("unable to connect to redis") - } + redisCli := redis.NostrRedisConn nameKey := getRkey("verified", n.Name, n.Hostname) enc := b64.StdEncoding.EncodeToString([]byte(jsonUser)) err = redisCli.Client.Set(ctx, nameKey, enc, 0).Err() diff --git a/redis/redis.go b/redis/redis.go index 2274c7e..e892e60 100644 --- a/redis/redis.go +++ b/redis/redis.go @@ -7,14 +7,19 @@ import ( ) const ( - NostrDb = iota - LndDb = iota + NostrKeyspace = iota + LndKeyspace = iota ) type Redis struct { Client *redis.Client } +var ( + NostrRedisConn *Redis + LndRedisConn *Redis +) + func New(address string, password string, database int) (*Redis, error) { client := redis.NewClient(&redis.Options{ Addr: address,