/
init.go
82 lines (70 loc) · 1.76 KB
/
init.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
package tcssync
import (
"context"
"encoding/json"
"log"
"os"
"time"
sdk "github.com/TinkoffCreditSystems/invest-openapi-go-sdk"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// Config represents configuration
type Config struct {
MongoURL string `json:"mongoURL"`
DbName string `json:"dbName"`
TcsToken string `json:"tcsToken"`
TcsTimeout int32 `json:"tcsTimeout"`
SyncFrom int64 `json:"syncFromTimestamp"`
}
type dbContext func() context.Context
type database struct {
operations *mongo.Collection
prices *mongo.Collection
context dbContext
}
type tcsContext func() (context.Context, context.CancelFunc)
type tinkoff struct {
client *sdk.SandboxRestClient
context tcsContext
}
var tcs tinkoff
var db database
var cfg Config
// Init filler
func Init(configPath string) {
loadConfiguration(configPath)
initDb()
initTcs()
}
func initTcs() {
tcs = tinkoff{
client: sdk.NewSandboxRestClient(cfg.TcsToken),
context: func() (context.Context, context.CancelFunc) {
return context.WithTimeout(context.Background(), time.Duration(cfg.TcsTimeout)*time.Second)
},
}
}
func initDb() {
db.context = func() context.Context { return context.Background() }
clientOptions := options.Client().ApplyURI(cfg.MongoURL)
client, err := mongo.NewClient(clientOptions)
if err != nil {
log.Fatal(err)
}
err = client.Connect(db.context())
if err != nil {
log.Fatal(err)
}
db.operations = client.Database(cfg.DbName).Collection("Operations")
db.prices = client.Database(cfg.DbName).Collection("Prices")
}
func loadConfiguration(file string) {
configFile, err := os.Open(file)
defer configFile.Close()
if err != nil {
log.Fatal(err.Error())
}
jsonParser := json.NewDecoder(configFile)
jsonParser.Decode(&cfg)
}