Skip to content

Commit

Permalink
fix toml yaml config (#53)
Browse files Browse the repository at this point in the history
修复通过 toml 或者 yaml 文件配置。
Fixes #52
  • Loading branch information
Patrick0308 authored Oct 10, 2024
1 parent da2cdc4 commit cda75dc
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 47 deletions.
61 changes: 42 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,42 +62,65 @@ All envs is listed in the last of [README](#environment-variables)

### Load from file[yaml,toml]

yaml
#### yaml example

To load configuration from a YAML file, use the following code snippet:

```golang
conf, err := config.New(config.WithFilePath("./test.yaml"))
```

toml
Here is an example of what the `test.yaml` file might look like:


```yaml
longport:
app_key: xxxxx
app_secret: xxxxx
access_token: xxxxx
```
#### toml example
Similarly, to load configuration from a TOML file, use this code snippet:
```golang
conf, err := config.New(config.WithFilePath("./test.toml"))
```

And here is an example of a `test.toml` file:

```toml
[longport]
app_key = "xxxxx"
app_secret = "xxxxx"
access_token = "xxxxx"
```

### Init Config manually

Config structure as follow:

```golang
type Config struct {
HttpURL string `env:"LONGPORT_HTTP_URL" yaml:"LONGPORT_HTTP_URL" toml:"LONGPORT_HTTP_URL"`
HTTPTimeout time.Duration `env:"LONGPORT_HTTP_TIMEOUT" yaml:"LONGPORT_HTTP_TIMEOUT" toml:"LONGPORT_HTTP_TIMEOUT"`
AppKey string `env:"LONGPORT_APP_KEY" yaml:"LONGPORT_APP_KEY" toml:"LONGPORT_APP_KEY"`
AppSecret string `env:"LONGPORT_APP_SECRET" yaml:"LONGPORT_APP_SECRET" toml:"LONGPORT_APP_SECRET"`
AccessToken string `env:"LONGPORT_ACCESS_TOKEN" yaml:"LONGPORT_ACCESS_TOKEN" toml:"LONGPORT_ACCESS_TOKEN"`
TradeUrl string `env:"LONGPORT_TRADE_URL" yaml:"LONGPORT_TRADE_URL" toml:"LONGPORT_TRADE_URL"`
QuoteUrl string `env:"LONGPORT_QUOTE_URL" yaml:"LONGPORT_QUOTE_URL" toml:"LONGPORT_QUOTE_URL"`
EnableOvernight bool `env:"LONGPORT_ENABLE_OVERNIGHT" yaml:"LONGPORT_ENABLE_OVERNIGHT" toml:"LONGPORT_ENABLE_OVERNIGHT"`

LogLevel string `env:"LONGPORT_LOG_LEVEL" yaml:"LONGPORT_LOG_LEVEL" toml:"LONGPORT_LOG_LEVEL"`
HttpURL string `env:"LONGPORT_HTTP_URL" yaml:"http_url" toml:"http_url"`
HTTPTimeout time.Duration `env:"LONGPORT_HTTP_TIMEOUT" yaml:"http_timeout" toml:"http_timeout"`
AppKey string `env:"LONGPORT_APP_KEY" yaml:"app_key" toml:"app_key"`
AppSecret string `env:"LONGPORT_APP_SECRET" yaml:"app_secret" toml:"app_secret"`
AccessToken string `env:"LONGPORT_ACCESS_TOKEN" yaml:"access_token" toml:"access_token"`
TradeUrl string `env:"LONGPORT_TRADE_URL" yaml:"trade_url" toml:"trade_url"`
QuoteUrl string `env:"LONGPORT_QUOTE_URL" yaml:"quote_url" toml:"quote_url"`
EnableOvernight bool `env:"LONGPORT_ENABLE_OVERNIGHT" yaml:"enable_overnight" toml:"enable_overnight"`

LogLevel string `env:"LONGPORT_LOG_LEVEL" yaml:"log_level" toml:"log_level"`
// LONGPORT protocol config
AuthTimeout time.Duration `env:"LONGPORT_AUTH_TIMEOUT" yaml:"LONGPORT_AUTH_TIMEOUT"toml:"LONGPORT_AUTH_TIMEOUT"`
Timeout time.Duration `env:"LONGPORT_TIMEOUT" yaml:"LONGPORT_TIMEOUT" toml:"LONGPORT_TIMEOUT"`
WriteQueueSize int `env:"LONGPORT_WRITE_QUEUE_SIZE" yaml:"LONGPORT_WRITE_QUEUE_SIZE" toml:"LONGPORT_WRITE_QUEUE_SIZE"`
ReadQueueSize int `env:"LONGPORT_READ_QUEUE_SIZE" yaml:"LONGPORT_READ_QUEUE_SIZE" toml:"LONGPORT_READ_QUEUE_SIZE"`
ReadBufferSize int `env:"LONGPORT_READ_BUFFER_SIZE" yaml:"LONGPORT_READ_BUFFER_SIZE" toml:"LONGPORT_READ_BUFFER_SIZE"`
MinGzipSize int `env:"LONGPORT_MIN_GZIP_SIZE" yaml:"LONGPORT_MIN_GZIP_SIZE" toml:"LONGPORT_MIN_GZIP_SIZE"`
Region Region `env:"LONGPORT_REGION" yaml:"LONGPORT_REGION" toml:"LONGPORT_REGION"`
AuthTimeout time.Duration `env:"LONGPORT_AUTH_TIMEOUT" yaml:"auth_timeout" toml:"timeout"`
Timeout time.Duration `env:"LONGPORT_TIMEOUT" yaml:"timeout" toml:"timeout"`
WriteQueueSize int `env:"LONGPORT_WRITE_QUEUE_SIZE" yaml:"write_queue_size" toml:"write_queue_size"`
ReadQueueSize int `env:"LONGPORT_READ_QUEUE_SIZE" yaml:"read_queue_size" toml:"read_queue_size"`
ReadBufferSize int `env:"LONGPORT_READ_BUFFER_SIZE" yaml:"read_buffer_size" toml:"read_buffer_size"`
MinGzipSize int `env:"LONGPORT_MIN_GZIP_SIZE" yaml:"min_gzip_size" toml:"min_gzip_size"`
Region Region `env:"LONGPORT_REGION" yaml:"region" toml:"region"`
}

```
Expand Down
39 changes: 22 additions & 17 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,31 @@ type Config struct {
// Client custom http client
Client *http.Client

HttpURL string `env:"LONGBRIDGE_HTTP_URL,LONGPORT_HTTP_URL" yaml:"LONGBRIDGE_HTTP_URL,LONGPORT_HTTP_URL" toml:"LONGBRIDGE_HTTP_URL,LONGPORT_HTTP_URL"`
HTTPTimeout time.Duration `env:"LONGBRIDGE_HTTP_TIMEOUT,LONGPORT_HTTP_TIMEOUT" yaml:"LONGBRIDGE_HTTP_TIMEOUT,LONGPORT_HTTP_TIMEOUT" toml:"LONGPORT_HTTP_TIMEOUT"`
AppKey string `env:"LONGBRIDGE_APP_KEY,LONGPORT_APP_KEY" yaml:"LONGBRIDGE_APP_KEY,LONGPORT_APP_KEY" toml:"LONGBRIDGE_APP_KEY,LONGPORT_APP_KEY"`
AppSecret string `env:"LONGBRIDGE_APP_SECRET,LONGPORT_APP_SECRET" yaml:"eONGBRIDGE_APP_SECRET,LONGPORT_APP_SECRET" toml:"LONGBRIDGE_APP_SECRET,LONGPORT_APP_SECRET"`
AccessToken string `env:"LONGBRIDGE_ACCESS_TOKEN,LONGPORT_ACCESS_TOKEN" yaml:"LONGBRIDGE_ACCESS_TOKEN,LONGPORT_ACCESS_TOKEN" toml:"LONGBRIDGE_ACCESS_TOKEN,LONGPORT_ACCESS_TOKEN"`
TradeUrl string `env:"LONGBRIDGE_TRADE_URL,LONGPORT_TRADE_URL" yaml:"LONGBRIDGE_TRADE_URL,LONGPORT_TRADE_URL" toml:"LONGBRIDGE_TRADE_URL,LONGPORT_TRADE_URL"`
QuoteUrl string `env:"LONGBRIDGE_QUOTE_URL,LONGPORT_QUOTE_URL" yaml:"LONGBRIDGE_QUOTE_URL,LONGPORT_QUOTE_URL" toml:"LONGBRIDGE_QUOTE_URL,LONGPORT_QUOTE_URL"`
EnableOvernight bool `env:"LONGPORT_ENABLE_OVERNIGHT" yaml:"LONGPORT_ENABLE_OVERNIGHT" toml:"LONGPORT_ENABLE_OVERNIGHT"`

LogLevel string `env:"LONGBRIDGE_LOG_LEVEL,LONGPORT_LOG_LEVEL" yaml:"LONGBRIDGE_LOG_LEVEL,LONGPORT_LOG_LEVEL" toml:"LONGBRIDGE_LOG_LEVEL,LONGPORT_LOG_LEVEL"`
HttpURL string `env:"LONGBRIDGE_HTTP_URL,LONGPORT_HTTP_URL" yaml:"http_url" toml:"http_url"`
HTTPTimeout time.Duration `env:"LONGBRIDGE_HTTP_TIMEOUT,LONGPORT_HTTP_TIMEOUT" yaml:"http_timeout" toml:"http_timeout"`
AppKey string `env:"LONGBRIDGE_APP_KEY,LONGPORT_APP_KEY" yaml:"app_key" toml:"app_key"`
AppSecret string `env:"LONGBRIDGE_APP_SECRET,LONGPORT_APP_SECRET" yaml:"app_secret" toml:"app_secret"`
AccessToken string `env:"LONGBRIDGE_ACCESS_TOKEN,LONGPORT_ACCESS_TOKEN" yaml:"access_token" toml:"access_token"`
TradeUrl string `env:"LONGBRIDGE_TRADE_URL,LONGPORT_TRADE_URL" yaml:"trade_url" toml:"trade_url"`
QuoteUrl string `env:"LONGBRIDGE_QUOTE_URL,LONGPORT_QUOTE_URL" yaml:"quote_url" toml:"quote_url"`
EnableOvernight bool `env:"LONGPORT_ENABLE_OVERNIGHT" yaml:"enable_overnight" toml:"enable_overnight"`

LogLevel string `env:"LONGBRIDGE_LOG_LEVEL,LONGPORT_LOG_LEVEL" yaml:"log_level" toml:"log_level"`
logger log.Logger

// longbridge protocol config
AuthTimeout time.Duration `env:"LONGBRIDGE_AUTH_TIMEOUT,LONGPORT_AUTH_TIMEOUT" yaml:"LONGBRIDGE_AUTH_TIMEOUT,LONGPORT_AUTH_TIMEOUT" toml:"LONGBRIDGE_AUTH_TIMEOUT,LONGPORT_AUTH_TIMEOUT"`
Timeout time.Duration `env:"LONGBRIDGE_TIMEOUT,LONGPORT_TIMEOUT" yaml:"LONGBRIDGE_TIMEOUT,LONGPORT_TIMEOUT" toml:"LONGBRIDGE_TIMEOUT,LONGPORT_TIMEOUT"`
WriteQueueSize int `env:"LONGBRIDGE_WRITE_QUEUE_SIZE,LONGPORT_WRITE_QUEUE_SIZE" yaml:"LONGBRIDGE_WRITE_QUEUE_SIZE,LONGPORT_WRITE_QUEUE_SIZE" toml:"LONGBRIDGE_WRITE_QUEUE_SIZE,LONGPORT_WRITE_QUEUE_SIZE"`
ReadQueueSize int `env:"LONGBRIDGE_READ_QUEUE_SIZE,LONGPORT_READ_QUEUE_SIZE" yaml:"LONGBRIDGE_READ_QUEUE_SIZE,LONGPORT_READ_QUEUE_SIZE" toml:"LONGBRIDGE_READ_QUEUE_SIZE,LONGPORT_READ_QUEUE_SIZE"`
ReadBufferSize int `env:"LONGBRIDGE_READ_BUFFER_SIZE,LONGPORT_READ_BUFFER_SIZE" yaml:"LONGBRIDGE_READ_BUFFER_SIZE,LONGPORT_READ_BUFFER_SIZE" toml:"LONGBRIDGE_READ_BUFFER_SIZE,LONGPORT_READ_BUFFER_SIZE"`
MinGzipSize int `env:"LONGBRIDGE_MIN_GZIP_SIZE,LONGPORT_MIN_GZIP_SIZE" yaml:"LONGBRIDGE_MIN_GZIP_SIZE,LONGPORT_MIN_GZIP_SIZE" toml:"LONGBRIDGE_MIN_GZIP_SIZE,LONGPORT_MIN_GZIP_SIZE"`
Region Region `env:"LONGPORT_REGION" yaml:"LONGPORT_REGION" toml:"LONGPORT_REGION"`
AuthTimeout time.Duration `env:"LONGBRIDGE_AUTH_TIMEOUT,LONGPORT_AUTH_TIMEOUT" yaml:"auth_timeout" toml:"auth_timeout"`
Timeout time.Duration `env:"LONGBRIDGE_TIMEOUT,LONGPORT_TIMEOUT" yaml:"timeout" toml:"timeout"`
WriteQueueSize int `env:"LONGBRIDGE_WRITE_QUEUE_SIZE,LONGPORT_WRITE_QUEUE_SIZE" yaml:"write_queue_size" toml:"write_queue_size"`
ReadQueueSize int `env:"LONGBRIDGE_READ_QUEUE_SIZE,LONGPORT_READ_QUEUE_SIZE" yaml:"read_queue_size" toml:"read_queue_size"`
ReadBufferSize int `env:"LONGBRIDGE_READ_BUFFER_SIZE,LONGPORT_READ_BUFFER_SIZE" yaml:"read_buffer_size" toml:"read_buffer_size"`
MinGzipSize int `env:"LONGBRIDGE_MIN_GZIP_SIZE,LONGPORT_MIN_GZIP_SIZE" yaml:"min_gzip_size" toml:"min_gzip_size"`
Region Region `env:"LONGPORT_REGION" yaml:"region" toml:"region"`
}

// parseConfig is a config for toml/yaml
type parseConfig struct {
Longport *Config `toml:"longport" yaml:"longport"`
}

func (c *Config) SetLogger(l log.Logger) {
Expand Down
31 changes: 31 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,46 @@ package config_test

import (
"testing"
"time"

"github.com/longbridgeapp/assert"
"github.com/longportapp/openapi-go/config"
)

var expectedConfig = &config.Config{
HttpURL: "http://test",
HTTPTimeout: 12 * time.Second,
AppKey: "test_app_key",
AppSecret: "test_app_secret",
AccessToken: "test_access_token",
TradeUrl: "http://trade_test",
QuoteUrl: "http://quote_test",
EnableOvernight: true,
AuthTimeout: 12 * time.Second,
Timeout: 12 * time.Second,
WriteQueueSize: 12,
ReadQueueSize: 12,
ReadBufferSize: 12,
MinGzipSize: 12,
Region: "hk",
}

func Test_withConfigKey(t *testing.T) {
var c, err = config.New(config.WithConfigKey("appKey", "appSecret", "accessToken"))
assert.NoError(t, err)
assert.Equal(t, "appKey", c.AppKey)
assert.Equal(t, "appSecret", c.AppSecret)
assert.Equal(t, "accessToken", c.AccessToken)
}

func Test_YamlConfig(t *testing.T) {
c, err := config.New(config.WithFilePath("./testdata/test_config.yaml"))
assert.NoError(t, err)
assert.Equal(t, expectedConfig, c)
}

func Test_TomlConfig(t *testing.T) {
c, err := config.New(config.WithFilePath("./testdata/test_config.toml"))
assert.NoError(t, err)
assert.Equal(t, expectedConfig, c)
}
13 changes: 8 additions & 5 deletions config/config_toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import (
type TOMLConfig struct {
}

func (c *TOMLConfig) GetConfig(opts *Options) (data *Config, err error) {
data = &Config{}
_, err = toml.DecodeFile(opts.filePath, data)
func (c *TOMLConfig) GetConfig(opts *Options) (*Config, error) {
parseData := &parseConfig{}
_, err := toml.DecodeFile(opts.filePath, parseData)
if err != nil {
err = errors.Wrapf(err, "TOML GetConfig err")
return
return nil, err
}
return
if parseData.Longport == nil {
return nil, errors.New("Longport config is not exist in toml file")
}
return parseData.Longport, nil
}
15 changes: 9 additions & 6 deletions config/config_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ import (
type YAMLConfig struct {
}

func (c *YAMLConfig) GetConfig(opts *Options) (data *Config, err error) {
data = &Config{}
func (c *YAMLConfig) GetConfig(opts *Options) (*Config, error) {
parseData := &parseConfig{}
bytes, err := ioutil.ReadFile(opts.filePath)
if err != nil {
err = errors.Wrapf(err, "YAML ReadFile err")
return
return nil, err
}
err = yaml.Unmarshal(bytes, data)
err = yaml.Unmarshal(bytes, parseData)
if err != nil {
err = errors.Wrapf(err, "YAML GetConfig err")
return
return nil, err
}
return
if parseData.Longport == nil {
return nil, errors.New("Longport config is not exist in yaml file")
}
return parseData.Longport, nil
}

0 comments on commit cda75dc

Please sign in to comment.