NIP11 implemented (relay metadata)

This commit is contained in:
Chris kerr 2024-07-27 16:46:25 -04:00
parent 46c9e4af3c
commit b761e47abb
6 changed files with 85 additions and 27 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
/tmp /tmp
config.yml config.yml
relay_metadata.json

View File

@ -52,8 +52,14 @@ func main() {
utils.SetSizeLimiter(sizeLimiter) utils.SetSizeLimiter(sizeLimiter)
err = web.LoadRelayMetadata("relay_metadata.json")
if err != nil {
log.Fatalf("Failed to load relay metadata: %v", err)
}
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/", ListenAndServe) mux.HandleFunc("/", ListenAndServe)
mux.HandleFunc("/relay-info", web.RelayInfoHandler)
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("web/static")))) mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("web/static"))))
mux.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "web/static/img/favicon.ico") http.ServeFile(w, r, "web/static/img/favicon.ico")

View File

@ -22,11 +22,14 @@ There is an example config in this repo. Copy the example config to config.yml t
cp config.example.yml config.yml cp config.example.yml config.yml
``` ```
Additionally, you may set metadata Information for your relay with `relay_matadata.json`
```bash
cp relay_metadata.example.json relay_metadata.json
```
### TODO ### TODO
- Handle Kind 5 explicitely to delete Events from the Database
- Handle Ephemeral event
- configurable amount of time to keep ephemeral notes
- configurable event purging - configurable event purging
- by category - by category
- by kind - by kind

View File

@ -0,0 +1,9 @@
{
"name": "GRAIN Relay",
"description": "Go Relay Archetecture for Implementing Nostr",
"pubkey": "16f1a0100d4cfffbcc4230e8e0e4290cc5849c1adc64d6653fda07c031b1074b",
"contact": "oceanslim@gmx.com",
"supported_nips": [1, 2, 9, 11],
"software": "https://forge.happytavern.co/oceanslim/grain",
"version": "0.1.0"
}

View File

@ -2,6 +2,7 @@ package web
import ( import (
"context" "context"
"encoding/json"
"grain/relay/db" "grain/relay/db"
relay "grain/relay/types" relay "grain/relay/types"
"html/template" "html/template"
@ -35,6 +36,20 @@ func RootHandler(w http.ResponseWriter, r *http.Request) {
RenderTemplate(w, data, "index.html") RenderTemplate(w, data, "index.html")
} }
func RelayInfoHandler(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Accept") != "application/nostr+json" {
http.Error(w, "Unsupported Media Type", http.StatusUnsupportedMediaType)
return
}
w.Header().Set("Content-Type", "application/nostr+json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
w.Header().Set("Access-Control-Allow-Methods", "GET")
json.NewEncoder(w).Encode(relayMetadata)
}
// Define the base directories for views and templates // Define the base directories for views and templates
const ( const (
viewsDir = "web/views/" viewsDir = "web/views/"
@ -43,7 +58,7 @@ const (
// Define the common layout templates filenames // Define the common layout templates filenames
var templateFiles = []string{ var templateFiles = []string{
"#layout.html", "layout.html",
"header.html", "header.html",
"footer.html", "footer.html",
} }
@ -52,7 +67,6 @@ var templateFiles = []string{
var layout = PrependDir(templatesDir, templateFiles) var layout = PrependDir(templatesDir, templateFiles)
func RenderTemplate(w http.ResponseWriter, data PageData, view string) { func RenderTemplate(w http.ResponseWriter, data PageData, view string) {
// Append the specific template for the route // Append the specific template for the route
templates := append(layout, viewsDir+view) templates := append(layout, viewsDir+view)
@ -83,33 +97,26 @@ func PrependDir(dir string, files []string) []string {
func FetchTopTenRecentEvents(client *mongo.Client) ([]relay.Event, error) { func FetchTopTenRecentEvents(client *mongo.Client) ([]relay.Event, error) {
var results []relay.Event var results []relay.Event
collections, err := client.Database("grain").ListCollectionNames(context.TODO(), bson.M{}) collection := client.Database("grain").Collection("events")
filter := bson.D{}
opts := options.Find().SetSort(bson.D{{Key: "createdat", Value: -1}}).SetLimit(10)
cursor, err := collection.Find(context.TODO(), filter, opts)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer cursor.Close(context.TODO())
for _, collectionName := range collections { for cursor.Next(context.TODO()) {
collection := client.Database("grain").Collection(collectionName) var event relay.Event
filter := bson.D{} if err := cursor.Decode(&event); err != nil {
opts := options.Find().SetSort(bson.D{{Key: "createdat", Value: -1}}).SetLimit(10)
cursor, err := collection.Find(context.TODO(), filter, opts)
if err != nil {
return nil, err return nil, err
} }
defer cursor.Close(context.TODO()) results = append(results, event)
}
for cursor.Next(context.TODO()) { if err := cursor.Err(); err != nil {
var event relay.Event return nil, err
if err := cursor.Decode(&event); err != nil {
return nil, err
}
results = append(results, event)
}
if err := cursor.Err(); err != nil {
return nil, err
}
} }
return results, nil return results, nil

32
web/relay_metadata.go Normal file
View File

@ -0,0 +1,32 @@
package web
import (
"encoding/json"
"os"
)
type RelayMetadata struct {
Name string `json:"name"`
Description string `json:"description"`
Pubkey string `json:"pubkey"`
Contact string `json:"contact"`
SupportedNIPs []int `json:"supported_nips"`
Software string `json:"software"`
Version string `json:"version"`
}
var relayMetadata RelayMetadata
func LoadRelayMetadata(filename string) error {
data, err := os.ReadFile(filename)
if err != nil {
return err
}
err = json.Unmarshal(data, &relayMetadata)
if err != nil {
return err
}
return nil
}