khatru/handlers.go

347 lines
8.8 KiB
Go
Raw Permalink Normal View History

package khatru
2021-01-13 23:46:06 -03:00
import (
"context"
"crypto/rand"
2021-01-13 23:46:06 -03:00
"crypto/sha256"
"encoding/hex"
"encoding/json"
"net/http"
"sync"
2021-01-13 23:46:06 -03:00
"time"
2023-06-23 07:10:03 -03:00
"github.com/fasthttp/websocket"
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip11"
"github.com/nbd-wtf/go-nostr/nip42"
2021-01-13 23:46:06 -03:00
)
// ServeHTTP implements http.Handler interface.
func (rl *Relay) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Upgrade") == "websocket" {
rl.HandleWebsocket(w, r)
} else if r.Header.Get("Accept") == "application/nostr+json" {
rl.HandleNIP11(w, r)
} else {
rl.serveMux.ServeHTTP(w, r)
}
2021-01-13 23:46:06 -03:00
}
func (rl *Relay) HandleWebsocket(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
conn, err := rl.upgrader.Upgrade(w, r, nil)
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
if err != nil {
rl.Log.Printf("failed to upgrade websocket: %v\n", err)
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
return
}
rl.clients.Store(conn, struct{}{})
ticker := time.NewTicker(rl.PingPeriod)
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
// NIP-42 challenge
challenge := make([]byte, 8)
rand.Read(challenge)
ws := &WebSocket{
conn: conn,
Challenge: hex.EncodeToString(challenge),
WaitingForAuth: make(chan struct{}),
}
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
// reader
go func() {
defer func() {
ticker.Stop()
if _, ok := rl.clients.Load(conn); ok {
conn.Close()
rl.clients.Delete(conn)
removeListener(ws)
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
}
}()
2021-01-13 23:46:06 -03:00
conn.SetReadLimit(rl.MaxMessageSize)
conn.SetReadDeadline(time.Now().Add(rl.PongWait))
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
conn.SetPongHandler(func(string) error {
conn.SetReadDeadline(time.Now().Add(rl.PongWait))
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
return nil
})
for _, onconnect := range rl.OnConnect {
onconnect(ctx)
}
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
for {
typ, message, err := conn.ReadMessage()
if err != nil {
if websocket.IsUnexpectedCloseError(
err,
websocket.CloseGoingAway, // 1001
websocket.CloseNoStatusReceived, // 1005
websocket.CloseAbnormalClosure, // 1006
) {
rl.Log.Printf("unexpected close error from %s: %v\n", r.Header.Get("X-Forwarded-For"), err)
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
}
break
}
2021-01-13 23:46:06 -03:00
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
if typ == websocket.PingMessage {
ws.WriteMessage(websocket.PongMessage, nil)
continue
}
go func(message []byte) {
ctx = context.Background()
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
var request []json.RawMessage
if err := json.Unmarshal(message, &request); err != nil {
// stop silently
return
2021-02-17 16:59:56 -03:00
}
2021-01-13 23:46:06 -03:00
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
if len(request) < 2 {
ws.WriteJSON(nostr.NoticeEnvelope("request has less than 2 parameters"))
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
return
}
2021-02-14 21:08:02 -03:00
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
var typ string
json.Unmarshal(request[0], &typ)
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
switch typ {
case "EVENT":
// it's a new event
var evt nostr.Event
if err := json.Unmarshal(request[1], &evt); err != nil {
ws.WriteJSON(nostr.NoticeEnvelope("failed to decode event: " + err.Error()))
2021-02-14 21:08:02 -03:00
return
}
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
// check serialization
serialized := evt.Serialize()
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
// assign ID
hash := sha256.Sum256(serialized)
evt.ID = hex.EncodeToString(hash[:])
2021-02-14 21:08:02 -03:00
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
// check signature (requires the ID to be set)
if ok, err := evt.CheckSignature(); err != nil {
ws.WriteJSON(nostr.OKEnvelope{EventID: evt.ID, OK: false, Reason: "error: failed to verify signature"})
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
return
} else if !ok {
ws.WriteJSON(nostr.OKEnvelope{EventID: evt.ID, OK: false, Reason: "invalid: signature is invalid"})
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
return
}
2021-02-14 21:08:02 -03:00
var ok bool
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
if evt.Kind == 5 {
err = rl.handleDeleteRequest(ctx, &evt)
} else {
err = rl.AddEvent(ctx, &evt)
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
}
var reason string
if err == nil {
ok = true
} else {
reason = err.Error()
2023-06-16 17:38:32 -03:00
}
ws.WriteJSON(nostr.OKEnvelope{EventID: evt.ID, OK: ok, Reason: reason})
2023-05-17 19:54:56 +09:00
case "COUNT":
if rl.CountEvents == nil {
ws.WriteJSON(nostr.NoticeEnvelope("this relay does not support NIP-45"))
2023-05-17 19:54:56 +09:00
return
}
var id string
json.Unmarshal(request[1], &id)
if id == "" {
ws.WriteJSON(nostr.NoticeEnvelope("COUNT has no <id>"))
2023-05-17 19:54:56 +09:00
return
}
var total int64
2023-05-17 19:54:56 +09:00
filters := make(nostr.Filters, len(request)-2)
for i, filterReq := range request[2:] {
if err := json.Unmarshal(filterReq, &filters[i]); err != nil {
ws.WriteJSON(nostr.NoticeEnvelope("failed to decode filter"))
continue
2023-05-17 19:54:56 +09:00
}
filter := filters[i]
// overwrite the filter (for example, to eliminate some kinds or tags that we know we don't support)
for _, ovw := range rl.OverwriteCountFilter {
ovw(ctx, &filter)
}
// then check if we'll reject this filter
for _, reject := range rl.RejectCountFilter {
if rejecting, msg := reject(ctx, filter); rejecting {
ws.WriteJSON(nostr.NoticeEnvelope(msg))
continue
2023-05-17 19:54:56 +09:00
}
}
// run the functions to count (generally it will be just one)
for _, count := range rl.CountEvents {
res, err := count(ctx, filter)
if err != nil {
ws.WriteJSON(nostr.NoticeEnvelope(err.Error()))
}
total += res
2023-05-17 19:54:56 +09:00
}
}
ws.WriteJSON([]interface{}{"COUNT", id, map[string]int64{"count": total}})
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
case "REQ":
var id string
json.Unmarshal(request[1], &id)
if id == "" {
ws.WriteJSON(nostr.NoticeEnvelope("REQ has no <id>"))
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
return
}
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
filters := make(nostr.Filters, len(request)-2)
eose := sync.WaitGroup{}
eose.Add(len(request[2:]))
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
for i, filterReq := range request[2:] {
if err := json.Unmarshal(
filterReq,
&filters[i],
); err != nil {
ws.WriteJSON(nostr.NoticeEnvelope("failed to decode filter"))
eose.Done()
continue
}
2021-01-13 23:46:06 -03:00
filter := filters[i]
// overwrite the filter (for example, to eliminate some kinds or
// that we know we don't support)
for _, ovw := range rl.OverwriteFilter {
ovw(ctx, &filter)
}
// then check if we'll reject this filter (we apply this after overwriting
// because we may, for example, remove some things from the incoming filters
// that we know we don't support, and then if the end result is an empty
// filter we can just reject it)
for _, reject := range rl.RejectFilter {
if rejecting, msg := reject(ctx, filter); rejecting {
ws.WriteJSON(nostr.NoticeEnvelope(msg))
continue
}
}
2021-01-13 23:46:06 -03:00
// run the functions to query events (generally just one,
// but we might be fetching stuff from multiple places)
eose.Add(len(rl.QueryEvents))
for _, query := range rl.QueryEvents {
ch, err := query(ctx, filter)
if err != nil {
ws.WriteJSON(nostr.NoticeEnvelope(err.Error()))
eose.Done()
continue
}
2023-05-01 19:40:16 -03:00
go func(ch chan *nostr.Event) {
for event := range ch {
for _, ovw := range rl.OverwriteResponseEvent {
ovw(ctx, event)
}
ws.WriteJSON(nostr.EventEnvelope{SubscriptionID: &id, Event: *event})
}
eose.Done()
}(ch)
2023-05-01 19:40:16 -03:00
}
eose.Done()
}
go func() {
eose.Wait()
ws.WriteJSON(nostr.EOSEEnvelope(id))
}()
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
setListener(id, ws, filters)
case "CLOSE":
var id string
json.Unmarshal(request[1], &id)
if id == "" {
ws.WriteJSON(nostr.NoticeEnvelope("CLOSE has no <id>"))
return
}
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
removeListenerId(ws, id)
case "AUTH":
if rl.ServiceURL != "" {
var evt nostr.Event
if err := json.Unmarshal(request[1], &evt); err != nil {
ws.WriteJSON(nostr.NoticeEnvelope("failed to decode auth event: " + err.Error()))
return
}
if pubkey, ok := nip42.ValidateAuthEvent(&evt, ws.Challenge, rl.ServiceURL); ok {
ws.Authed = pubkey
close(ws.WaitingForAuth)
ctx = context.WithValue(ctx, AUTH_CONTEXT_KEY, pubkey)
2023-06-16 17:38:32 -03:00
ws.WriteJSON(nostr.OKEnvelope{EventID: evt.ID, OK: true})
} else {
ws.WriteJSON(nostr.OKEnvelope{EventID: evt.ID, OK: false, Reason: "error: failed to authenticate"})
}
}
}
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
}(message)
}
}()
// writer
go func() {
defer func() {
ticker.Stop()
conn.Close()
}()
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
for {
select {
case <-ticker.C:
err := ws.WriteMessage(websocket.PingMessage, nil)
if err != nil {
if err.Error() != "use of closed network connection" {
rl.Log.Printf("error writing ping: %v; closing websocket\n", err)
}
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
return
}
}
}
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
}()
}
func (rl *Relay) HandleNIP11(w http.ResponseWriter, r *http.Request) {
2023-11-09 21:39:28 -03:00
w.Header().Set("Content-Type", "application/nostr+json")
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
supportedNIPs := []int{9, 11, 12, 15, 16, 20, 33}
if rl.ServiceURL != "" {
supportedNIPs = append(supportedNIPs, 42)
}
if rl.CountEvents != nil {
supportedNIPs = append(supportedNIPs, 45)
2023-05-17 19:54:56 +09:00
}
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
info := nip11.RelayInformationDocument{
Name: rl.Name,
Description: rl.Description,
PubKey: rl.PubKey,
Contact: rl.Contact,
2023-11-02 21:24:19 -03:00
Icon: rl.IconURL,
SupportedNIPs: supportedNIPs,
Software: "https://github.com/trailriver/khatru",
Version: "n/a",
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
}
for _, edit := range rl.EditInformation {
edit(r.Context(), &info)
}
start: introduce Server type and Shutdown (breaking change) the main motivation for this change is to be able to run tests. before this commit, Start, Router and Log operated on global variables, making automated testing unreasonably hard. this commit puts all that a server needs in a new Server type, which also made it possible for a Server.Shutdown - see ShutdownAware doc comments. BREAKING CHANGES: - Relay.OnInitialized takes one argument now, *relayer.Server. - relayer.Router is now replaced by relayer.Server.Router(). package users can still hook into the router from OnInitialized for custom HTTP routing. - relayer.Log is gone. apart from another global var, imho this was a too opinionated choice for a framework to build a custom relay upon. this commit introduces a Logger interface which package users can implement for zerolog to make it log like before. see Server.Log for details. other notable changes: finally added a couple basic tests, for start up and shutdown. doc comments now explain most of the essentials, hopefully making it more approachable for newcomers and easier to understand the relayer package. the changes in handlers.go are minimal, although git diff goes crazy. this is because most of the lines are simply shifted indentation back by one due to go fmt. before this commit: func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) after: func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request) func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
json.NewEncoder(w).Encode(info)
}