-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
118 lines (92 loc) · 2.55 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
113
114
115
116
117
118
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/macaroons"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"gopkg.in/macaroon.v2"
"github.com/valcanobacon/StreamWall/api"
"github.com/valcanobacon/StreamWall/money"
"github.com/valcanobacon/StreamWall/music"
)
var (
macFile = flag.String("macaroon", "readonly.macaroon", "The file container the macaroon")
tlsFile = flag.String("tls-cert-file", "tls.cert", "The file container the cert file")
lndAddr = flag.String("lndAddr", "localhost:10009", "The server address in the format of host:port")
apiPort = flag.Int("port", 8080, "Port to run the api server on")
durationFiles = "songs/*/durations.txt"
durationFilePrefix = "songs/"
)
func main() {
flag.Parse()
durations, err := music.LoadDurations(durationFiles, durationFilePrefix)
if err != nil {
log.Fatal(err)
}
bank := money.NewBank()
ptCtx, ptCancel := context.WithCancel(context.Background())
defer ptCancel()
log.Printf("Starting Transaction Processor")
go bank.ProcessTransactions(ptCtx)
conn, err := newGrpcConn(*lndAddr, *tlsFile, *macFile)
if err != nil {
log.Fatal(err)
}
defer conn.Close()
pp := &money.PaymentProcessor{
Client: lnrpc.NewLightningClient(conn),
Bank: bank,
Index: 0,
}
ppCtx, ppCancel := context.WithCancel(context.Background())
defer ppCancel()
log.Printf("Starting Lightning Payment Processor")
go pp.Run(ppCtx)
router := api.NewRouter(bank, durations)
log.Printf("Starting web server on %v\n", *apiPort)
err = http.ListenAndServe(fmt.Sprintf(":%v", *apiPort), router)
if err != nil {
log.Fatal((err))
}
}
func newGrpcConn(addr, tlsFile, macFile string) (*grpc.ClientConn, error) {
tlsCert, err := credentials.NewClientTLSFromFile(tlsFile, "")
if err != nil {
return nil, err
}
macCred, err := macCredFromFile(macFile)
if err != nil {
return nil, err
}
opts := []grpc.DialOption{
grpc.WithTransportCredentials(tlsCert),
grpc.WithPerRPCCredentials(macCred),
}
conn, err := grpc.Dial(addr, opts...)
if err != nil {
return nil, err
}
return conn, nil
}
// macCredFromFile loads the macaroon from a file
func macCredFromFile(macFile string) (*macaroons.MacaroonCredential, error) {
bs, err := ioutil.ReadFile(macFile)
if err != nil {
return nil, err
}
mac := &macaroon.Macaroon{}
if err = mac.UnmarshalBinary(bs); err != nil {
return nil, err
}
cred, err := macaroons.NewMacaroonCredential(mac)
if err != nil {
return nil, err
}
return &cred, nil
}