-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
138 lines (119 loc) · 3.64 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"encoding/hex"
"flag"
"io"
"os"
"strings"
"time"
"github.com/eoscanada/eos-go/ecc"
"github.com/BurntSushi/toml"
"github.com/DaoCasino/casino-backend/utils"
broker "github.com/DaoCasino/platform-action-monitor-client"
"github.com/eoscanada/eos-go"
"github.com/kelseyhightower/envconfig"
"github.com/rs/zerolog/log"
)
func MakeAppConfig(cfg *Config) (*AppConfig, *eos.KeyBag, error) {
appCfg := new(AppConfig)
var err error
// set broker config
appCfg.Broker.TopicID = cfg.Broker.TopicID
if f, err := os.Open(cfg.Broker.TopicOffsetPath); err == nil {
defer f.Close()
appCfg.Broker.TopicOffset, err = utils.ReadOffset(f)
if err != nil {
if err == io.EOF { // if file empty just set 0
appCfg.Broker.TopicOffset = 0
} else {
return nil, nil, err
}
}
} else {
// initial start
appCfg.Broker.TopicOffset = 0
}
// set blockchain config
keyBag := &eos.KeyBag{}
if err = keyBag.Add(cfg.BlockChain.DepositKey); err != nil {
return nil, nil, err
}
if err = keyBag.Add(cfg.BlockChain.SigniDiceKey); err != nil {
return nil, nil, err
}
pubKeys, err := keyBag.AvailableKeys()
if err != nil {
return nil, nil, err
}
appCfg.BlockChain.SignerAccountName = eos.AN(cfg.BlockChain.SigniDiceAccountName)
appCfg.BlockChain.CasinoAccountName = eos.AN(cfg.BlockChain.CasinoAccountName)
appCfg.BlockChain.EosPubKeys = PubKeys{pubKeys[0], pubKeys[1]}
if appCfg.BlockChain.RSAKey, err = utils.ReadRsa(cfg.BlockChain.RSAKey); err != nil {
return nil, nil, err
}
if appCfg.BlockChain.ChainID, err = hex.DecodeString(cfg.BlockChain.ChainID); err != nil {
return nil, nil, err
}
appCfg.BlockChain.PlatformAccountName = eos.AN(cfg.BlockChain.PlatformAccountName)
if appCfg.BlockChain.PlatformPubKey, err = ecc.NewPublicKey(cfg.BlockChain.PlatformPubKey); err != nil {
return nil, nil, err
}
// set HTTP config
appCfg.HTTP.RetryDelay = time.Duration(cfg.HTTP.RetryDelay) * time.Second
appCfg.HTTP.Timeout = time.Duration(cfg.HTTP.Timeout) * time.Second
appCfg.HTTP.RetryAmount = cfg.HTTP.RetryAmount
return appCfg, keyBag, nil
}
func MakeApp(cfg *Config) (*App, *os.File, error) {
appConfig, keyBag, err := MakeAppConfig(cfg)
if err != nil {
log.Panic().Msgf("Failed to process config, reason: %s", err.Error())
}
events := make(chan *broker.EventMessage)
f, err := os.OpenFile(cfg.Broker.TopicOffsetPath, os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return nil, nil, err
}
bc := eos.New(cfg.BlockChain.URL)
bc.SetSigner(keyBag)
brokerClient := broker.NewEventListener(cfg.Broker.URL, events)
brokerClient.ReconnectionAttempts = cfg.Broker.ReconnectionAttempts
brokerClient.ReconnectionDelay = time.Duration(cfg.Broker.ReconnectionDelay) * time.Second
brokerClient.SetToken(cfg.Broker.Token)
app := NewApp(bc, brokerClient, events, f, appConfig)
return app, f, nil
}
func GetConfig(configPath string) (*Config, error) {
cfg := &Config{}
if err := envconfig.Process("", cfg); err != nil {
return nil, err
}
if _, err := toml.DecodeFile(configPath, cfg); err != nil {
if !os.IsNotExist(err) {
return nil, err
}
}
return cfg, nil
}
func main() {
configPath := flag.String("config", utils.GetConfigPath(configEnvVar, defaultConfigPath),
"config file path")
flag.Parse()
cfg, err := GetConfig(*configPath)
if err != nil {
log.Panic().Msg(err.Error())
}
logLevel := cfg.Server.LogLevel
InitLogger(cfg.Server.LogLevel)
if strings.ToLower(logLevel) == "debug" {
broker.EnableDebugLogging()
}
app, f, err := MakeApp(cfg)
if err != nil {
log.Panic().Msg(err.Error())
}
defer f.Close()
if err := app.Run(utils.GetAddr(cfg.Server.Port)); err != nil {
log.Panic().Msg(err.Error())
}
}