From d1b3750c87cb793c6a0f92d204471cd525505dca Mon Sep 17 00:00:00 2001 From: 0ceanSlim Date: Tue, 17 Sep 2024 10:35:31 -0400 Subject: [PATCH] move kinds to db package, refactor new mongo package --- main.go | 6 +-- server/db/{ => mongo}/dbMongo.go | 2 +- .../kind5.go => db/mongo/kinds/delete.go} | 2 +- .../kind2.go => db/mongo/kinds/deprecated.go} | 2 +- .../{handlers => db/mongo}/kinds/regular.go | 0 .../mongo}/kinds/replaceable.go | 0 .../mongo}/kinds/replaceableParameters.go | 0 .../{handlers => db/mongo}/kinds/unknown.go | 0 server/db/{ => mongo}/queryMongo.go | 2 +- server/db/{ => mongo}/storeMongo.go | 12 ++--- server/handlers/event.go | 4 +- server/handlers/kinds/kind0.go | 50 ------------------- server/handlers/kinds/kind1.go | 24 --------- server/handlers/req.go | 4 +- 14 files changed, 17 insertions(+), 91 deletions(-) rename server/db/{ => mongo}/dbMongo.go (99%) rename server/{handlers/kinds/kind5.go => db/mongo/kinds/delete.go} (97%) rename server/{handlers/kinds/kind2.go => db/mongo/kinds/deprecated.go} (76%) rename server/{handlers => db/mongo}/kinds/regular.go (100%) rename server/{handlers => db/mongo}/kinds/replaceable.go (100%) rename server/{handlers => db/mongo}/kinds/replaceableParameters.go (100%) rename server/{handlers => db/mongo}/kinds/unknown.go (100%) rename server/db/{ => mongo}/queryMongo.go (99%) rename server/db/{ => mongo}/storeMongo.go (81%) delete mode 100644 server/handlers/kinds/kind0.go delete mode 100644 server/handlers/kinds/kind1.go diff --git a/main.go b/main.go index 43b476e..39e1a3a 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,7 @@ import ( "grain/config" configTypes "grain/config/types" relay "grain/server" - "grain/server/db" + "grain/server/db/mongo" "grain/server/utils" "log" "net/http" @@ -41,7 +41,7 @@ func main() { config.SetResourceLimit(&cfg.ResourceLimits) // Apply limits once before starting the server - client, err := db.InitDB(cfg) + client, err := mongo.InitDB(cfg) if err != nil { log.Fatal("Error initializing database: ", err) } @@ -71,7 +71,7 @@ func main() { case <-signalChan: log.Println("Shutting down server...") server.Close() // Stop the server - db.DisconnectDB(client) // Disconnect from MongoDB + mongo.DisconnectDB(client) // Disconnect from MongoDB wg.Wait() // Wait for all goroutines to finish return } diff --git a/server/db/dbMongo.go b/server/db/mongo/dbMongo.go similarity index 99% rename from server/db/dbMongo.go rename to server/db/mongo/dbMongo.go index 9709075..f5e5832 100644 --- a/server/db/dbMongo.go +++ b/server/db/mongo/dbMongo.go @@ -1,4 +1,4 @@ -package db +package mongo import ( "context" diff --git a/server/handlers/kinds/kind5.go b/server/db/mongo/kinds/delete.go similarity index 97% rename from server/handlers/kinds/kind5.go rename to server/db/mongo/kinds/delete.go index 7217c18..08614b0 100644 --- a/server/handlers/kinds/kind5.go +++ b/server/db/mongo/kinds/delete.go @@ -12,7 +12,7 @@ import ( "golang.org/x/net/websocket" ) -func HandleKind5(ctx context.Context, evt relay.Event, dbClient *mongo.Client, ws *websocket.Conn) error { +func HandleDeleteKind(ctx context.Context, evt relay.Event, dbClient *mongo.Client, ws *websocket.Conn) error { for _, tag := range evt.Tags { if len(tag) < 2 { continue diff --git a/server/handlers/kinds/kind2.go b/server/db/mongo/kinds/deprecated.go similarity index 76% rename from server/handlers/kinds/kind2.go rename to server/db/mongo/kinds/deprecated.go index cdb9ee6..9c6db06 100644 --- a/server/handlers/kinds/kind2.go +++ b/server/db/mongo/kinds/deprecated.go @@ -8,7 +8,7 @@ import ( "golang.org/x/net/websocket" ) -func HandleKind2(ctx context.Context, evt relay.Event, ws *websocket.Conn) error { +func HandleDeprecatedKind(ctx context.Context, evt relay.Event, ws *websocket.Conn) error { // Send an OK message to indicate the event was not accepted response.SendOK(ws, evt.ID, false, "invalid: kind 2 is deprecated, use kind 10002 (NIP65)") diff --git a/server/handlers/kinds/regular.go b/server/db/mongo/kinds/regular.go similarity index 100% rename from server/handlers/kinds/regular.go rename to server/db/mongo/kinds/regular.go diff --git a/server/handlers/kinds/replaceable.go b/server/db/mongo/kinds/replaceable.go similarity index 100% rename from server/handlers/kinds/replaceable.go rename to server/db/mongo/kinds/replaceable.go diff --git a/server/handlers/kinds/replaceableParameters.go b/server/db/mongo/kinds/replaceableParameters.go similarity index 100% rename from server/handlers/kinds/replaceableParameters.go rename to server/db/mongo/kinds/replaceableParameters.go diff --git a/server/handlers/kinds/unknown.go b/server/db/mongo/kinds/unknown.go similarity index 100% rename from server/handlers/kinds/unknown.go rename to server/db/mongo/kinds/unknown.go diff --git a/server/db/queryMongo.go b/server/db/mongo/queryMongo.go similarity index 99% rename from server/db/queryMongo.go rename to server/db/mongo/queryMongo.go index ed3077a..9e47cb5 100644 --- a/server/db/queryMongo.go +++ b/server/db/mongo/queryMongo.go @@ -1,4 +1,4 @@ -package db +package mongo import ( "context" diff --git a/server/db/storeMongo.go b/server/db/mongo/storeMongo.go similarity index 81% rename from server/db/storeMongo.go rename to server/db/mongo/storeMongo.go index 21f643e..3ca833e 100644 --- a/server/db/storeMongo.go +++ b/server/db/mongo/storeMongo.go @@ -1,9 +1,9 @@ -package db +package mongo import ( "context" "fmt" - "grain/server/handlers/kinds" + "grain/server/db/mongo/kinds" "grain/server/handlers/response" nostr "grain/server/types" @@ -16,15 +16,15 @@ func StoreMongoEvent(ctx context.Context, evt nostr.Event, ws *websocket.Conn) { var err error switch { case evt.Kind == 0: - err = kinds.HandleKind0(ctx, evt, collection, ws) + err = kinds.HandleReplaceableKind(ctx, evt, collection, ws) case evt.Kind == 1: - err = kinds.HandleKind1(ctx, evt, collection, ws) + err = kinds.HandleRegularKind(ctx, evt, collection, ws) case evt.Kind == 2: - err = kinds.HandleKind2(ctx, evt, ws) + err = kinds.HandleDeprecatedKind(ctx, evt, ws) case evt.Kind == 3: err = kinds.HandleReplaceableKind(ctx, evt, collection, ws) case evt.Kind == 5: - err = kinds.HandleKind5(ctx, evt, GetClient(), ws) + err = kinds.HandleDeleteKind(ctx, evt, GetClient(), ws) case evt.Kind >= 4 && evt.Kind < 45: err = kinds.HandleRegularKind(ctx, evt, collection, ws) case evt.Kind >= 1000 && evt.Kind < 10000: diff --git a/server/handlers/event.go b/server/handlers/event.go index 551f143..9dc7b12 100644 --- a/server/handlers/event.go +++ b/server/handlers/event.go @@ -5,7 +5,7 @@ import ( "encoding/json" "fmt" "grain/config" - "grain/server/db" + "grain/server/db/mongo" "grain/server/handlers/response" "grain/server/utils" @@ -61,7 +61,7 @@ func HandleEvent(ws *websocket.Conn, message []interface{}) { } // This is where I'll handle storage for multiple database types in the future - db.StoreMongoEvent(context.TODO(), evt, ws) + mongo.StoreMongoEvent(context.TODO(), evt, ws) fmt.Println("Event processed:", evt.ID) diff --git a/server/handlers/kinds/kind0.go b/server/handlers/kinds/kind0.go deleted file mode 100644 index ec1826e..0000000 --- a/server/handlers/kinds/kind0.go +++ /dev/null @@ -1,50 +0,0 @@ -package kinds - -import ( - "context" - "fmt" - "grain/server/handlers/response" - relay "grain/server/types" - - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/options" - "golang.org/x/net/websocket" -) - -func HandleKind0(ctx context.Context, evt relay.Event, collection *mongo.Collection, ws *websocket.Conn) error { - filter := bson.M{"pubkey": evt.PubKey} - var existingEvent relay.Event - err := collection.FindOne(ctx, filter).Decode(&existingEvent) - if err != nil && err != mongo.ErrNoDocuments { - return fmt.Errorf("error finding existing event: %v", err) - } - - if err != mongo.ErrNoDocuments { - if existingEvent.CreatedAt >= evt.CreatedAt { - response.SendOK(ws, evt.ID, false, "blocked: a newer kind 0 event already exists for this pubkey") - return nil - } - } - - update := bson.M{ - "$set": bson.M{ - "id": evt.ID, - "created_at": evt.CreatedAt, - "kind": evt.Kind, - "tags": evt.Tags, - "content": evt.Content, - "sig": evt.Sig, - }, - } - - opts := options.Update().SetUpsert(true) - _, err = collection.UpdateOne(ctx, filter, update, opts) - if err != nil { - response.SendOK(ws, evt.ID, false, "error: could not connect to the database") - return fmt.Errorf("error updating/inserting event kind 0 into MongoDB: %v", err) - } - response.SendOK(ws, evt.ID, true, "") - fmt.Println("Upserted event kind 0 into MongoDB:", evt.ID) - return nil -} diff --git a/server/handlers/kinds/kind1.go b/server/handlers/kinds/kind1.go deleted file mode 100644 index 5f2fdcc..0000000 --- a/server/handlers/kinds/kind1.go +++ /dev/null @@ -1,24 +0,0 @@ -// kinds/kind1.go -package kinds - -import ( - "context" - "fmt" - "grain/server/handlers/response" - relay "grain/server/types" - - "go.mongodb.org/mongo-driver/mongo" - "golang.org/x/net/websocket" -) - -func HandleKind1(ctx context.Context, evt relay.Event, collection *mongo.Collection, ws *websocket.Conn) error { - _, err := collection.InsertOne(ctx, evt) - if err != nil { - response.SendOK(ws, evt.ID, false, "error: could not connect to the database") - return fmt.Errorf("error inserting event into MongoDB: %v", err) - } - - fmt.Println("Inserted event kind 1 into MongoDB:", evt.ID) - response.SendOK(ws, evt.ID, true, "") - return nil -} diff --git a/server/handlers/req.go b/server/handlers/req.go index 9b7b97b..4c34ccc 100644 --- a/server/handlers/req.go +++ b/server/handlers/req.go @@ -4,7 +4,7 @@ import ( "encoding/json" "fmt" "grain/config" - "grain/server/db" + "grain/server/db/mongo" "grain/server/handlers/response" relay "grain/server/types" "grain/server/utils" @@ -113,7 +113,7 @@ func processRequest(ws *websocket.Conn, message []interface{}) { fmt.Printf("Subscription updated: %s with %d filters\n", subID, len(filters)) // Query the database with filters and send back the results - queriedEvents, err := db.QueryEvents(filters, db.GetClient(), "grain") + queriedEvents, err := mongo.QueryEvents(filters, mongo.GetClient(), "grain") if err != nil { fmt.Println("Error querying events:", err) response.SendClosed(ws, subID, "error: could not query events")