-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
112 lines (105 loc) · 2.72 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"context"
"fmt"
"github.com/getAlby/ln-event-publisher/config"
"github.com/getAlby/ln-event-publisher/db"
"github.com/getAlby/ln-event-publisher/service"
"os"
"os/signal"
"strings"
"sync"
"time"
"github.com/getAlby/ln-event-publisher/lnd"
"github.com/getsentry/sentry-go"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/sirupsen/logrus"
)
func main() {
c := &config.Config{}
logrus.SetFormatter(&logrus.JSONFormatter{})
// Load configruation from environment variables
err := godotenv.Load(".env")
if err != nil {
logrus.Warn("Failed to load .env file")
}
err = envconfig.Process("", c)
if err != nil {
logrus.Fatalf("Error loading environment variables: %v", err)
}
// Setup exception tracking with Sentry if configured
if c.SentryDSN != "" {
if err = sentry.Init(sentry.ClientOptions{
Dsn: c.SentryDSN,
}); err != nil {
logrus.Error(err)
}
}
client, err := lnd.NewLNDclient(lnd.LNDoptions{
Address: c.LNDAddress,
MacaroonFile: c.LNDMacaroonFile,
CertFile: c.LNDCertFile,
})
if err != nil {
sentry.CaptureException(err)
logrus.Fatalf("Error loading environment variables: %v", err)
}
resp, err := client.GetInfo(context.Background(), &lnrpc.GetInfoRequest{})
if err != nil {
sentry.CaptureException(err)
logrus.Fatal(err)
}
logrus.Infof("Connected to LND: %s - %s", resp.Alias, resp.IdentityPubkey)
logrus.Info("Opening PG database")
db, err := db.OpenDB(c)
if err != nil {
sentry.CaptureException(err)
logrus.Fatal(err)
}
svc := &service.Service{
Cfg: c,
Lnd: client,
Db: db,
}
err = svc.InitRabbitMq()
if err != nil {
sentry.CaptureException(err)
logrus.Fatal(err)
}
backgroundCtx := context.Background()
ctx, _ := signal.NotifyContext(backgroundCtx, os.Interrupt)
//start both subscriptions
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
err = svc.StartInvoiceSubscription(ctx)
if err != nil && !strings.Contains(err.Error(), context.Canceled.Error()) {
logrus.Fatal(err)
}
logrus.Info("invoice routine done")
wg.Done()
}()
wg.Add(1)
go func() {
err = svc.StartPaymentSubscription(ctx)
if err != nil && !strings.Contains(err.Error(), context.Canceled.Error()) {
logrus.Fatal(err)
}
logrus.Info("payment routine done")
wg.Done()
}()
<-ctx.Done()
// start goroutine that will exit program after 10 seconds
// in case graceful shutdown fails
go func() {
time.Sleep(10 * time.Second)
nonGracefulShutdownErr := fmt.Errorf("non-graceful shutdown because of timeout")
sentry.CaptureException(nonGracefulShutdownErr)
logrus.Fatal(nonGracefulShutdownErr)
}()
//wait for goroutines to finish
wg.Wait()
logrus.Info("Exited gracefully. Goodbye.")
}