From 2771d9b8badabe73dbb0558a7328a318abca245d Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Mon, 8 May 2023 23:55:48 +0900 Subject: [PATCH] fix(all): use v2 module --- add-event.go | 2 +- basic/main.go | 19 ++++---- expensive/main.go | 30 +++++++------ go.mod | 15 ++++--- go.sum | 30 +++++++------ rss-bridge/main.go | 98 ++++++++++++++++++++++------------------- search/main.go | 17 ++++--- start.go | 14 ++++-- storage/sqlite3/save.go | 2 +- whitelisted/main.go | 17 ++++--- 10 files changed, 137 insertions(+), 107 deletions(-) diff --git a/add-event.go b/add-event.go index b686258..a954465 100644 --- a/add-event.go +++ b/add-event.go @@ -4,7 +4,7 @@ import ( "context" "fmt" - "github.com/fiatjaf/relayer/storage" + "github.com/fiatjaf/relayer/v2/storage" "github.com/nbd-wtf/go-nostr" ) diff --git a/basic/main.go b/basic/main.go index 0a30137..5568ad6 100644 --- a/basic/main.go +++ b/basic/main.go @@ -1,13 +1,14 @@ package main import ( + "context" "encoding/json" "fmt" "log" "time" - "github.com/fiatjaf/relayer" - "github.com/fiatjaf/relayer/storage/postgresql" + "github.com/fiatjaf/relayer/v2" + "github.com/fiatjaf/relayer/v2/storage/postgresql" "github.com/kelseyhightower/envconfig" "github.com/nbd-wtf/go-nostr" ) @@ -22,12 +23,10 @@ func (r *Relay) Name() string { return "BasicRelay" } -func (r *Relay) Storage() relayer.Storage { +func (r *Relay) Storage(ctx context.Context) relayer.Storage { return r.storage } -func (r *Relay) OnInitialized(*relayer.Server) {} - func (r *Relay) Init() error { err := envconfig.Process("", r) if err != nil { @@ -36,7 +35,7 @@ func (r *Relay) Init() error { // every hour, delete all very old events go func() { - db := r.Storage().(*postgresql.PostgresBackend) + db := r.Storage(context.TODO()).(*postgresql.PostgresBackend) for { time.Sleep(60 * time.Minute) @@ -47,7 +46,7 @@ func (r *Relay) Init() error { return nil } -func (r *Relay) AcceptEvent(evt *nostr.Event) bool { +func (r *Relay) AcceptEvent(ctx context.Context, evt *nostr.Event) bool { // block events that are too large jsonb, _ := json.Marshal(evt) if len(jsonb) > 10000 { @@ -64,7 +63,11 @@ func main() { return } r.storage = &postgresql.PostgresBackend{DatabaseURL: r.PostgresDatabase} - if err := relayer.Start(&r); err != nil { + server, err := relayer.NewServer(&r) + if err != nil { + log.Fatalf("failed to create server: %v", err) + } + if err := server.Start("0.0.0.0", 7447); err != nil { log.Fatalf("server terminated: %v", err) } } diff --git a/expensive/main.go b/expensive/main.go index f786833..5fcbb46 100644 --- a/expensive/main.go +++ b/expensive/main.go @@ -1,13 +1,14 @@ package main import ( + "context" "encoding/json" "log" "net/http" "time" - "github.com/fiatjaf/relayer" - "github.com/fiatjaf/relayer/storage/postgresql" + "github.com/fiatjaf/relayer/v2" + "github.com/fiatjaf/relayer/v2/storage/postgresql" "github.com/kelseyhightower/envconfig" _ "github.com/lib/pq" "github.com/nbd-wtf/go-nostr" @@ -29,14 +30,14 @@ func (r *Relay) Name() string { return "ExpensiveRelay" } -func (r *Relay) Storage() relayer.Storage { +func (r *Relay) Storage(ctx context.Context) relayer.Storage { return r.storage } func (r *Relay) Init() error { // every hour, delete all very old events go func() { - db := r.Storage().(*postgresql.PostgresBackend) + db := r.Storage(context.TODO()).(*postgresql.PostgresBackend) for { time.Sleep(60 * time.Minute) @@ -47,15 +48,7 @@ func (r *Relay) Init() error { return nil } -func (r *Relay) OnInitialized(s *relayer.Server) { - // special handlers - s.Router().Path("/").HandlerFunc(handleWebpage) - s.Router().Path("/invoice").HandlerFunc(func(w http.ResponseWriter, rq *http.Request) { - handleInvoice(w, rq, r) - }) -} - -func (r *Relay) AcceptEvent(evt *nostr.Event) bool { +func (r *Relay) AcceptEvent(ctx context.Context, evt *nostr.Event) bool { // only accept they have a good preimage for a paid invoice for their public key if !checkInvoicePaidOk(evt.PubKey) { return false @@ -77,7 +70,16 @@ func main() { return } r.storage = &postgresql.PostgresBackend{DatabaseURL: r.PostgresDatabase} - if err := relayer.Start(&r); err != nil { + server, err := relayer.NewServer(&r) + if err != nil { + log.Fatalf("failed to create server: %v", err) + } + // special handlers + server.Router().HandleFunc("/", handleWebpage) + server.Router().HandleFunc("/invoice", func(w http.ResponseWriter, rq *http.Request) { + handleInvoice(w, rq, &r) + }) + if err := server.Start("0.0.0.0", 7447); err != nil { log.Fatalf("server terminated: %v", err) } } diff --git a/go.mod b/go.mod index 90de5b2..71351c6 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,6 @@ require ( github.com/aquasecurity/esquery v0.2.0 github.com/cockroachdb/pebble v0.0.0-20220723153705-3fc374e4dc66 github.com/elastic/go-elasticsearch/v8 v8.6.0 - github.com/fiatjaf/relayer v1.7.3 github.com/gorilla/websocket v1.5.0 github.com/grokify/html-strip-tags-go v0.0.1 github.com/jb55/lnsocket/go v0.0.0-20220725174341-b98b5cd37bb6 @@ -16,13 +15,13 @@ require ( github.com/lib/pq v1.10.3 github.com/mattn/go-sqlite3 v1.14.6 github.com/mmcdole/gofeed v1.1.3 - github.com/nbd-wtf/go-nostr v0.17.1 + github.com/nbd-wtf/go-nostr v0.18.0 github.com/rif/cache2go v1.0.0 github.com/rs/cors v1.7.0 github.com/stevelacy/daz v0.1.4 github.com/stretchr/testify v1.8.0 github.com/tidwall/gjson v1.14.1 - golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2 + golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 ) require ( @@ -51,16 +50,18 @@ require ( github.com/cockroachdb/redact v1.0.8 // indirect github.com/cockroachdb/sentry-go v0.6.1-cockroachdb.2 // indirect github.com/davecgh/go-spew v1.1.1 // indirect - github.com/decred/dcrd/crypto/blake256 v1.0.0 // indirect - github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect + github.com/decred/dcrd/crypto/blake256 v1.0.1 // indirect + github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect github.com/decred/dcrd/lru v1.0.0 // indirect github.com/elastic/elastic-transport-go/v8 v8.0.0-20211216131617-bbee439d559c // indirect github.com/elastic/go-elasticsearch/v7 v7.6.0 // indirect github.com/fatih/structs v1.1.0 // indirect github.com/go-errors/errors v1.0.1 // indirect + github.com/gobwas/httphead v0.1.0 // indirect + github.com/gobwas/pool v0.2.1 // indirect + github.com/gobwas/ws v1.2.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/snappy v0.0.4 // indirect - github.com/gorilla/mux v1.8.0 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/json-iterator/go v1.1.11 // indirect github.com/kkdai/bstream v1.0.0 // indirect @@ -86,7 +87,7 @@ require ( github.com/tidwall/pretty v1.2.0 // indirect golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f // indirect - golang.org/x/sys v0.1.0 // indirect + golang.org/x/sys v0.8.0 // indirect golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 // indirect golang.org/x/text v0.3.7 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index 7c343ad..85a40cd 100644 --- a/go.sum +++ b/go.sum @@ -113,11 +113,12 @@ github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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/decred/dcrd/crypto/blake256 v1.0.0 h1:/8DMNYp9SGi5f0w7uCm6d6M4OU2rGFK09Y2A4Xv7EE0= github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc= +github.com/decred/dcrd/crypto/blake256 v1.0.1 h1:7PltbUIQB7u/FfZ39+DGa/ShuMyJ5ilcvdfma9wOH6Y= +github.com/decred/dcrd/crypto/blake256 v1.0.1/go.mod h1:2OfgNZ5wDpcsFmHmCK5gZTPcCXqlm2ArzUIkw9czNJo= github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1/go.mod h1:hyedUtir6IdtD/7lIxGeCxkaw7y45JueMRL4DIyJDKs= -github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 h1:HbphB4TFFXpv7MNrT52FGrrgVXF1owhMVTHFZIlnvd4= -github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0/go.mod h1:DZGJHZMqrU4JJqFAWUS2UO1+lbSKsdiOoYi9Zzey7Fc= +github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 h1:8UrgZ3GkP4i/CLijOJx79Yu+etlyjdBU4sfcs2WYQMs= +github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0= github.com/decred/dcrd/lru v1.0.0 h1:Kbsb1SFDsIlaupWPwsPp+dkxiBY1frcS07PCPgotKz8= github.com/decred/dcrd/lru v1.0.0/go.mod h1:mxKOwFd7lFjN2GZYsiz/ecgqR6kkYAl+0pz0tEMk218= github.com/dgraph-io/badger v1.6.0/go.mod h1:zwt7syl517jmP8s94KqSxTlM6IMsdhYy6psNgSztDR4= @@ -142,8 +143,6 @@ github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072/go.mod github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo= github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= github.com/fergusstrange/embedded-postgres v1.10.0 h1:YnwF6xAQYmKLAXXrrRx4rHDLih47YJwVPvg8jeKfdNg= -github.com/fiatjaf/relayer v1.7.3 h1:HbE67EFsabLO4bvbuB3uMRfRizsvDxseCIaeQHQstv8= -github.com/fiatjaf/relayer v1.7.3/go.mod h1:cQGM8YSoU/7I79Mg9ULlLQWYm/U54/B/4k60fRXEY2o= github.com/flosch/pongo2 v0.0.0-20190707114632-bbf5a6c351f4/go.mod h1:T9YF2M40nIgbVgp3rreNmTged+9HrbNTIQf1PsaIiTA= github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k= github.com/form3tech-oss/jwt-go v3.2.3+incompatible h1:7ZaBxOI7TMoYBfyA3cQHErNNyAWIKUMIwqxEtgHOs5c= @@ -162,8 +161,14 @@ github.com/go-martini/martini v0.0.0-20170121215854-22fa46961aab/go.mod h1:/P9AE github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo= +github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU= +github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM= github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw= +github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og= +github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw= github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM= +github.com/gobwas/ws v1.2.0 h1:u0p9s3xLYpZCA1z5JgCkMeB34CKCMMQbM+G8Ii7YD0I= +github.com/gobwas/ws v1.2.0/go.mod h1:hRKAFb8wOxFROYNsT1bqfWnhX+b5MFeJM9r2ZSwg/KY= github.com/gogo/googleapis v0.0.0-20180223154316-0cd9801be74a/go.mod h1:gf4bu3Q80BeJ6H1S1vYPm8/ELATdvryBaNFGgqEef3s= github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= @@ -199,8 +204,6 @@ github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= -github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= -github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= @@ -342,8 +345,8 @@ github.com/moul/http2curl v1.0.0/go.mod h1:8UbvGypXm98wA/IqH45anm5Y2Z6ep6O31QGOA github.com/nats-io/nats.go v1.8.1/go.mod h1:BrFz9vVn0fU3AcH9Vn4Kd7W0NpJ651tD5omQ3M8LwxM= github.com/nats-io/nkeys v0.0.2/go.mod h1:dab7URMsZm6Z/jp9Z5UGa87Uutgc2mVpXLC4B7TDb/4= github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c= -github.com/nbd-wtf/go-nostr v0.17.1 h1:dRyNNf1rx5vGYi9AH1A/mcKKQg8ZSiY8uRUJGObXPPI= -github.com/nbd-wtf/go-nostr v0.17.1/go.mod h1:YCDHJtaFQE76d1ZkcUsTkz3dYNP+bldo5CIQwXPPcbk= +github.com/nbd-wtf/go-nostr v0.18.0 h1:ygOvnVzl3TbfIwYE2iPTfaUdieWADr0gP0kNUfH+Rtc= +github.com/nbd-wtf/go-nostr v0.18.0/go.mod h1:fN8trCzHEtsf2954h8neqNERM/OXnCYI71nA4wWCobI= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/nwaples/rardecode v1.1.2 h1:Cj0yZY6T1Zx1R7AhTbyGSALm44/Mmq+BAPc4B/p/d3M= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= @@ -473,8 +476,8 @@ golang.org/x/exp v0.0.0-20190125153040-c74c464bbbf2/go.mod h1:CJ0aWSM057203Lf6IL golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20191030013958-a1ab85dbe136/go.mod h1:JXzH8nQsPlswgeRAPE3MuO9GYsAcnJvJ4vnMwN/5qkY= golang.org/x/exp v0.0.0-20200513190911-00229845015e/go.mod h1:4M0jN8W1tt0AVLNr8HDosyJCDCDuyL9N9+3m7wDWgKw= -golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2 h1:Jvc7gsqn21cJHCmAWx0LiimpP18LZmUxkT5Mp7EZ1mI= -golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= +golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 h1:5llv2sWeaMSnA3w2kS57ouQQ4pudlXrR0dCgw51QK9o= +golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= @@ -547,8 +550,9 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210909193231-528a39cd75f3/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U= -golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU= +golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/rss-bridge/main.go b/rss-bridge/main.go index 7a54591..ab1422d 100644 --- a/rss-bridge/main.go +++ b/rss-bridge/main.go @@ -1,6 +1,7 @@ package main import ( + "context" "encoding/json" "errors" "fmt" @@ -9,7 +10,7 @@ import ( "time" "github.com/cockroachdb/pebble" - "github.com/fiatjaf/relayer" + "github.com/fiatjaf/relayer/v2" "github.com/kelseyhightower/envconfig" "github.com/nbd-wtf/go-nostr" "golang.org/x/exp/slices" @@ -32,8 +33,8 @@ func (relay *Relay) Name() string { } func (r *Relay) OnInitialized(s *relayer.Server) { - s.Router().Path("/").HandlerFunc(handleWebpage) - s.Router().Path("/create").HandlerFunc(handleCreateFeed) + s.Router().HandleFunc("/", handleWebpage) + s.Router().HandleFunc("/create", handleCreateFeed) } func (relay *Relay) Init() error { @@ -90,11 +91,11 @@ func (relay *Relay) Init() error { return nil } -func (relay *Relay) AcceptEvent(_ *nostr.Event) bool { +func (relay *Relay) AcceptEvent(ctx context.Context, _ *nostr.Event) bool { return false } -func (relay *Relay) Storage() relayer.Storage { +func (relay *Relay) Storage(ctx context.Context) relayer.Storage { return store{relay.db} } @@ -103,55 +104,39 @@ type store struct { } func (b store) Init() error { return nil } -func (b store) SaveEvent(_ *nostr.Event) error { +func (b store) SaveEvent(ctx context.Context, _ *nostr.Event) error { return errors.New("blocked: we don't accept any events") } -func (b store) DeleteEvent(_, _ string) error { +func (b store) DeleteEvent(ctx context.Context, id string, pubkey string) error { return errors.New("blocked: we can't delete any events") } -func (b store) QueryEvents(filter *nostr.Filter) ([]nostr.Event, error) { - var evts []nostr.Event - +func (b store) QueryEvents(ctx context.Context, filter *nostr.Filter) (chan *nostr.Event, error) { if filter.IDs != nil || len(filter.Tags) > 0 { - return evts, nil + return nil, nil } - for _, pubkey := range filter.Authors { - if val, closer, err := relay.db.Get([]byte(pubkey)); err == nil { - defer closer.Close() - - var entity Entity - if err := json.Unmarshal(val, &entity); err != nil { - log.Printf("got invalid json from db at key %s: %v", pubkey, err) - continue - } - - feed, err := parseFeed(entity.URL) - if err != nil { - log.Printf("failed to parse feed at url %q: %v", entity.URL, err) - continue - } - - if filter.Kinds == nil || slices.Contains(filter.Kinds, nostr.KindSetMetadata) { - evt := feedToSetMetadata(pubkey, feed) + evts := make(chan *nostr.Event) + go func() { + for _, pubkey := range filter.Authors { + if val, closer, err := relay.db.Get([]byte(pubkey)); err == nil { + defer closer.Close() - if filter.Since != nil && evt.CreatedAt.Time().Before(filter.Since.Time()) { + var entity Entity + if err := json.Unmarshal(val, &entity); err != nil { + log.Printf("got invalid json from db at key %s: %v", pubkey, err) continue } - if filter.Until != nil && evt.CreatedAt.Time().After(filter.Until.Time()) { + + feed, err := parseFeed(entity.URL) + if err != nil { + log.Printf("failed to parse feed at url %q: %v", entity.URL, err) continue } - evt.Sign(entity.PrivateKey) - evts = append(evts, evt) - } - - if filter.Kinds == nil || slices.Contains(filter.Kinds, nostr.KindTextNote) { - var last uint32 = 0 - for _, item := range feed.Items { - evt := itemToTextNote(pubkey, item) + if filter.Kinds == nil || slices.Contains(filter.Kinds, nostr.KindSetMetadata) { + evt := feedToSetMetadata(pubkey, feed) if filter.Since != nil && evt.CreatedAt.Time().Before(filter.Since.Time()) { continue @@ -161,18 +146,35 @@ func (b store) QueryEvents(filter *nostr.Filter) ([]nostr.Event, error) { } evt.Sign(entity.PrivateKey) + evts <- &evt + } + + if filter.Kinds == nil || slices.Contains(filter.Kinds, nostr.KindTextNote) { + var last uint32 = 0 + for _, item := range feed.Items { + evt := itemToTextNote(pubkey, item) - if evt.CreatedAt.Time().After(time.Unix(int64(last), 0)) { - last = uint32(evt.CreatedAt.Time().Unix()) + if filter.Since != nil && evt.CreatedAt.Time().Before(filter.Since.Time()) { + continue + } + if filter.Until != nil && evt.CreatedAt.Time().After(filter.Until.Time()) { + continue + } + + evt.Sign(entity.PrivateKey) + + if evt.CreatedAt.Time().After(time.Unix(int64(last), 0)) { + last = uint32(evt.CreatedAt.Time().Unix()) + } + + evts <- &evt } - evts = append(evts, evt) + relay.lastEmitted.Store(entity.URL, last) } - - relay.lastEmitted.Store(entity.URL, last) } } - } + }() return evts, nil } @@ -182,7 +184,11 @@ func (relay *Relay) InjectEvents() chan nostr.Event { } func main() { - if err := relayer.Start(relay); err != nil { + server, err := relayer.NewServer(relay) + if err != nil { + log.Fatalf("failed to create server: %v", err) + } + if err := server.Start("0.0.0.0", 7447); err != nil { log.Fatalf("server terminated: %v", err) } } diff --git a/search/main.go b/search/main.go index 14dce89..db65ec6 100644 --- a/search/main.go +++ b/search/main.go @@ -1,11 +1,12 @@ package main import ( + "context" "fmt" "log" - "github.com/fiatjaf/relayer" - "github.com/fiatjaf/relayer/storage/elasticsearch" + "github.com/fiatjaf/relayer/v2" + "github.com/fiatjaf/relayer/v2/storage/elasticsearch" "github.com/kelseyhightower/envconfig" "github.com/nbd-wtf/go-nostr" ) @@ -18,12 +19,10 @@ func (r *Relay) Name() string { return "SearchRelay" } -func (r *Relay) Storage() relayer.Storage { +func (r *Relay) Storage(ctx context.Context) relayer.Storage { return r.storage } -func (r *Relay) OnInitialized(*relayer.Server) {} - func (r *Relay) Init() error { err := envconfig.Process("", r) if err != nil { @@ -33,7 +32,7 @@ func (r *Relay) Init() error { return nil } -func (r *Relay) AcceptEvent(evt *nostr.Event) bool { +func (r *Relay) AcceptEvent(ctx context.Context, evt *nostr.Event) bool { // block events that are too large // jsonb, _ := json.Marshal(evt) // if len(jsonb) > 100000 { @@ -58,7 +57,11 @@ func main() { return } r.storage = &elasticsearch.ElasticsearchStorage{} - if err := relayer.Start(&r); err != nil { + server, err := relayer.NewServer(&r) + if err != nil { + log.Fatalf("failed to create server: %v", err) + } + if err := server.Start("0.0.0.0", 7447); err != nil { log.Fatalf("server terminated: %v", err) } } diff --git a/start.go b/start.go index 0895bbb..ef583fa 100644 --- a/start.go +++ b/start.go @@ -42,16 +42,22 @@ type Server struct { // in case you call Server.Start Addr string + serveMux *http.ServeMux httpServer *http.Server } +func (s *Server) Router() *http.ServeMux { + return s.serveMux +} + // NewServer initializes the relay and its storage using their respective Init methods, // returning any non-nil errors, and returns a Server ready to listen for HTTP requests. func NewServer(relay Relay) (*Server, error) { srv := &Server{ - Log: defaultLogger(relay.Name() + ": "), - relay: relay, - clients: make(map[*websocket.Conn]struct{}), + Log: defaultLogger(relay.Name() + ": "), + relay: relay, + clients: make(map[*websocket.Conn]struct{}), + serveMux: &http.ServeMux{}, } // init the relay @@ -80,6 +86,8 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { s.HandleWebsocket(w, r) } else if r.Header.Get("Accept") == "application/nostr+json" { s.HandleNIP11(w, r) + } else { + s.serveMux.ServeHTTP(w, r) } } diff --git a/storage/sqlite3/save.go b/storage/sqlite3/save.go index b40d5b2..615930c 100644 --- a/storage/sqlite3/save.go +++ b/storage/sqlite3/save.go @@ -5,7 +5,7 @@ import ( "encoding/json" "fmt" - "github.com/fiatjaf/relayer/storage" + "github.com/fiatjaf/relayer/v2/storage" "github.com/nbd-wtf/go-nostr" ) diff --git a/whitelisted/main.go b/whitelisted/main.go index 94b31b6..7be21cd 100644 --- a/whitelisted/main.go +++ b/whitelisted/main.go @@ -1,11 +1,12 @@ package main import ( + "context" "encoding/json" "log" - "github.com/fiatjaf/relayer" - "github.com/fiatjaf/relayer/storage/postgresql" + "github.com/fiatjaf/relayer/v2" + "github.com/fiatjaf/relayer/v2/storage/postgresql" "github.com/kelseyhightower/envconfig" "github.com/nbd-wtf/go-nostr" ) @@ -21,9 +22,7 @@ func (r *Relay) Name() string { return "WhitelistedRelay" } -func (r *Relay) OnInitialized(*relayer.Server) {} - -func (r *Relay) Storage() relayer.Storage { +func (r *Relay) Storage(ctx context.Context) relayer.Storage { return r.storage } @@ -31,7 +30,7 @@ func (r *Relay) Init() error { return nil } -func (r *Relay) AcceptEvent(evt *nostr.Event) bool { +func (r *Relay) AcceptEvent(ctx context.Context, evt *nostr.Event) bool { // disallow anything from non-authorized pubkeys found := false for _, pubkey := range r.Whitelist { @@ -60,7 +59,11 @@ func main() { return } r.storage = &postgresql.PostgresBackend{DatabaseURL: r.PostgresDatabase} - if err := relayer.Start(&r); err != nil { + server, err := relayer.NewServer(&r) + if err != nil { + log.Fatalf("failed to create server: %v", err) + } + if err := server.Start("0.0.0.0", 7447); err != nil { log.Fatalf("server terminated: %v", err) } }