This repository has been archived by the owner on Aug 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
77 lines (61 loc) · 1.69 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
75
76
77
package main
import (
"fmt"
"github.com/fiatjaf/relayer"
"github.com/jmoiron/sqlx"
"github.com/jmoiron/sqlx/reflectx"
"github.com/kelseyhightower/envconfig"
"github.com/lnbits/relampago"
rc "github.com/lnbits/relampago/connect"
)
type ExpensiveRelay struct {
Domain string `envconfig:"DOMAIN"`
PostgresDatabase string `envconfig:"POSTGRESQL_DATABASE"`
LightningBackendSettings rc.LightningBackendSettings
db *sqlx.DB
ln relampago.Wallet
}
func (relay *ExpensiveRelay) Name() string {
return "ExpensiveRelay"
}
func (relay *ExpensiveRelay) Init() error {
err := envconfig.Process("", relay)
if err != nil {
return fmt.Errorf("couldn't process envconfig: %w", err)
}
err = envconfig.Process("", &relay.LightningBackendSettings)
if err != nil {
return fmt.Errorf("couldn't process envconfig: %w", err)
}
if db, err := initDB(relay.PostgresDatabase); err != nil {
return fmt.Errorf("failed to open database: %w", err)
} else {
db.Mapper = reflectx.NewMapperFunc("json", sqlx.NameMapper)
relay.db = db
}
// lightning
relay.ln, err = rc.Connect()
if err != nil {
return fmt.Errorf("failed to connect to lightning backend: %w", err)
}
// getting notified of invoice payments
stream, err := relay.ln.PaidInvoicesStream()
if err != nil {
return fmt.Errorf("failed to listen for incoming payments: %w", err)
}
go func() {
for status := range stream {
handlePaidInvoice(status)
}
}()
// endpoints
relayer.Router.Path("/").HandlerFunc(handleWebpage)
relayer.Router.Path("/.well-known/lnurlp/{pubkey}").HandlerFunc(handleLnurlRegister)
// cleanup events
go cleanupRoutine()
return nil
}
var relay = ExpensiveRelay{}
func main() {
relayer.Start(&relay)
}