-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
98 lines (79 loc) · 2.16 KB
/
main.go
File metadata and controls
98 lines (79 loc) · 2.16 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"github.com/fiatjaf/eventstore"
"github.com/fiatjaf/eventstore/postgresql"
"github.com/fiatjaf/relayer/v2"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
"github.com/nbd-wtf/go-nostr"
)
type Config struct {
POSTGRESQL_DATABASE string
RELAYER_URL string
PORT int
}
var config Config
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("", &config)
if err != nil {
return fmt.Errorf("couldn't process envconfig: %w", err)
}
fmt.Printf("config: %s\n", config)
// let config = Config{
// }
// 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() {
// Load environment variables from .env file
err := godotenv.Load()
if err != nil {
log.Fatalf("Error loading .env file")
}
r := Relay{}
if err := envconfig.Process("", &config); err != nil {
log.Fatalf("failed to read from env: %v", err)
return
}
fmt.Printf("Database URL: %s\n", config.POSTGRESQL_DATABASE)
fmt.Printf("Relay URL: %s\n", config.RELAYER_URL)
r.storage = &postgresql.PostgresBackend{DatabaseURL: config.POSTGRESQL_DATABASE}
server, err := relayer.NewServer(&r)
if err != nil {
log.Fatalf("failed to create server: %v", err)
}
if err := server.Start(config.RELAYER_URL, config.PORT); err != nil {
log.Fatalf("server terminated: %v", err)
}
// You can now use these variables in your relayer logic
// Example: connectToRelayer(relayerURL, relayerSecret)
}