mirror of
https://github.com/0ceanSlim/grain.git
synced 2024-11-22 16:47:13 +00:00
Adds database and collections if non exists, renamed project
This commit is contained in:
parent
26fec6e371
commit
4a92b0ad25
205
main.go
205
main.go
@ -7,6 +7,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
"golang.org/x/net/websocket"
|
"golang.org/x/net/websocket"
|
||||||
@ -14,13 +15,13 @@ import (
|
|||||||
|
|
||||||
// Event represents the structure of the incoming events
|
// Event represents the structure of the incoming events
|
||||||
type Event struct {
|
type Event struct {
|
||||||
CreatedAt int64 `json:"created_at"`
|
CreatedAt int64 `json:"created_at"`
|
||||||
Kind int `json:"kind"`
|
Kind int `json:"kind"`
|
||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
Tags []string `json:"tags"`
|
Tags []string `json:"tags"`
|
||||||
PubKey string `json:"pubkey"`
|
PubKey string `json:"pubkey"`
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Sig string `json:"sig"`
|
Sig string `json:"sig"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Database client and collections
|
// Database client and collections
|
||||||
@ -29,107 +30,121 @@ var eventKind0Collection *mongo.Collection
|
|||||||
var eventKind1Collection *mongo.Collection
|
var eventKind1Collection *mongo.Collection
|
||||||
|
|
||||||
func handler(ws *websocket.Conn) {
|
func handler(ws *websocket.Conn) {
|
||||||
var msg string
|
var msg string
|
||||||
for {
|
for {
|
||||||
err := websocket.Message.Receive(ws, &msg)
|
err := websocket.Message.Receive(ws, &msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error receiving message:", err)
|
fmt.Println("Error receiving message:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Println("Received message:", msg)
|
fmt.Println("Received message:", msg)
|
||||||
|
|
||||||
// Parse the received message
|
// Parse the received message
|
||||||
var event []interface{}
|
var event []interface{}
|
||||||
err = json.Unmarshal([]byte(msg), &event)
|
err = json.Unmarshal([]byte(msg), &event)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error parsing message:", err)
|
fmt.Println("Error parsing message:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(event) < 2 || event[0] != "EVENT" {
|
if len(event) < 2 || event[0] != "EVENT" {
|
||||||
fmt.Println("Invalid event format")
|
fmt.Println("Invalid event format")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert the event map to an Event struct
|
// Convert the event map to an Event struct
|
||||||
eventData, ok := event[1].(map[string]interface{})
|
eventData, ok := event[1].(map[string]interface{})
|
||||||
if !ok {
|
if !ok {
|
||||||
fmt.Println("Invalid event data format")
|
fmt.Println("Invalid event data format")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
eventBytes, err := json.Marshal(eventData)
|
eventBytes, err := json.Marshal(eventData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error marshaling event data:", err)
|
fmt.Println("Error marshaling event data:", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
var evt Event
|
var evt Event
|
||||||
err = json.Unmarshal(eventBytes, &evt)
|
err = json.Unmarshal(eventBytes, &evt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error unmarshaling event data:", err)
|
fmt.Println("Error unmarshaling event data:", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store the event in the appropriate MongoDB collection
|
// Store the event in the appropriate MongoDB collection
|
||||||
var collection *mongo.Collection
|
var collection *mongo.Collection
|
||||||
switch evt.Kind {
|
switch evt.Kind {
|
||||||
case 0:
|
case 0:
|
||||||
collection = eventKind0Collection
|
collection = eventKind0Collection
|
||||||
case 1:
|
case 1:
|
||||||
collection = eventKind1Collection
|
collection = eventKind1Collection
|
||||||
default:
|
default:
|
||||||
fmt.Println("Unknown event kind:", evt.Kind)
|
fmt.Println("Unknown event kind:", evt.Kind)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = collection.InsertOne(context.TODO(), evt)
|
_, err = collection.InsertOne(context.TODO(), evt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error inserting event into MongoDB:", err)
|
fmt.Println("Error inserting event into MongoDB:", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Inserted event into MongoDB:", evt.ID)
|
fmt.Println("Inserted event into MongoDB:", evt.ID)
|
||||||
|
|
||||||
err = websocket.Message.Send(ws, "Echo: "+msg)
|
err = websocket.Message.Send(ws, "Echo: "+msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error sending message:", err)
|
fmt.Println("Error sending message:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Initialize MongoDB client
|
// Initialize MongoDB client
|
||||||
var err error
|
var err error
|
||||||
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017/")
|
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017/")
|
||||||
client, err = mongo.Connect(context.TODO(), clientOptions)
|
client, err = mongo.Connect(context.TODO(), clientOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check the connection
|
// Check the connection
|
||||||
err = client.Ping(context.TODO(), nil)
|
err = client.Ping(context.TODO(), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
fmt.Println("Connected to MongoDB!")
|
fmt.Println("Connected to MongoDB!")
|
||||||
|
|
||||||
// Initialize collections
|
// Initialize collections
|
||||||
eventKind0Collection = client.Database("grain").Collection("event-kind0")
|
eventKind0Collection = client.Database("grain").Collection("event-kind0")
|
||||||
eventKind1Collection = client.Database("grain").Collection("event-kind1")
|
eventKind1Collection = client.Database("grain").Collection("event-kind1")
|
||||||
|
|
||||||
// Start WebSocket server
|
// Ensure collections exist by creating an index (which will implicitly create the collections)
|
||||||
http.Handle("/", websocket.Handler(handler))
|
indexModel := mongo.IndexModel{
|
||||||
fmt.Println("WebSocket server started on :8080")
|
Keys: bson.D{{Key: "id", Value: 1}},
|
||||||
err = http.ListenAndServe(":8080", nil)
|
Options: options.Index().SetUnique(true),
|
||||||
if err != nil {
|
}
|
||||||
fmt.Println("Error starting server:", err)
|
_, 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)
|
||||||
|
}
|
||||||
|
|
||||||
// Disconnect MongoDB client on exit
|
// Start WebSocket server
|
||||||
defer func() {
|
http.Handle("/", websocket.Handler(handler))
|
||||||
if err = client.Disconnect(context.TODO()); err != nil {
|
fmt.Println("WebSocket server started on :8080")
|
||||||
log.Fatal(err)
|
err = http.ListenAndServe(":8080", nil)
|
||||||
}
|
if err != nil {
|
||||||
}()
|
fmt.Println("Error starting server:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Disconnect MongoDB client on exit
|
||||||
|
defer func() {
|
||||||
|
if err = client.Disconnect(context.TODO()); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
# GRAIN 🌾 WIP
|
# 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.
|
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.
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user