Skip to content

Commit

Permalink
Use toml instead of yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
f0cii committed May 3, 2020
1 parent d21d483 commit 495e74f
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 182 deletions.
73 changes: 44 additions & 29 deletions configtest/configtest.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,58 @@
package configtest

import (
"github.com/spf13/viper"
"github.com/BurntSushi/toml"
"log"
"path/filepath"
)

var (
testDataDir = "../../testdata/"
)

type Config struct {
BinanceFutures TestConfig `toml:"binancefutures"`
Bitmex TestConfig `toml:"bitmex"`
Bybit TestConfig `toml:"bybit"`
Deribit TestConfig `toml:"deribit"`
Hbdm TestConfig `toml:"hbdm"`
HbdmSwap TestConfig `toml:"hbdmswap"`
OkexFutures TestConfig `toml:"okexfutures"`
OkexSwap TestConfig `toml:"okexswap"`
}

type TestConfig struct {
AccessKey string
SecretKey string
Passphrase string
Testnet bool
ProxyURL string
AccessKey string `toml:"access_key"`
SecretKey string `toml:"secret_key"`
Passphrase string `toml:"passphrase"`
Testnet bool `toml:"testnet"`
ProxyURL string `toml:"proxy_url"`
}

func LoadTestConfig(name string) *TestConfig {
viper.SetConfigName("configtest")
viper.AddConfigPath("../../testdata")
err := viper.ReadInConfig()
if err != nil {
fPath := filepath.Join(testDataDir, "configtest.toml")
var cfg Config
if _, err := toml.DecodeFile(fPath, &cfg); err != nil {
log.Panic(err)
}
cfg := &TestConfig{}
items := viper.GetStringMapString(name)
if len(items) == 0 {
log.Panic("test config not found [configtest.yaml]")
}
for key, value := range items {
switch key {
case "access_key":
cfg.AccessKey = value
case "secret_key":
cfg.SecretKey = value
case "passphrase":
cfg.Passphrase = value
case "testnet":
cfg.Testnet = value == "true"
case "proxy_url":
cfg.ProxyURL = value
}
tCfg := &TestConfig{}
switch name {
case "binancefutures":
tCfg = &cfg.BinanceFutures
case "bitmex":
tCfg = &cfg.Bitmex
case "bybit":
tCfg = &cfg.Bybit
case "deribit":
tCfg = &cfg.Deribit
case "hbdm":
tCfg = &cfg.Hbdm
case "hbdmswap":
tCfg = &cfg.HbdmSwap
case "okexfutures":
tCfg = &cfg.OkexFutures
case "okexswap":
tCfg = &cfg.OkexSwap
}
return cfg
return tCfg
}
9 changes: 9 additions & 0 deletions configtest/configtest_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package configtest

import "testing"

func TestLoadTestConfig(t *testing.T) {
testDataDir = "../testdata/"
tCfg := LoadTestConfig("bybit")
t.Logf("%#v", tCfg)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/coinrust/crex
go 1.13

require (
github.com/BurntSushi/toml v0.3.1
github.com/MauriceGit/skiplist v0.0.0-20191117202105-643e379adb62
github.com/adshao/go-binance v0.0.0-20200326152909-7314295d8a33
github.com/chuckpreslar/emission v0.0.0-20170206194824-a7ddd980baf9
Expand All @@ -14,5 +15,4 @@ require (
github.com/micro/go-micro v1.18.0 // indirect
github.com/sony/sonyflake v1.0.0
github.com/spf13/cast v1.3.1
github.com/spf13/viper v1.6.3
)
57 changes: 0 additions & 57 deletions go.sum

Large diffs are not rendered by default.

61 changes: 19 additions & 42 deletions serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,34 @@ package serve
import (
"flag"
"fmt"
"github.com/BurntSushi/toml"
. "github.com/coinrust/crex"
"github.com/coinrust/crex/exchanges"
"github.com/spf13/viper"
"path/filepath"
"strings"
)

var (
configFile string
)

type ExchangeConfig struct {
Exchanges []ExchangeItem `yaml:"exchanges"`
type SConfig struct {
Exchanges []SExchange `toml:"exchange"`
Options map[string]interface{} `toml:"option"`
}

type ExchangeItem struct {
Name string `yaml:"name"`
Debug_Mode bool `yaml:"debug_mode"`
Access_Key string `yaml:"access_key"`
Secret_Key string `yaml:"secret_key"`
Testnet bool `yaml:"testnet"`
WebSocket bool `yaml:"websocket"`
type SExchange struct {
Name string `toml:"name"`
DebugMode bool `toml:"debug_mode"`
AccessKey string `toml:"access_key"`
SecretKey string `toml:"secret_key"`
Testnet bool `toml:"testnet"`
WebSocket bool `toml:"websocket"`
}

// Serve 加载策略并执行
func Serve(strategy Strategy) (err error) {
flag.StringVar(&configFile, "c", "config.yaml", "")
flag.StringVar(&configFile, "c", "config.toml", "")
flag.Parse()

base := filepath.Base(configFile)
ext := filepath.Ext(configFile)
var configType string
if strings.HasPrefix(ext, ".") {
configType = ext[1:]
} else {
err = fmt.Errorf("wrong configuration file")
return
}

viper.SetConfigType(configType)
viper.SetConfigName(base)
viper.AddConfigPath(".")

err = viper.ReadInConfig()
if err != nil {
return
}

err = strategy.SetSelf(strategy)
if err != nil {
return
Expand All @@ -75,9 +55,8 @@ func Serve(strategy Strategy) (err error) {

// SetupStrategyFromConfig 根据配置文件设置策略参数
func SetupStrategyFromConfig(strategy Strategy) (err error) {
c := ExchangeConfig{}
err = viper.Unmarshal(&c)
if err != nil {
c := SConfig{}
if _, err = toml.DecodeFile(configFile, &c); err != nil {
return
}
if len(c.Exchanges) == 0 {
Expand All @@ -87,19 +66,17 @@ func SetupStrategyFromConfig(strategy Strategy) (err error) {
var exs []Exchange
for _, ex := range c.Exchanges {
exchange := exchanges.NewExchange(ex.Name,
ApiDebugModeOption(ex.Debug_Mode),
ApiAccessKeyOption(ex.Access_Key),
ApiSecretKeyOption(ex.Secret_Key),
ApiDebugModeOption(ex.DebugMode),
ApiAccessKeyOption(ex.AccessKey),
ApiSecretKeyOption(ex.SecretKey),
ApiTestnetOption(ex.Testnet),
ApiWebSocketOption(ex.WebSocket))
exs = append(exs, exchange)
}
err = strategy.Setup(TradeModeLiveTrading, exs...)
if err != nil {
if err = strategy.Setup(TradeModeLiveTrading, exs...); err != nil {
return
}
options := viper.GetStringMap("options")
//log.Printf("options: %#v", options)
err = strategy.SetOptions(options)
err = strategy.SetOptions(c.Options)
return
}
16 changes: 16 additions & 0 deletions serve/serve_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package serve

import (
"github.com/BurntSushi/toml"
"testing"
)

func TestSetupConfig(t *testing.T) {
var c SConfig
if _, err := toml.DecodeFile("../testdata/serve-config-sample.toml", &c); err != nil {
t.Error(err)
return
}

t.Logf("%#v", c)
}
53 changes: 53 additions & 0 deletions testdata/configtest.example.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# example:
# proxy_url = "socks5://127.0.0.1:1080"

[binancefutures]
access_key = ""
secret_key = ""
testnet = true
proxy_url = ""

[bitmex]
access_key = ""
secret_key = ""
testnet = true
proxy_url = ""

[bybit]
access_key = ""
secret_key = ""
testnet = true
proxy_url = ""

[deribit]
access_key = ""
secret_key = ""
testnet = true
proxy_url = ""

[hbdm]
access_key = ""
secret_key = ""
testnet = false
proxy_url = "socks5://127.0.0.1:1080"

[hbdmswap]
access_key = ""
secret_key = ""
testnet = false
proxy_url = "socks5://127.0.0.1:1080"

[okexfutures]
access_key = ""
secret_key = ""
passphrase = ""
testnet = false
proxy_url = ""

okexswap:
access_key: ""
secret_key: ""
passphrase: ""
testnet: false
proxy_url: ""

53 changes: 0 additions & 53 deletions testdata/configtest.example.yaml

This file was deleted.

12 changes: 12 additions & 0 deletions testdata/serve-config-sample.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[[exchange]]
name = "deribit"
debug_mode = false
access_key = ""
secret_key = ""
testnet = true
websocket = false

[option]
log_only = false # 只输出日志
currency = "BTC" # 货币 BTC
currency_pair = "BTC-PERPETUAL" # BTC

0 comments on commit 495e74f

Please sign in to comment.