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