mg-transport-core/core/config.go

211 lines
5.0 KiB
Go
Raw Normal View History

2019-09-04 15:22:27 +03:00
package core
import (
"io/ioutil"
"path/filepath"
"regexp"
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"
)
var (
credentialsTransport = []string{
"/api/integration-modules/{code}",
"/api/integration-modules/{code}/edit",
}
markdownSymbols = []string{"*", "_", "`", "["}
2019-12-12 09:35:05 +03:00
regCommandName = regexp.MustCompile(
2021-02-09 15:57:34 +03:00
`^https://?[\da-z.-]+\.(retailcrm\.(ru|pro|es)|ecomlogic\.com|simla(chat)?\.(com|ru))/?$`)
2019-12-12 10:08:26 +03:00
slashRegex = regexp.MustCompile(`/+$`)
)
// ConfigInterface settings data structure.
2019-09-04 15:22:27 +03:00
type ConfigInterface interface {
GetVersion() string
GetSentryDSN() string
GetLogLevel() logging.Level
GetHTTPConfig() HTTPServerConfig
GetDBConfig() DatabaseConfig
GetAWSConfig() ConfigAWS
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
}
// Config struct.
2019-09-04 15:22:27 +03:00
type Config struct {
Version string `yaml:"version"`
LogLevel logging.Level `yaml:"log_level"`
Database DatabaseConfig `yaml:"database"`
SentryDSN string `yaml:"sentry_dsn"`
HTTPServer HTTPServerConfig `yaml:"http_server"`
Debug bool `yaml:"debug"`
UpdateInterval int `yaml:"update_interval"`
ConfigAWS ConfigAWS `yaml:"config_aws"`
TransportInfo Info `yaml:"transport_info"`
HTTPClientConfig *HTTPClientConfig `yaml:"http_client"`
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"`
}
// ConfigAWS struct.
2019-09-04 15:22:27 +03:00
type ConfigAWS struct {
AccessKeyID string `yaml:"access_key_id"`
SecretAccessKey string `yaml:"secret_access_key"`
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"`
Logging bool `yaml:"logging"`
TablePrefix string `yaml:"table_prefix"`
MaxOpenConnections int `yaml:"max_open_connections"`
MaxIdleConnections int `yaml:"max_idle_connections"`
ConnectionLifetime int `yaml:"connection_lifetime"`
2019-09-04 15:22:27 +03:00
}
// HTTPClientConfig struct.
2019-10-18 13:18:36 +03:00
type HTTPClientConfig struct {
Timeout time.Duration `yaml:"timeout"`
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"`
}
// 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.
2019-09-04 15:22:27 +03:00
func (c Config) GetAWSConfig() ConfigAWS {
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
// 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
}