-
Notifications
You must be signed in to change notification settings - Fork 85
/
main.go
74 lines (61 loc) · 1.54 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"time"
"github.com/fiatjaf/eventstore"
"github.com/fiatjaf/eventstore/postgresql"
"github.com/fiatjaf/relayer/v2"
"github.com/kelseyhightower/envconfig"
"github.com/nbd-wtf/go-nostr"
)
type Relay struct {
PostgresDatabase string `envconfig:"POSTGRESQL_DATABASE"`
storage *postgresql.PostgresBackend
}
func (r *Relay) Name() string {
return "BasicRelay"
}
func (r *Relay) Storage(ctx context.Context) eventstore.Store {
return r.storage
}
func (r *Relay) Init() error {
err := envconfig.Process("", r)
if err != nil {
return fmt.Errorf("couldn't process envconfig: %w", err)
}
// every hour, delete all very old events
go func() {
db := r.Storage(context.TODO()).(*postgresql.PostgresBackend)
for {
time.Sleep(60 * time.Minute)
db.DB.Exec(`DELETE FROM event WHERE created_at < $1`, time.Now().AddDate(0, -3, 0).Unix()) // 3 months
}
}()
return nil
}
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 {
return false
}
return true
}
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}
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)
}
}