mg-transport-core/core/config/config.go

207 lines
4.9 KiB
Go
Raw Normal View History

package config
2019-09-04 15:22:27 +03:00
import (
"io/ioutil"
"path/filepath"
2019-10-18 13:18:36 +03:00
"time"
2019-09-04 15:22:27 +03:00
"github.com/op/go-logging"
"gopkg.in/yaml.v2"
)
// Configuration settings data structure.
type Configuration interface {
2019-09-04 15:22:27 +03:00
GetVersion() string
GetSentryDSN() string
GetLogLevel() logging.Level
GetHTTPConfig() HTTPServerConfig
GetDBConfig() DatabaseConfig
GetAWSConfig() AWS
2019-09-04 15:22:27 +03:00
GetTransportInfo() InfoInterface
GetHTTPClientConfig() *HTTPClientConfig
2019-09-04 15:22:27 +03:00
GetUpdateInterval() int
IsDebug() bool
}
// InfoInterface transport settings data structure.
2019-09-04 15:22:27 +03:00
type InfoInterface interface {
GetName() string
GetCode() string
GetLogoPath() string
2021-11-18 10:43:30 +03:00
GetSecret() string
2019-09-04 15:22:27 +03:00
}
// Config struct.
2019-09-04 15:22:27 +03:00
type Config struct {
HTTPClientConfig *HTTPClientConfig `yaml:"http_client"`
ConfigAWS AWS `yaml:"config_aws"`
TransportInfo Info `yaml:"transport_info"`
HTTPServer HTTPServerConfig `yaml:"http_server"`
Version string `yaml:"version"`
SentryDSN string `yaml:"sentry_dsn"`
Database DatabaseConfig `yaml:"database"`
UpdateInterval int `yaml:"update_interval"`
LogLevel logging.Level `yaml:"log_level"`
Debug bool `yaml:"debug"`
2019-09-04 15:22:27 +03:00
}
// Info struct.
2019-09-04 15:22:27 +03:00
type Info struct {
Name string `yaml:"name"`
Code string `yaml:"code"`
LogoPath string `yaml:"logo_path"`
2021-11-02 09:38:27 +03:00
Secret string `yaml:"secret"`
2019-09-04 15:22:27 +03:00
}
// AWS struct.
type AWS struct {
2019-09-04 15:22:27 +03:00
AccessKeyID string `yaml:"access_key_id"`
SecretAccessKey string `yaml:"secret_access_key"`
Endpoint string `yaml:"endpoint"`
2019-09-04 15:22:27 +03:00
Region string `yaml:"region"`
Bucket string `yaml:"bucket"`
FolderName string `yaml:"folder_name"`
ContentType string `yaml:"content_type"`
}
// DatabaseConfig struct.
2019-09-04 15:22:27 +03:00
type DatabaseConfig struct {
2019-09-19 14:16:52 +03:00
Connection interface{} `yaml:"connection"`
TablePrefix string `yaml:"table_prefix"`
MaxOpenConnections int `yaml:"max_open_connections"`
MaxIdleConnections int `yaml:"max_idle_connections"`
ConnectionLifetime int `yaml:"connection_lifetime"`
Logging bool `yaml:"logging"`
2019-09-04 15:22:27 +03:00
}
// HTTPClientConfig struct.
2019-10-18 13:18:36 +03:00
type HTTPClientConfig struct {
2019-12-12 09:35:05 +03:00
SSLVerification *bool `yaml:"ssl_verification"`
2019-10-18 13:18:36 +03:00
MockAddress string `yaml:"mock_address"`
MockedDomains []string `yaml:"mocked_domains"`
Timeout time.Duration `yaml:"timeout"`
2019-10-18 13:18:36 +03:00
}
// HTTPServerConfig struct.
2019-09-04 15:22:27 +03:00
type HTTPServerConfig struct {
Host string `yaml:"host"`
Listen string `yaml:"listen"`
}
// NewConfig reads configuration file and returns config instance
// Usage:
// NewConfig("config.yml")
func NewConfig(path string) *Config {
return (&Config{}).LoadConfig(path)
}
// LoadConfig read & load configuration file.
2019-09-04 15:22:27 +03:00
func (c *Config) LoadConfig(path string) *Config {
return c.LoadConfigFromData(c.GetConfigData(path))
}
// LoadConfigFromData loads config from byte sequence.
func (c *Config) LoadConfigFromData(data []byte) *Config {
if err := yaml.Unmarshal(data, c); err != nil {
panic(err)
}
return c
}
// GetConfigData returns config file data in form of byte sequence.
func (c *Config) GetConfigData(path string) []byte {
2019-09-04 15:22:27 +03:00
var err error
path, err = filepath.Abs(path)
if err != nil {
panic(err)
}
source, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
return source
2019-09-04 15:22:27 +03:00
}
// GetSentryDSN sentry connection dsn.
2019-09-04 15:22:27 +03:00
func (c Config) GetSentryDSN() string {
return c.SentryDSN
}
// GetVersion transport version.
2019-09-04 15:22:27 +03:00
func (c Config) GetVersion() string {
return c.Version
}
// GetLogLevel log level.
2019-09-04 15:22:27 +03:00
func (c Config) GetLogLevel() logging.Level {
return c.LogLevel
}
// GetTransportInfo transport basic data.
2019-09-04 15:22:27 +03:00
func (c Config) GetTransportInfo() InfoInterface {
return c.TransportInfo
}
// IsDebug debug flag.
2019-09-19 14:16:52 +03:00
func (c Config) IsDebug() bool {
2019-09-04 15:22:27 +03:00
return c.Debug
}
// GetAWSConfig AWS configuration.
func (c Config) GetAWSConfig() AWS {
2019-09-04 15:22:27 +03:00
return c.ConfigAWS
}
// GetDBConfig database configuration.
2019-09-04 15:22:27 +03:00
func (c Config) GetDBConfig() DatabaseConfig {
return c.Database
}
// GetHTTPConfig server configuration.
2019-09-04 15:22:27 +03:00
func (c Config) GetHTTPConfig() HTTPServerConfig {
return c.HTTPServer
}
// GetUpdateInterval user data update interval.
2019-09-04 15:22:27 +03:00
func (c Config) GetUpdateInterval() int {
return c.UpdateInterval
}
// GetHTTPClientConfig returns http client config.
func (c Config) GetHTTPClientConfig() *HTTPClientConfig {
2019-10-18 13:18:36 +03:00
return c.HTTPClientConfig
}
// GetName transport name.
2019-09-04 15:22:27 +03:00
func (t Info) GetName() string {
return t.Name
}
// GetCode transport code.
2019-09-04 15:22:27 +03:00
func (t Info) GetCode() string {
return t.Code
}
// GetLogoPath transport logo.
2019-09-04 15:22:27 +03:00
func (t Info) GetLogoPath() string {
return t.LogoPath
}
2019-12-12 09:35:05 +03:00
2021-11-18 10:43:30 +03:00
// GetSecret returns secret.
func (t Info) GetSecret() string {
return t.Secret
}
// IsSSLVerificationEnabled returns SSL verification flag (default is true).
2019-12-12 09:35:05 +03:00
func (h *HTTPClientConfig) IsSSLVerificationEnabled() bool {
if h.SSLVerification == nil {
return true
}
return *h.SSLVerification
}