diff --git a/cmd/khatru/examples/basic-badger/main.go b/cmd/khatru/examples/basic-badger/main.go index bce5c725..4c52b933 100644 --- a/cmd/khatru/examples/basic-badger/main.go +++ b/cmd/khatru/examples/basic-badger/main.go @@ -1,8 +1,8 @@ package main import ( - "fmt" "net/http" + "os" "github.com/Hubmakerlabs/replicatr/cmd/khatru" "github.com/fiatjaf/eventstore/badger" @@ -11,18 +11,17 @@ import ( const appName = "replicatr" func main() { - relay := khatru.NewRelay(appName) + r := khatru.NewRelay(appName) - db := badger.BadgerBackend{Path: "/tmp/khatru-badgern-tmp"} + db := badger.BadgerBackend{Path: "/tmp/replicatr-badger"} if err := db.Init(); err != nil { - panic(err) + r.Log.E.F("unable to start database: '%s'", err) + os.Exit(1) } - - relay.StoreEvent = append(relay.StoreEvent, db.SaveEvent) - relay.QueryEvents = append(relay.QueryEvents, db.QueryEvents) - relay.CountEvents = append(relay.CountEvents, db.CountEvents) - relay.DeleteEvent = append(relay.DeleteEvent, db.DeleteEvent) - - fmt.Println("running on :3334") - http.ListenAndServe(":3334", relay) + r.StoreEvent = append(r.StoreEvent, db.SaveEvent) + r.QueryEvents = append(r.QueryEvents, db.QueryEvents) + r.CountEvents = append(r.CountEvents, db.CountEvents) + r.DeleteEvent = append(r.DeleteEvent, db.DeleteEvent) + r.Log.I.Ln("running on :3334") + r.Log.E.Chk(http.ListenAndServe(":3334", r)) } diff --git a/cmd/khatru/relay.go b/cmd/khatru/relay.go index 3a1eae79..c72d9253 100644 --- a/cmd/khatru/relay.go +++ b/cmd/khatru/relay.go @@ -14,35 +14,39 @@ import ( "github.com/puzpuzpuz/xsync/v2" ) +const ( + WriteWait = 10 * time.Second + PongWait = 60 * time.Second + PingPeriod = 30 * time.Second + ReadBufferSize = 4096 + WriteBufferSize = 4096 + MaxMessageSize int64 = 512000 // ??? +) + func NewRelay(appName string) *Relay { return &Relay{ Log: log.New(os.Stderr, appName, 0), - Info: &nip11.RelayInformationDocument{ Software: "https://github.com/Hubmakerlabs/replicatr/cmd/khatru", Version: "n/a", SupportedNIPs: make([]int, 0), }, - upgrader: websocket.Upgrader{ - ReadBufferSize: 1024, - WriteBufferSize: 1024, + ReadBufferSize: ReadBufferSize, + WriteBufferSize: WriteBufferSize, CheckOrigin: func(r *http.Request) bool { return true }, }, - - clients: xsync.NewTypedMapOf[*websocket.Conn, struct{}](pointerHasher[websocket.Conn]), - serveMux: &http.ServeMux{}, - - WriteWait: 10 * time.Second, - PongWait: 60 * time.Second, - PingPeriod: 30 * time.Second, - MaxMessageSize: 512000, + clients: xsync.NewTypedMapOf[*websocket.Conn, struct{}](pointerHasher[websocket.Conn]), + serveMux: &http.ServeMux{}, + WriteWait: WriteWait, + PongWait: PongWait, + PingPeriod: PingPeriod, + MaxMessageSize: MaxMessageSize, } } type Relay struct { - ServiceURL string - + ServiceURL string RejectEvent []func(ctx context.Context, event *nostr.Event) (reject bool, msg string) RejectFilter []func(ctx context.Context, filter nostr.Filter) (reject bool, msg string) RejectCountFilter []func(ctx context.Context, filter nostr.Filter) (reject bool, msg string) @@ -58,25 +62,17 @@ type Relay struct { OnConnect []func(ctx context.Context) OnDisconnect []func(ctx context.Context) OnEventSaved []func(ctx context.Context, event *nostr.Event) - // editing info will affect Info *nip11.RelayInformationDocument - - // Default logger, as set by NewServer, is a stdlib logger prefixed with "[khatru-relay] ", - // outputting to stderr. - Log *log.Logger - + Log *log.Logger // for establishing websockets upgrader websocket.Upgrader - // keep a connection reference to all connected clients for Server.Shutdown clients *xsync.MapOf[*websocket.Conn, struct{}] - // in case you call Server.Start Addr string serveMux *http.ServeMux httpServer *http.Server - // websocket options WriteWait time.Duration // Time allowed to write a message to the peer. PongWait time.Duration // Time allowed to read the next pong message from the peer.