@@ -3,54 +3,34 @@ package serve
33import (
44 "flag"
55 "fmt"
6+ "github.com/BurntSushi/toml"
67 . "github.com/coinrust/crex"
78 "github.com/coinrust/crex/exchanges"
8- "github.com/spf13/viper"
9- "path/filepath"
10- "strings"
119)
1210
1311var (
1412 configFile string
1513)
1614
17- type ExchangeConfig struct {
18- Exchanges []ExchangeItem `yaml:"exchanges"`
15+ type SConfig struct {
16+ Exchanges []SExchange `toml:"exchange"`
17+ Options map [string ]interface {} `toml:"option"`
1918}
2019
21- type ExchangeItem struct {
22- Name string `yaml :"name"`
23- Debug_Mode bool `yaml :"debug_mode"`
24- Access_Key string `yaml :"access_key"`
25- Secret_Key string `yaml :"secret_key"`
26- Testnet bool `yaml :"testnet"`
27- WebSocket bool `yaml :"websocket"`
20+ type SExchange struct {
21+ Name string `toml :"name"`
22+ DebugMode bool `toml :"debug_mode"`
23+ AccessKey string `toml :"access_key"`
24+ SecretKey string `toml :"secret_key"`
25+ Testnet bool `toml :"testnet"`
26+ WebSocket bool `toml :"websocket"`
2827}
2928
3029// Serve 加载策略并执行
3130func Serve (strategy Strategy ) (err error ) {
32- flag .StringVar (& configFile , "c" , "config.yaml " , "" )
31+ flag .StringVar (& configFile , "c" , "config.toml " , "" )
3332 flag .Parse ()
3433
35- base := filepath .Base (configFile )
36- ext := filepath .Ext (configFile )
37- var configType string
38- if strings .HasPrefix (ext , "." ) {
39- configType = ext [1 :]
40- } else {
41- err = fmt .Errorf ("wrong configuration file" )
42- return
43- }
44-
45- viper .SetConfigType (configType )
46- viper .SetConfigName (base )
47- viper .AddConfigPath ("." )
48-
49- err = viper .ReadInConfig ()
50- if err != nil {
51- return
52- }
53-
5434 err = strategy .SetSelf (strategy )
5535 if err != nil {
5636 return
@@ -75,9 +55,8 @@ func Serve(strategy Strategy) (err error) {
7555
7656// SetupStrategyFromConfig 根据配置文件设置策略参数
7757func SetupStrategyFromConfig (strategy Strategy ) (err error ) {
78- c := ExchangeConfig {}
79- err = viper .Unmarshal (& c )
80- if err != nil {
58+ c := SConfig {}
59+ if _ , err = toml .DecodeFile (configFile , & c ); err != nil {
8160 return
8261 }
8362 if len (c .Exchanges ) == 0 {
@@ -87,19 +66,17 @@ func SetupStrategyFromConfig(strategy Strategy) (err error) {
8766 var exs []Exchange
8867 for _ , ex := range c .Exchanges {
8968 exchange := exchanges .NewExchange (ex .Name ,
90- ApiDebugModeOption (ex .Debug_Mode ),
91- ApiAccessKeyOption (ex .Access_Key ),
92- ApiSecretKeyOption (ex .Secret_Key ),
69+ ApiDebugModeOption (ex .DebugMode ),
70+ ApiAccessKeyOption (ex .AccessKey ),
71+ ApiSecretKeyOption (ex .SecretKey ),
9372 ApiTestnetOption (ex .Testnet ),
9473 ApiWebSocketOption (ex .WebSocket ))
9574 exs = append (exs , exchange )
9675 }
97- err = strategy .Setup (TradeModeLiveTrading , exs ... )
98- if err != nil {
76+ if err = strategy .Setup (TradeModeLiveTrading , exs ... ); err != nil {
9977 return
10078 }
101- options := viper .GetStringMap ("options" )
10279 //log.Printf("options: %#v", options)
103- err = strategy .SetOptions (options )
80+ err = strategy .SetOptions (c . Options )
10481 return
10582}
0 commit comments