-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (70 loc) · 2.1 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
package main
import (
"context"
"distributed-price-feed/config"
"distributed-price-feed/connection"
"distributed-price-feed/database"
priceshandling "distributed-price-feed/prices-handling"
"distributed-price-feed/pubsub"
"fmt"
"os"
"os/signal"
"syscall"
libp2p "github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/crypto"
"go.uber.org/zap"
log "go.uber.org/zap"
)
var logger, _ = log.NewProduction()
func main() {
defer logger.Sync()
config, err := config.ParseFlags()
if err != nil {
logger.Fatal("failed to parse flags", zap.Error(err))
}
db, err := database.ConnectToDatabase(config)
if err != nil {
logger.Fatal("failed to connect to database", zap.Error(err))
}
priv, _, err := crypto.GenerateKeyPair(
crypto.Ed25519,
-1,
)
if err != nil {
logger.Fatal("failed generating key pair", zap.Error(err))
}
node, err := libp2p.New(
libp2p.Identity(priv),
libp2p.ListenAddrStrings(fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", config.Port)),
libp2p.ForceReachabilityPublic(),
// TODO: support TLS connections
// libp2p.Security(libp2ptls.ID, libp2ptls.New),
)
if err != nil {
logger.Fatal("failed initializing node", zap.Error(err))
}
logger = logger.With(log.String("node_id", node.ID().String()))
logger.Info("Node initialized")
ctx := context.Background()
kdht, err := connection.StartDHT(ctx, logger, node, config.BootstrapPeer)
if err != nil {
logger.Fatal("failed to start DHT", zap.Error(err))
}
topic, sub, err := pubsub.AdvertiseInDHTAndStartPubSub(ctx, logger, node, kdht, config.RendezvousPoint)
if err != nil {
logger.Fatal("failed while DHT advertisement or pub sub start", zap.Error(err))
}
pricesHandler, err := priceshandling.NewPricesHandler(logger, db, node.ID(), topic, sub)
if err != nil {
logger.Fatal("failed to initialize prices handler", zap.Error(err))
}
go pricesHandler.SubscribeToEthPriceMessages(ctx)
go pricesHandler.PollForEthPrice(ctx)
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
<-ch
logger.Info("Received signal, shutting down...")
if err := node.Close(); err != nil {
panic(err)
}
}