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)
93 lines
2.2 KiB
Go
93 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/fiatjaf/relayer"
|
|
"github.com/fiatjaf/relayer/storage/postgresql"
|
|
"github.com/kelseyhightower/envconfig"
|
|
_ "github.com/lib/pq"
|
|
"github.com/nbd-wtf/go-nostr"
|
|
)
|
|
|
|
type Relay struct {
|
|
PostgresDatabase string `envconfig:"POSTGRESQL_DATABASE"`
|
|
CLNNodeId string `envconfig:"CLN_NODE_ID"`
|
|
CLNHost string `envconfig:"CLN_HOST"`
|
|
CLNRune string `envconfig:"CLN_RUNE"`
|
|
TicketPriceSats int64 `envconfig:"TICKET_PRICE_SATS"`
|
|
|
|
storage *postgresql.PostgresBackend
|
|
}
|
|
|
|
var r = &Relay{}
|
|
|
|
func (r *Relay) Name() string {
|
|
return "ExpensiveRelay"
|
|
}
|
|
|
|
func (r *Relay) Storage() relayer.Storage {
|
|
return r.storage
|
|
}
|
|
|
|
func (r *Relay) Init() error {
|
|
// every hour, delete all very old events
|
|
go func() {
|
|
db := r.Storage().(*postgresql.PostgresBackend)
|
|
|
|
for {
|
|
time.Sleep(60 * time.Minute)
|
|
db.DB.Exec(`DELETE FROM event WHERE created_at < $1`, time.Now().AddDate(0, -6, 0)) // 6 months
|
|
}
|
|
}()
|
|
|
|
return nil
|
|
}
|
|
|
|
func (r *Relay) OnInitialized(s *relayer.Server) {
|
|
// special handlers
|
|
s.Router().Path("/").HandlerFunc(handleWebpage)
|
|
s.Router().Path("/invoice").HandlerFunc(handleInvoice)
|
|
}
|
|
|
|
func (r *Relay) AcceptEvent(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
|
|
}
|
|
|
|
// block events that are too large
|
|
jsonb, _ := json.Marshal(evt)
|
|
if len(jsonb) > 100000 {
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func (r *Relay) BeforeSave(evt *nostr.Event) {
|
|
// do nothing
|
|
}
|
|
|
|
func (r *Relay) AfterSave(evt *nostr.Event) {
|
|
// delete all but the 1000 most recent ones for each key
|
|
r.Storage().(*postgresql.PostgresBackend).DB.Exec(`DELETE FROM event WHERE pubkey = $1 AND kind = $2 AND created_at < (
|
|
SELECT created_at FROM event WHERE pubkey = $1
|
|
ORDER BY created_at DESC OFFSET 1000 LIMIT 1
|
|
)`, evt.PubKey, evt.Kind)
|
|
}
|
|
|
|
func main() {
|
|
r := Relay{}
|
|
if err := envconfig.Process("", &r); err != nil {
|
|
log.Fatalf("failed to read from env: %v", err)
|
|
return
|
|
}
|
|
r.storage = &postgresql.PostgresBackend{DatabaseURL: r.PostgresDatabase}
|
|
if err := relayer.Start(&r); err != nil {
|
|
log.Fatalf("server terminated: %v", err)
|
|
}
|
|
}
|