-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (55 loc) · 1.68 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
package main
import (
"context"
"os"
"os/signal"
"syscall"
"github.com/ojo-network/ethereum-api/client"
"github.com/ojo-network/ethereum-api/config"
"github.com/ojo-network/indexer/indexer"
"github.com/ojo-network/indexer/server"
"github.com/rs/zerolog"
)
func init() {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}
func main() {
logger := zerolog.New(os.Stderr).With().Timestamp().Logger()
ctx, cancel := context.WithCancel(context.Background())
// Parse the config
cfg, err := config.ParseConfig("config.yaml")
if err != nil {
logger.Fatal().Err(err).Msg("error parsing config")
}
// Create new websocket and REST server for each exchange
for iter, exchange := range cfg.Exchanges {
// Only register base health check on one server to avoid multiple registrations panic
var registerBaseHealthCheck bool
if iter == 0 {
registerBaseHealthCheck = true
} else {
registerBaseHealthCheck = false
}
// Initialize indexer
i := indexer.NewIndexer(logger, ctx)
// Start and maintain connection to blockchain nodes
client.MaintainConnection(exchange, i, ctx, logger)
s, err := server.NewServer(logger, exchange.Server, cfg.AssetPairs(exchange), registerBaseHealthCheck)
if err != nil {
logger.Error().Err(err).Msg("error creating server")
cancel()
}
go func(exchangeName string) {
err = s.StartWebsocketAPI(ctx, logger, i, exchangeName)
if err != nil {
logger.Error().Err(err).Msg("websocket api error")
}
}(string(exchange.Name))
}
// Cancel the context on user interrupt
userInterrupt := make(chan os.Signal, 1)
signal.Notify(userInterrupt, os.Interrupt, syscall.SIGTERM)
<-userInterrupt
logger.Info().Msg("user interrupt")
cancel()
}