mirror of
https://github.com/0ceanSlim/grain.git
synced 2024-10-29 17:16:31 +00:00
server package renamed relay
This commit is contained in:
parent
47dcc0bf4d
commit
81d73a5ec3
10
main.go
10
main.go
@ -5,9 +5,9 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"grain/server"
|
||||
"grain/server/db"
|
||||
"grain/server/utils"
|
||||
"grain/relay"
|
||||
"grain/relay/db"
|
||||
"grain/relay/utils"
|
||||
|
||||
"golang.org/x/net/websocket"
|
||||
)
|
||||
@ -26,8 +26,8 @@ func main() {
|
||||
}
|
||||
defer db.DisconnectDB()
|
||||
|
||||
// Start WebSocket server
|
||||
http.Handle("/", websocket.Handler(server.Listener))
|
||||
// Start WebSocket relay
|
||||
http.Handle("/", websocket.Handler(relay.Listener))
|
||||
fmt.Println("WebSocket server started on", config.Server.Address)
|
||||
err = http.ListenAndServe(config.Server.Address, nil)
|
||||
if err != nil {
|
||||
|
@ -4,14 +4,14 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
server "grain/server/types"
|
||||
relay "grain/relay/types"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
func HandleKind0(ctx context.Context, evt server.Event, collection *mongo.Collection) error {
|
||||
func HandleKind0(ctx context.Context, evt relay.Event, collection *mongo.Collection) error {
|
||||
// Replace the existing event if it has the same pubkey
|
||||
filter := bson.M{"pubkey": evt.PubKey}
|
||||
update := bson.M{
|
@ -4,12 +4,12 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
server "grain/server/types"
|
||||
relay "grain/relay/types"
|
||||
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
func HandleKind1(ctx context.Context, evt server.Event, collection *mongo.Collection) error {
|
||||
func HandleKind1(ctx context.Context, evt relay.Event, collection *mongo.Collection) error {
|
||||
// Insert event into MongoDB
|
||||
_, err := collection.InsertOne(ctx, evt)
|
||||
if err != nil {
|
@ -3,12 +3,12 @@ package kinds
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
server "grain/server/types"
|
||||
relay "grain/relay/types"
|
||||
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
func HandleUnknownKind(ctx context.Context, evt server.Event, collection *mongo.Collection) error {
|
||||
func HandleUnknownKind(ctx context.Context, evt relay.Event, collection *mongo.Collection) error {
|
||||
_, err := collection.InsertOne(ctx, evt)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error inserting unknown event into MongoDB: %v", err)
|
@ -1,10 +1,10 @@
|
||||
package server
|
||||
package relay
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
server "grain/server/types"
|
||||
relay "grain/relay/types"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
@ -12,10 +12,10 @@ import (
|
||||
)
|
||||
|
||||
// QueryEvents queries events from the MongoDB collection based on filters
|
||||
func QueryEvents(filters []server.Filter, client *mongo.Client, databaseName, collectionName string) ([]server.Event, error) {
|
||||
func QueryEvents(filters []relay.Filter, client *mongo.Client, databaseName, collectionName string) ([]relay.Event, error) {
|
||||
collection := client.Database(databaseName).Collection(collectionName)
|
||||
|
||||
var results []server.Event
|
||||
var results []relay.Event
|
||||
|
||||
for _, filter := range filters {
|
||||
filterBson := bson.M{}
|
||||
@ -55,7 +55,7 @@ func QueryEvents(filters []server.Filter, client *mongo.Client, databaseName, co
|
||||
defer cursor.Close(context.TODO())
|
||||
|
||||
for cursor.Next(context.TODO()) {
|
||||
var event server.Event
|
||||
var event relay.Event
|
||||
if err := cursor.Decode(&event); err != nil {
|
||||
return nil, fmt.Errorf("error decoding event: %v", err)
|
||||
}
|
@ -1,18 +1,18 @@
|
||||
package server
|
||||
package relay
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"grain/server/db"
|
||||
"grain/server/kinds"
|
||||
server "grain/server/types"
|
||||
"grain/server/utils"
|
||||
"grain/relay/db"
|
||||
"grain/relay/kinds"
|
||||
relay "grain/relay/types"
|
||||
"grain/relay/utils"
|
||||
|
||||
"golang.org/x/net/websocket"
|
||||
)
|
||||
|
||||
var subscriptions = make(map[string]server.Subscription)
|
||||
var subscriptions = make(map[string]relay.Subscription)
|
||||
|
||||
func Listener(ws *websocket.Conn) {
|
||||
defer ws.Close()
|
||||
@ -74,7 +74,7 @@ func handleEvent(ws *websocket.Conn, message []interface{}) {
|
||||
return
|
||||
}
|
||||
|
||||
var evt server.Event
|
||||
var evt relay.Event
|
||||
err = json.Unmarshal(eventBytes, &evt)
|
||||
if err != nil {
|
||||
fmt.Println("Error unmarshaling event data:", err)
|
||||
@ -87,7 +87,7 @@ func handleEvent(ws *websocket.Conn, message []interface{}) {
|
||||
fmt.Println("Event processed:", evt.ID)
|
||||
}
|
||||
|
||||
func HandleKind(ctx context.Context, evt server.Event, ws *websocket.Conn) {
|
||||
func HandleKind(ctx context.Context, evt relay.Event, ws *websocket.Conn) {
|
||||
if !utils.CheckSignature(evt) {
|
||||
sendOKResponse(ws, evt.ID, false, "invalid: signature verification failed")
|
||||
return
|
||||
@ -131,7 +131,7 @@ func handleReq(ws *websocket.Conn, message []interface{}) {
|
||||
return
|
||||
}
|
||||
|
||||
filters := make([]server.Filter, len(message)-2)
|
||||
filters := make([]relay.Filter, len(message)-2)
|
||||
for i, filter := range message[2:] {
|
||||
filterData, ok := filter.(map[string]interface{})
|
||||
if !ok {
|
||||
@ -139,7 +139,7 @@ func handleReq(ws *websocket.Conn, message []interface{}) {
|
||||
return
|
||||
}
|
||||
|
||||
var f server.Filter
|
||||
var f relay.Filter
|
||||
f.IDs = utils.ToStringArray(filterData["ids"])
|
||||
f.Authors = utils.ToStringArray(filterData["authors"])
|
||||
f.Kinds = utils.ToIntArray(filterData["kinds"])
|
||||
@ -151,7 +151,7 @@ func handleReq(ws *websocket.Conn, message []interface{}) {
|
||||
filters[i] = f
|
||||
}
|
||||
|
||||
subscriptions[subID] = server.Subscription{ID: subID, Filters: filters}
|
||||
subscriptions[subID] = relay.Subscription{ID: subID, Filters: filters}
|
||||
fmt.Println("Subscription added:", subID)
|
||||
|
||||
// Query the database with filters and send back the results
|
@ -1,4 +1,4 @@
|
||||
package server
|
||||
package relay
|
||||
|
||||
type Event struct {
|
||||
ID string `json:"id"`
|
@ -1,4 +1,4 @@
|
||||
package server
|
||||
package relay
|
||||
|
||||
import "time"
|
||||
|
@ -1,4 +1,4 @@
|
||||
package server
|
||||
package relay
|
||||
|
||||
type Subscription struct {
|
||||
ID string
|
@ -6,12 +6,12 @@ import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
|
||||
server "grain/server/types"
|
||||
relay "grain/relay/types"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec/v2"
|
||||
"github.com/btcsuite/btcd/btcec/v2/schnorr"
|
||||
)
|
||||
func SerializeEvent(evt server.Event) []byte {
|
||||
func SerializeEvent(evt relay.Event) []byte {
|
||||
eventData := []interface{}{
|
||||
0,
|
||||
evt.PubKey,
|
||||
@ -24,7 +24,7 @@ func SerializeEvent(evt server.Event) []byte {
|
||||
return serializedEvent
|
||||
}
|
||||
|
||||
func CheckSignature(evt server.Event) bool {
|
||||
func CheckSignature(evt relay.Event) bool {
|
||||
serializedEvent := SerializeEvent(evt)
|
||||
hash := sha256.Sum256(serializedEvent)
|
||||
eventID := hex.EncodeToString(hash[:])
|
Loading…
Reference in New Issue
Block a user