-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple.go
79 lines (63 loc) · 1.84 KB
/
simple.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
package simple
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"github.com/rs/zerolog"
"github.com/vanillaiice/gocryptobot/pkg/binance"
"github.com/vanillaiice/gocryptobot/pkg/db/bbolt"
"github.com/vanillaiice/gocryptobot/pkg/db/postgres"
"github.com/vanillaiice/gocryptobot/pkg/db/redis"
"github.com/vanillaiice/gocryptobot/pkg/db/sqlite"
"github.com/vanillaiice/gocryptobot/pkg/pricews"
)
func Run(botCfg *BotCfg) (err error) {
client := binance.NewClient(botCfg.ServerBaseEndpoint, botCfg.SecretKey, botCfg.ApiKey)
symbolUpper = strings.ToUpper(botCfg.Base + botCfg.Quote)
state, err := NewState(botCfg, client)
if err != nil {
return
}
if botCfg.LogDb {
switch botCfg.DbBackend {
case dbBackendSqlite:
receiptsDb, err = sqlite.Open(botCfg.DbUrl, symbolUpper, context.Background())
case dbBackendPostgres:
receiptsDb, err = postgres.Open(botCfg.DbUrl, symbolUpper, context.Background())
case dbBackendBbolt:
receiptsDb, err = bbolt.Open(botCfg.DbUrl, symbolUpper)
case dbBackendRedis:
receiptsDb, err = redis.Open(botCfg.DbUrl, symbolUpper, context.Background())
default:
return fmt.Errorf("invalid db backend: %s", botCfg.DbBackend)
}
if err != nil {
return
}
defer receiptsDb.Close()
}
logLevel, ok := logLevelMap[botCfg.LogLevel]
if !ok {
return fmt.Errorf("invalid log level: %s", botCfg.LogLevel)
}
zerolog.SetGlobalLevel(logLevel)
sigChan := make(chan os.Signal, 1)
errChan := make(chan error)
priceChan := make(chan pricews.PriceData)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigChan
errChan <- errors.New("signal received, shutting down")
}()
go func() {
errChan <- pricews.Run(botCfg.ServerBaseEndpoint, symbolUpper, priceChan)
}()
go func() {
errChan <- trade(botCfg, state, client, priceChan)
}()
return <-errChan
}