diff --git a/README.md b/README.md index c81ea8b..4f22f9c 100644 --- a/README.md +++ b/README.md @@ -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"` } ``` diff --git a/config/config.go b/config/config.go index a22835f..a95b5a1 100644 --- a/config/config.go +++ b/config/config.go @@ -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) { diff --git a/config/config_test.go b/config/config_test.go index 4b125cf..1f4184c 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -2,11 +2,30 @@ 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) @@ -14,3 +33,15 @@ func Test_withConfigKey(t *testing.T) { 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) +} diff --git a/config/config_toml.go b/config/config_toml.go index daa73c5..246a9ae 100644 --- a/config/config_toml.go +++ b/config/config_toml.go @@ -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 } diff --git a/config/config_yaml.go b/config/config_yaml.go index 5f741a9..de2d2f1 100644 --- a/config/config_yaml.go +++ b/config/config_yaml.go @@ -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 }