Adds database and collections if non exists, renamed project

This commit is contained in:
Chris kerr 2024-07-19 19:05:12 -04:00
parent 26fec6e371
commit 4a92b0ad25
2 changed files with 111 additions and 96 deletions

15
main.go
View File

@ -7,6 +7,7 @@ import (
"log"
"net/http"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"golang.org/x/net/websocket"
@ -118,6 +119,20 @@ func main() {
eventKind0Collection = client.Database("grain").Collection("event-kind0")
eventKind1Collection = client.Database("grain").Collection("event-kind1")
// Ensure collections exist by creating an index (which will implicitly create the collections)
indexModel := mongo.IndexModel{
Keys: bson.D{{Key: "id", Value: 1}},
Options: options.Index().SetUnique(true),
}
_, err = eventKind0Collection.Indexes().CreateOne(context.TODO(), indexModel)
if err != nil {
log.Fatal("Failed to create index on event-kind0: ", err)
}
_, err = eventKind1Collection.Indexes().CreateOne(context.TODO(), indexModel)
if err != nil {
log.Fatal("Failed to create index on event-kind1: ", err)
}
// Start WebSocket server
http.Handle("/", websocket.Handler(handler))
fmt.Println("WebSocket server started on :8080")

View File

@ -1,6 +1,6 @@
# GRAIN 🌾 WIP
**Go Relay and Information Network**
**Go Relay Archetecture for Implementing Nostr**
GRAIN is an open-source Nostr relay implementation written in Go. This project aims to provide a robust and efficient Nostr relay that supports the NIP-01 protocol, focusing on processing user metadata and text notes.