mirror of
https://github.com/0ceanSlim/grain.git
synced 2024-11-21 16:17:13 +00:00
first commit
This commit is contained in:
commit
26fec6e371
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/tmp
|
0
config.example.yml
Normal file
0
config.example.yml
Normal file
21
go.mod
Normal file
21
go.mod
Normal file
@ -0,0 +1,21 @@
|
||||
module GRAIN
|
||||
|
||||
go 1.22.2
|
||||
|
||||
require (
|
||||
go.mongodb.org/mongo-driver v1.16.0
|
||||
golang.org/x/net v0.27.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/golang/snappy v0.0.4 // indirect
|
||||
github.com/klauspost/compress v1.13.6 // indirect
|
||||
github.com/montanaflynn/stats v0.7.1 // indirect
|
||||
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
|
||||
github.com/xdg-go/scram v1.1.2 // indirect
|
||||
github.com/xdg-go/stringprep v1.0.4 // indirect
|
||||
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
|
||||
golang.org/x/crypto v0.25.0 // indirect
|
||||
golang.org/x/sync v0.7.0 // indirect
|
||||
golang.org/x/text v0.16.0 // indirect
|
||||
)
|
52
go.sum
Normal file
52
go.sum
Normal file
@ -0,0 +1,52 @@
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
|
||||
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc=
|
||||
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
|
||||
github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE=
|
||||
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
|
||||
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
|
||||
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
|
||||
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
|
||||
github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4=
|
||||
github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8=
|
||||
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=
|
||||
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA=
|
||||
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
go.mongodb.org/mongo-driver v1.16.0 h1:tpRsfBJMROVHKpdGyc1BBEzzjDUWjItxbVSZ8Ls4BQ4=
|
||||
go.mongodb.org/mongo-driver v1.16.0/go.mod h1:oB6AhJQvFQL4LEHyXi6aJzQJtBiTQHiAd83l0GdFaiw=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
|
||||
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
|
||||
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
|
||||
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
|
||||
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
|
||||
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
21
license
Normal file
21
license
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) [2024] [OceanSlim]
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
135
main.go
Normal file
135
main.go
Normal file
@ -0,0 +1,135 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"golang.org/x/net/websocket"
|
||||
)
|
||||
|
||||
// Event represents the structure of the incoming events
|
||||
type Event struct {
|
||||
CreatedAt int64 `json:"created_at"`
|
||||
Kind int `json:"kind"`
|
||||
Content string `json:"content"`
|
||||
Tags []string `json:"tags"`
|
||||
PubKey string `json:"pubkey"`
|
||||
ID string `json:"id"`
|
||||
Sig string `json:"sig"`
|
||||
}
|
||||
|
||||
// Database client and collections
|
||||
var client *mongo.Client
|
||||
var eventKind0Collection *mongo.Collection
|
||||
var eventKind1Collection *mongo.Collection
|
||||
|
||||
func handler(ws *websocket.Conn) {
|
||||
var msg string
|
||||
for {
|
||||
err := websocket.Message.Receive(ws, &msg)
|
||||
if err != nil {
|
||||
fmt.Println("Error receiving message:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("Received message:", msg)
|
||||
|
||||
// Parse the received message
|
||||
var event []interface{}
|
||||
err = json.Unmarshal([]byte(msg), &event)
|
||||
if err != nil {
|
||||
fmt.Println("Error parsing message:", err)
|
||||
return
|
||||
}
|
||||
|
||||
if len(event) < 2 || event[0] != "EVENT" {
|
||||
fmt.Println("Invalid event format")
|
||||
continue
|
||||
}
|
||||
|
||||
// Convert the event map to an Event struct
|
||||
eventData, ok := event[1].(map[string]interface{})
|
||||
if !ok {
|
||||
fmt.Println("Invalid event data format")
|
||||
continue
|
||||
}
|
||||
eventBytes, err := json.Marshal(eventData)
|
||||
if err != nil {
|
||||
fmt.Println("Error marshaling event data:", err)
|
||||
continue
|
||||
}
|
||||
|
||||
var evt Event
|
||||
err = json.Unmarshal(eventBytes, &evt)
|
||||
if err != nil {
|
||||
fmt.Println("Error unmarshaling event data:", err)
|
||||
continue
|
||||
}
|
||||
|
||||
// Store the event in the appropriate MongoDB collection
|
||||
var collection *mongo.Collection
|
||||
switch evt.Kind {
|
||||
case 0:
|
||||
collection = eventKind0Collection
|
||||
case 1:
|
||||
collection = eventKind1Collection
|
||||
default:
|
||||
fmt.Println("Unknown event kind:", evt.Kind)
|
||||
continue
|
||||
}
|
||||
|
||||
_, err = collection.InsertOne(context.TODO(), evt)
|
||||
if err != nil {
|
||||
fmt.Println("Error inserting event into MongoDB:", err)
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Println("Inserted event into MongoDB:", evt.ID)
|
||||
|
||||
err = websocket.Message.Send(ws, "Echo: "+msg)
|
||||
if err != nil {
|
||||
fmt.Println("Error sending message:", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Initialize MongoDB client
|
||||
var err error
|
||||
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017/")
|
||||
client, err = mongo.Connect(context.TODO(), clientOptions)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Check the connection
|
||||
err = client.Ping(context.TODO(), nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println("Connected to MongoDB!")
|
||||
|
||||
// Initialize collections
|
||||
eventKind0Collection = client.Database("grain").Collection("event-kind0")
|
||||
eventKind1Collection = client.Database("grain").Collection("event-kind1")
|
||||
|
||||
// Start WebSocket server
|
||||
http.Handle("/", websocket.Handler(handler))
|
||||
fmt.Println("WebSocket server started on :8080")
|
||||
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)
|
||||
}
|
||||
}()
|
||||
}
|
96
readme.md
Normal file
96
readme.md
Normal file
@ -0,0 +1,96 @@
|
||||
# GRAIN 🌾 WIP
|
||||
|
||||
**Go Relay and Information Network**
|
||||
|
||||
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.
|
||||
|
||||
## Features
|
||||
|
||||
- **NIP-01 Protocol Support**: GRAIN fully supports the NIP-01 protocol for WebSocket communication.
|
||||
- **Event Processing**: Handles events of kind 0 (user metadata) and kind 1 (text note).
|
||||
- **MongoDB Storage**: Utilizes MongoDB to store and manage events efficiently.
|
||||
- **Scalability**: Built with Go, ensuring high performance and scalability.
|
||||
- **Open Source**: Licensed under the MIT License, making it free to use and modify.
|
||||
|
||||
## Installation
|
||||
|
||||
1. **Clone the repository**:
|
||||
|
||||
```sh
|
||||
git clone https://github.com/oceanslim/grain.git
|
||||
cd grain
|
||||
```
|
||||
|
||||
2. **Build the executable**:
|
||||
|
||||
```sh
|
||||
go build -o grain.exe
|
||||
```
|
||||
|
||||
The `grain.exe` will be placed in a temporary directory within `...\appdata\local\temp\go-build` and subdirectories.
|
||||
|
||||
## Usage
|
||||
|
||||
To run the GRAIN relay:
|
||||
|
||||
```sh
|
||||
./grain.exe
|
||||
```
|
||||
|
||||
### Configuration 🍃
|
||||
|
||||
Configuration options can be set through environment variables or a configuration file. Example configuration:
|
||||
|
||||
```yml
|
||||
server:
|
||||
port: 8080
|
||||
database:
|
||||
type: mongodb
|
||||
connection_string: mongodb://localhost:27017
|
||||
database_name: grain
|
||||
logging:
|
||||
level: info
|
||||
```
|
||||
|
||||
### WebSocket Endpoints
|
||||
|
||||
- Connect: / - Clients can connect to this endpoint to start a WebSocket session.
|
||||
- Publish Event: Send events of kind 0 (user metadata) or kind 1 (text note) to the relay.
|
||||
|
||||
### Development
|
||||
|
||||
To contribute to GRAIN, follow these steps:
|
||||
|
||||
1. Fork the repository.
|
||||
2. Create a new branch:
|
||||
|
||||
```sh
|
||||
git checkout -b feature-branch
|
||||
```
|
||||
|
||||
3. Make your changes.
|
||||
4. Commit your changes:
|
||||
|
||||
```sh
|
||||
git commit -m "Description of changes"
|
||||
```
|
||||
|
||||
5. Push to the branch:
|
||||
|
||||
```sh
|
||||
git push origin feature-branch
|
||||
```
|
||||
|
||||
6. Create a Pull Request.
|
||||
|
||||
### Contributing
|
||||
|
||||
### License
|
||||
|
||||
This project is Open Source and licensed under the MIT License. See the [LICENSE](license) file for details.
|
||||
|
||||
### Acknowledgments
|
||||
|
||||
Special thanks to the Nostr community for their continuous support and contributions.
|
||||
|
||||
Feel free to reach out with any questions or issues you encounter while using GRAIN. Happy coding!
|
Loading…
Reference in New Issue
Block a user