-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
93 lines (75 loc) · 2.13 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
package main
import (
"context"
"fmt"
"log"
"strings"
flag "github.com/spf13/pflag"
"github.com/ParadigmFoundation/go-obm/exchange"
_ "github.com/ParadigmFoundation/go-obm/exchange/binance"
_ "github.com/ParadigmFoundation/go-obm/exchange/coinbase"
_ "github.com/ParadigmFoundation/go-obm/exchange/gemini"
"github.com/ParadigmFoundation/go-obm/grpc"
"github.com/ParadigmFoundation/go-obm/store/mem"
)
type xchange struct {
name string
symbols []string
}
type config struct {
bind string
exchanges []xchange
}
func parseArgs() (*config, error) {
bind := flag.String("bind", "localhost:8000", "Webserver binding address")
xz := flag.StringArray("exchange", nil, "Specify exchanges and symbols using <exchange>:<symbol[,symbol|...], you can use this flag many times to specify more than one exchange.\nExample:\n\t--exchange=coinbase:BTC-USD,ETH-USD --echange=binance:BTCUSDT")
flag.Parse()
cfg := &config{
bind: *bind,
}
for _, x := range *xz {
split := strings.Split(x, ":")
if len(split) != 2 {
return nil, fmt.Errorf("invalid format %s, use <echange>:<symbol[,symbol|...]", split)
}
xch := xchange{
name: split[0],
symbols: strings.Split(split[1], ","),
}
if len(xch.symbols) == 0 || xch.symbols[0] == "" {
return nil, fmt.Errorf("invalid format %s, no symbols defined, use <echange>:<symbol[,symbol|...]", split)
}
cfg.exchanges = append(cfg.exchanges, xch)
}
if len(cfg.exchanges) == 0 {
return nil, fmt.Errorf("please specify at least one exchange")
}
return cfg, nil
}
func main() {
cfg, err := parseArgs()
if err != nil {
log.Fatal(err)
}
store := mem.New()
srv := grpc.NewServer(store)
log.Printf("API Listening on %s", cfg.bind)
go func() { _ = srv.Listen(cfg.bind) }()
errCh := make(chan error)
ctx := context.Background()
for _, i := range cfg.exchanges {
xch, err := exchange.Get(i.name)
if err != nil {
log.Fatalf("unknown exchange %s. Available exchanges: %q", i.name, exchange.ListNames())
}
syms := i.symbols
go func() {
err := xch.Subscribe(ctx, store, syms...)
if err != nil {
errCh <- err
}
}()
}
log.Fatal(<-errCh)
srv.Stop()
}