-
Notifications
You must be signed in to change notification settings - Fork 32
/
main.go
113 lines (92 loc) · 2.26 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
package main
import (
_ "embed"
"fmt"
"os"
"time"
logrusStack "github.com/Gurpartap/logrus-stack"
"github.com/jaffee/commandeer"
"github.com/mikekonan/exchange-proxy/proxy"
"github.com/mikekonan/exchange-proxy/proxy/kucoin"
"github.com/mikekonan/exchange-proxy/store"
"github.com/sirupsen/logrus"
"github.com/valyala/fasthttp"
)
var (
//go:embed disclaimer.txt
disclaimer string
version = "dev"
)
type app struct {
Verbose int `help:"verbose level: 0 - info, 1 - debug, 2 - trace"`
CacheSize int `help:"amount of candles to cache"`
TTLCacheTimeout time.Duration `help:"ttl of blobs of cached data"`
ClientTimeout time.Duration `help:"client timeout"`
ProxyConfig proxy.Config `flag:"!embed"`
KucoinConfig kucoin.Config `flag:"!embed"`
}
func newApp() *app {
return &app{
Verbose: 0,
CacheSize: 1000,
TTLCacheTimeout: time.Minute * 10,
ClientTimeout: time.Second * 15,
KucoinConfig: kucoin.Config{
KucoinTopicsPerWs: 200,
KucoinApiURL: "https://openapi-v2.kucoin.com",
},
ProxyConfig: proxy.Config{
Port: "8080",
Bindaddr: "0.0.0.0",
ConcurrencyLimit: fasthttp.DefaultConcurrency,
},
}
}
func (app *app) configure() {
switch app.Verbose {
case 0:
logrus.SetLevel(logrus.InfoLevel)
case 1:
logrus.SetLevel(logrus.DebugLevel)
case 2:
logrus.SetLevel(logrus.TraceLevel)
}
}
func (app *app) Run() error {
logrus.SetOutput(os.Stdout)
logrus.AddHook(logrusStack.StandardHook())
fmt.Println(disclaimer)
logrus.Infof("starting exchange-proxy: version - '%s'... ", version)
if app.Verbose > 2 {
return fmt.Errorf("wrong verbose level '%d'", app.Verbose)
}
app.configure()
if err := app.ProxyConfig.Validate(); err != nil {
return err
}
if err := app.KucoinConfig.Validate(); err != nil {
return err
}
client := &proxy.Client{
Client: fasthttp.Client{
ReadTimeout: app.ClientTimeout,
WriteTimeout: app.ClientTimeout,
},
}
proxySrv := proxy.New(&app.ProxyConfig,
kucoin.New(
store.NewStore(app.CacheSize),
store.NewTTLCache(app.TTLCacheTimeout),
client,
&app.KucoinConfig,
),
)
proxySrv.Serve()
return nil
}
func main() {
app := newApp()
if err := commandeer.Run(app); err != nil {
logrus.Fatal(err)
}
}