2019-09-04 15:22:27 +03:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
2019-09-04 15:52:41 +03:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2019-09-04 15:52:41 +03:00
|
|
|
var (
|
|
|
|
markdownSymbols = []string{"*", "_", "`", "["}
|
2021-07-27 14:29:05 +03:00
|
|
|
slashRegex = regexp.MustCompile(`/+$`)
|
2019-09-04 15:52:41 +03:00
|
|
|
)
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// 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
|
2019-12-13 12:13:57 +03:00
|
|
|
GetHTTPClientConfig() *HTTPClientConfig
|
2019-09-04 15:22:27 +03:00
|
|
|
GetUpdateInterval() int
|
|
|
|
IsDebug() bool
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// InfoInterface transport settings data structure.
|
2019-09-04 15:22:27 +03:00
|
|
|
type InfoInterface interface {
|
|
|
|
GetName() string
|
|
|
|
GetCode() string
|
|
|
|
GetLogoPath() string
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// Config struct.
|
2019-09-04 15:22:27 +03:00
|
|
|
type Config struct {
|
2019-12-13 12:13:57 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +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
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// 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"`
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +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"`
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +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)
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// LoadConfig read & load configuration file.
|
2019-09-04 15:22:27 +03:00
|
|
|
func (c *Config) LoadConfig(path string) *Config {
|
2019-09-12 11:49:41 +03:00
|
|
|
return c.LoadConfigFromData(c.GetConfigData(path))
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// LoadConfigFromData loads config from byte sequence.
|
2019-09-12 11:49:41 +03:00
|
|
|
func (c *Config) LoadConfigFromData(data []byte) *Config {
|
|
|
|
if err := yaml.Unmarshal(data, c); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// GetConfigData returns config file data in form of byte sequence.
|
2019-09-12 11:49:41 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-09-12 11:49:41 +03:00
|
|
|
return source
|
2019-09-04 15:22:27 +03:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// GetSentryDSN sentry connection dsn.
|
2019-09-04 15:22:27 +03:00
|
|
|
func (c Config) GetSentryDSN() string {
|
|
|
|
return c.SentryDSN
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// GetVersion transport version.
|
2019-09-04 15:22:27 +03:00
|
|
|
func (c Config) GetVersion() string {
|
|
|
|
return c.Version
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// GetLogLevel log level.
|
2019-09-04 15:22:27 +03:00
|
|
|
func (c Config) GetLogLevel() logging.Level {
|
|
|
|
return c.LogLevel
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// GetTransportInfo transport basic data.
|
2019-09-04 15:22:27 +03:00
|
|
|
func (c Config) GetTransportInfo() InfoInterface {
|
|
|
|
return c.TransportInfo
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// GetAWSConfig AWS configuration.
|
2019-09-04 15:22:27 +03:00
|
|
|
func (c Config) GetAWSConfig() ConfigAWS {
|
|
|
|
return c.ConfigAWS
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// GetDBConfig database configuration.
|
2019-09-04 15:22:27 +03:00
|
|
|
func (c Config) GetDBConfig() DatabaseConfig {
|
|
|
|
return c.Database
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// GetHTTPConfig server configuration.
|
2019-09-04 15:22:27 +03:00
|
|
|
func (c Config) GetHTTPConfig() HTTPServerConfig {
|
|
|
|
return c.HTTPServer
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// GetUpdateInterval user data update interval.
|
2019-09-04 15:22:27 +03:00
|
|
|
func (c Config) GetUpdateInterval() int {
|
|
|
|
return c.UpdateInterval
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// GetHTTPClientConfig returns http client config.
|
2019-12-13 12:13:57 +03:00
|
|
|
func (c Config) GetHTTPClientConfig() *HTTPClientConfig {
|
2019-10-18 13:18:36 +03:00
|
|
|
return c.HTTPClientConfig
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// GetName transport name.
|
2019-09-04 15:22:27 +03:00
|
|
|
func (t Info) GetName() string {
|
|
|
|
return t.Name
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// GetCode transport code.
|
2019-09-04 15:22:27 +03:00
|
|
|
func (t Info) GetCode() string {
|
|
|
|
return t.Code
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// 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-02-09 14:57:14 +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
|
|
|
|
}
|