mirror of
https://github.com/fiatjaf/nak.git
synced 2024-11-22 00:09:08 -05:00
connect to relays once per call instead of in each iteration and fail early if no connection works.
This commit is contained in:
parent
6a7a5eb26e
commit
11fe6b5809
18
event.go
18
event.go
|
@ -97,6 +97,16 @@ example:
|
|||
},
|
||||
ArgsUsage: "[relay...]",
|
||||
Action: func(c *cli.Context) error {
|
||||
// try to connect to the relays here
|
||||
var relays []*nostr.Relay
|
||||
if relayUrls := c.Args().Slice(); len(relayUrls) > 0 {
|
||||
_, relays = connectToAllRelays(c.Context, relayUrls)
|
||||
if len(relays) == 0 {
|
||||
fmt.Fprintf(os.Stderr, "failed to connect to any of the given relays.\n")
|
||||
os.Exit(3)
|
||||
}
|
||||
}
|
||||
|
||||
// gather the secret key first
|
||||
sec := c.String("sec")
|
||||
if c.Bool("prompt-sec") {
|
||||
|
@ -204,12 +214,12 @@ example:
|
|||
}
|
||||
}
|
||||
|
||||
relays := c.Args().Slice()
|
||||
if len(relays) > 0 {
|
||||
fmt.Println(evt.String())
|
||||
for _, url := range relays {
|
||||
fmt.Fprintf(os.Stderr, "publishing to %s... ", url)
|
||||
if relay, err := nostr.RelayConnect(c.Context, url); err != nil {
|
||||
os.Stdout.Sync()
|
||||
for _, relay := range relays {
|
||||
fmt.Fprintf(os.Stderr, "publishing to %s... ", relay.URL)
|
||||
if relay, err := nostr.RelayConnect(c.Context, relay.URL); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to connect: %s\n", err)
|
||||
} else {
|
||||
ctx, cancel := context.WithTimeout(c.Context, 10*time.Second)
|
||||
|
|
16
helpers.go
16
helpers.go
|
@ -9,6 +9,7 @@ import (
|
|||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/nbd-wtf/go-nostr"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
|
@ -99,6 +100,21 @@ func validate32BytesHex(target string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func connectToAllRelays(ctx context.Context, relayUrls []string) (*nostr.SimplePool, []*nostr.Relay) {
|
||||
relays := make([]*nostr.Relay, 0, len(relayUrls))
|
||||
pool := nostr.NewSimplePool(ctx)
|
||||
for _, url := range relayUrls {
|
||||
fmt.Fprintf(os.Stderr, "connecting to %s... ", url)
|
||||
if relay, err := pool.EnsureRelay(url); err == nil {
|
||||
relays = append(relays, relay)
|
||||
fmt.Fprintf(os.Stderr, "ok.\n")
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, err.Error()+"\n")
|
||||
}
|
||||
}
|
||||
return pool, relays
|
||||
}
|
||||
|
||||
func lineProcessingError(c *cli.Context, msg string, args ...any) {
|
||||
c.Context = context.WithValue(c.Context, LINE_PROCESSING_ERROR, true)
|
||||
fmt.Fprintf(os.Stderr, msg+"\n", args...)
|
||||
|
|
22
req.go
22
req.go
|
@ -3,6 +3,7 @@ package main
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/nbd-wtf/go-nostr"
|
||||
|
@ -95,6 +96,21 @@ example:
|
|||
},
|
||||
ArgsUsage: "[relay...]",
|
||||
Action: func(c *cli.Context) error {
|
||||
var pool *nostr.SimplePool
|
||||
relayUrls := c.Args().Slice()
|
||||
if len(relayUrls) > 0 {
|
||||
var relays []*nostr.Relay
|
||||
pool, relays = connectToAllRelays(c.Context, relayUrls)
|
||||
if len(relays) == 0 {
|
||||
fmt.Fprintf(os.Stderr, "failed to connect to any of the given relays.\n")
|
||||
os.Exit(3)
|
||||
}
|
||||
relayUrls = make([]string, len(relays))
|
||||
for i, relay := range relays {
|
||||
relayUrls[i] = relay.URL
|
||||
}
|
||||
}
|
||||
|
||||
for stdinFilter := range getStdinLinesOrBlank() {
|
||||
filter := nostr.Filter{}
|
||||
if stdinFilter != "" {
|
||||
|
@ -155,14 +171,12 @@ example:
|
|||
filter.Limit = limit
|
||||
}
|
||||
|
||||
relays := c.Args().Slice()
|
||||
if len(relays) > 0 {
|
||||
pool := nostr.NewSimplePool(c.Context)
|
||||
if len(relayUrls) > 0 {
|
||||
fn := pool.SubManyEose
|
||||
if c.Bool("stream") {
|
||||
fn = pool.SubMany
|
||||
}
|
||||
for ie := range fn(c.Context, relays, nostr.Filters{filter}) {
|
||||
for ie := range fn(c.Context, relayUrls, nostr.Filters{filter}) {
|
||||
fmt.Println(ie.Event)
|
||||
}
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue
Block a user