-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
137 lines (119 loc) · 3.12 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright (c) 2019, The Fonero developers
// See LICENSE for details.
package main
import (
"context"
"fmt"
"net"
"os"
"os/signal"
"path/filepath"
"strings"
"sync"
"github.com/fonero-project/fnodata/fnorates"
"github.com/fonero-project/fnodata/exchanges"
"google.golang.org/grpc"
)
func main() {
killSwitch := make(chan os.Signal, 1)
signal.Notify(killSwitch, os.Interrupt)
cfg, err := loadConfig()
if err != nil {
fmt.Printf("%v\n", err)
return
}
err = os.MkdirAll(cfg.AppDirectory, 0700)
if err != nil {
fmt.Printf("Unable to create application directory: %v", err)
return
}
initializeLogging(filepath.Join(cfg.LogPath, "rateserver.log"), cfg.LogLevel)
if cfg.CertificatePath == "" || cfg.KeyPath == "" {
log.Errorf("TLS certificate and key files must be provided")
return
}
creds, err := openRPCKeyPair(cfg)
if err != nil {
log.Errorf("TLS certificate error: %v", err)
return
}
// Initialize the ExchangeBot
var xcBot *exchanges.ExchangeBot
botCfg := exchanges.ExchangeBotConfig{
DataExpiry: cfg.ExchangeRefresh,
RequestExpiry: cfg.ExchangeExpiry,
BtcIndex: cfg.ExchangeCurrency,
}
if cfg.DisabledExchanges != "" {
botCfg.Disabled = strings.Split(cfg.DisabledExchanges, ",")
}
xcBot, err = exchanges.NewExchangeBot(&botCfg)
if err != nil {
log.Errorf("Could not create exchange monitor: %v", err)
return
}
var xcList, prepend string
for k := range xcBot.Exchanges {
xcList += prepend + k
prepend = ", "
}
log.Infof("ExchangeBot monitoring %s", xcList)
xcSignals := xcBot.UpdateChannels()
ctx, shutdown := context.WithCancel(context.Background())
wg := new(sync.WaitGroup)
wg.Add(1)
go xcBot.Start(ctx, wg)
rateServer := NewRateServer(cfg.ExchangeCurrency, xcBot)
// Set up gRPC server.
listener, err := net.Listen("tcp", cfg.GRPCListen)
if err != nil {
log.Errorf("Failed to create net.Listener at %s", cfg.GRPCListen)
shutdown()
return
}
grpcServer := grpc.NewServer(grpc.Creds(creds))
fnorates.RegisterFNORatesServer(grpcServer, rateServer)
printUpdate := func(token string) {
msg := fmt.Sprintf("Update received from %s", token)
if !xcBot.IsFailed() {
msg += fmt.Sprintf(". Current price: %.2f %s", xcBot.Price(), xcBot.BtcIndex)
}
log.Infof(msg)
}
sendUpdate := func(update *fnorates.ExchangeRateUpdate) {
rateServer.clientLock.RLock()
for _, client := range rateServer.clients {
err := client.SendExchangeUpdate(update)
if err != nil {
log.Warnf("send error: %v", err)
}
}
rateServer.clientLock.RUnlock()
}
// Start the main loop in a goroutine, shutting down the grpcServer when done.
go func() {
out:
for {
select {
case <-killSwitch:
break out
case update := <-xcSignals.Exchange:
printUpdate(update.Token)
sendUpdate(makeExchangeRateUpdate(update))
case update := <-xcSignals.Index:
printUpdate(update.Token)
sendUpdate(&fnorates.ExchangeRateUpdate{
Token: update.Token,
Indices: update.Indices,
})
case <-xcSignals.Quit:
log.Infof("ExchangeBot Quit signal received.")
break out
}
}
shutdown()
grpcServer.Stop()
}()
grpcServer.Serve(listener)
wg.Wait()
}