Skip to content

Commit 495e74f

Browse files
committed
Use toml instead of yaml
1 parent d21d483 commit 495e74f

9 files changed

Lines changed: 154 additions & 182 deletions

File tree

configtest/configtest.go

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,58 @@
11
package configtest
22

33
import (
4-
"github.com/spf13/viper"
4+
"github.com/BurntSushi/toml"
55
"log"
6+
"path/filepath"
67
)
78

9+
var (
10+
testDataDir = "../../testdata/"
11+
)
12+
13+
type Config struct {
14+
BinanceFutures TestConfig `toml:"binancefutures"`
15+
Bitmex TestConfig `toml:"bitmex"`
16+
Bybit TestConfig `toml:"bybit"`
17+
Deribit TestConfig `toml:"deribit"`
18+
Hbdm TestConfig `toml:"hbdm"`
19+
HbdmSwap TestConfig `toml:"hbdmswap"`
20+
OkexFutures TestConfig `toml:"okexfutures"`
21+
OkexSwap TestConfig `toml:"okexswap"`
22+
}
23+
824
type TestConfig struct {
9-
AccessKey string
10-
SecretKey string
11-
Passphrase string
12-
Testnet bool
13-
ProxyURL string
25+
AccessKey string `toml:"access_key"`
26+
SecretKey string `toml:"secret_key"`
27+
Passphrase string `toml:"passphrase"`
28+
Testnet bool `toml:"testnet"`
29+
ProxyURL string `toml:"proxy_url"`
1430
}
1531

1632
func LoadTestConfig(name string) *TestConfig {
17-
viper.SetConfigName("configtest")
18-
viper.AddConfigPath("../../testdata")
19-
err := viper.ReadInConfig()
20-
if err != nil {
33+
fPath := filepath.Join(testDataDir, "configtest.toml")
34+
var cfg Config
35+
if _, err := toml.DecodeFile(fPath, &cfg); err != nil {
2136
log.Panic(err)
2237
}
23-
cfg := &TestConfig{}
24-
items := viper.GetStringMapString(name)
25-
if len(items) == 0 {
26-
log.Panic("test config not found [configtest.yaml]")
27-
}
28-
for key, value := range items {
29-
switch key {
30-
case "access_key":
31-
cfg.AccessKey = value
32-
case "secret_key":
33-
cfg.SecretKey = value
34-
case "passphrase":
35-
cfg.Passphrase = value
36-
case "testnet":
37-
cfg.Testnet = value == "true"
38-
case "proxy_url":
39-
cfg.ProxyURL = value
40-
}
38+
tCfg := &TestConfig{}
39+
switch name {
40+
case "binancefutures":
41+
tCfg = &cfg.BinanceFutures
42+
case "bitmex":
43+
tCfg = &cfg.Bitmex
44+
case "bybit":
45+
tCfg = &cfg.Bybit
46+
case "deribit":
47+
tCfg = &cfg.Deribit
48+
case "hbdm":
49+
tCfg = &cfg.Hbdm
50+
case "hbdmswap":
51+
tCfg = &cfg.HbdmSwap
52+
case "okexfutures":
53+
tCfg = &cfg.OkexFutures
54+
case "okexswap":
55+
tCfg = &cfg.OkexSwap
4156
}
42-
return cfg
57+
return tCfg
4358
}

configtest/configtest_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package configtest
2+
3+
import "testing"
4+
5+
func TestLoadTestConfig(t *testing.T) {
6+
testDataDir = "../testdata/"
7+
tCfg := LoadTestConfig("bybit")
8+
t.Logf("%#v", tCfg)
9+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/coinrust/crex
33
go 1.13
44

55
require (
6+
github.com/BurntSushi/toml v0.3.1
67
github.com/MauriceGit/skiplist v0.0.0-20191117202105-643e379adb62
78
github.com/adshao/go-binance v0.0.0-20200326152909-7314295d8a33
89
github.com/chuckpreslar/emission v0.0.0-20170206194824-a7ddd980baf9
@@ -14,5 +15,4 @@ require (
1415
github.com/micro/go-micro v1.18.0 // indirect
1516
github.com/sony/sonyflake v1.0.0
1617
github.com/spf13/cast v1.3.1
17-
github.com/spf13/viper v1.6.3
1818
)

go.sum

Lines changed: 0 additions & 57 deletions
Large diffs are not rendered by default.

serve/serve.go

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,34 @@ package serve
33
import (
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

1311
var (
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 加载策略并执行
3130
func 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 根据配置文件设置策略参数
7757
func 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
}

serve/serve_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package serve
2+
3+
import (
4+
"github.com/BurntSushi/toml"
5+
"testing"
6+
)
7+
8+
func TestSetupConfig(t *testing.T) {
9+
var c SConfig
10+
if _, err := toml.DecodeFile("../testdata/serve-config-sample.toml", &c); err != nil {
11+
t.Error(err)
12+
return
13+
}
14+
15+
t.Logf("%#v", c)
16+
}

testdata/configtest.example.toml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# example:
2+
# proxy_url = "socks5://127.0.0.1:1080"
3+
4+
[binancefutures]
5+
access_key = ""
6+
secret_key = ""
7+
testnet = true
8+
proxy_url = ""
9+
10+
[bitmex]
11+
access_key = ""
12+
secret_key = ""
13+
testnet = true
14+
proxy_url = ""
15+
16+
[bybit]
17+
access_key = ""
18+
secret_key = ""
19+
testnet = true
20+
proxy_url = ""
21+
22+
[deribit]
23+
access_key = ""
24+
secret_key = ""
25+
testnet = true
26+
proxy_url = ""
27+
28+
[hbdm]
29+
access_key = ""
30+
secret_key = ""
31+
testnet = false
32+
proxy_url = "socks5://127.0.0.1:1080"
33+
34+
[hbdmswap]
35+
access_key = ""
36+
secret_key = ""
37+
testnet = false
38+
proxy_url = "socks5://127.0.0.1:1080"
39+
40+
[okexfutures]
41+
access_key = ""
42+
secret_key = ""
43+
passphrase = ""
44+
testnet = false
45+
proxy_url = ""
46+
47+
okexswap:
48+
access_key: ""
49+
secret_key: ""
50+
passphrase: ""
51+
testnet: false
52+
proxy_url: ""
53+

testdata/configtest.example.yaml

Lines changed: 0 additions & 53 deletions
This file was deleted.

testdata/serve-config-sample.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[[exchange]]
2+
name = "deribit"
3+
debug_mode = false
4+
access_key = ""
5+
secret_key = ""
6+
testnet = true
7+
websocket = false
8+
9+
[option]
10+
log_only = false # 只输出日志
11+
currency = "BTC" # 货币 BTC
12+
currency_pair = "BTC-PERPETUAL" # BTC

0 commit comments

Comments
 (0)