mg-transport-core/core/config.go

192 lines
4.3 KiB
Go
Raw Normal View History

2019-09-04 15:22:27 +03:00
package core
import (
"io/ioutil"
"path/filepath"
"regexp"
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{"*", "_", "`", "["}
regCommandName = regexp.MustCompile(`^https://?[\da-z.-]+\.(retailcrm\.(ru|pro|es)|ecomlogic\.com|simlachat\.(com|ru))/?$`)
slashRegex = regexp.MustCompile(`/+$`)
)
2019-09-04 15:22:27 +03:00
// ConfigInterface settings data structure
type ConfigInterface interface {
GetVersion() string
GetSentryDSN() string
GetLogLevel() logging.Level
GetDebug() bool
GetHTTPConfig() HTTPServerConfig
GetDBConfig() DatabaseConfig
GetAWSConfig() ConfigAWS
GetTransportInfo() InfoInterface
GetUpdateInterval() int
IsDebug() bool
}
// InfoInterface transport settings data structure
type InfoInterface interface {
GetName() string
GetCode() string
GetLogoPath() string
}
// Config struct
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"`
}
// Info struct
type Info struct {
Name string `yaml:"name"`
Code string `yaml:"code"`
LogoPath string `yaml:"logo_path"`
}
// ConfigAWS struct
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
type DatabaseConfig struct {
Connection string `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"`
}
// HTTPServerConfig struct
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
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
func (c Config) GetSentryDSN() string {
return c.SentryDSN
}
// GetVersion transport version
func (c Config) GetVersion() string {
return c.Version
}
// GetLogLevel log level
func (c Config) GetLogLevel() logging.Level {
return c.LogLevel
}
// GetTransportInfo transport basic data
func (c Config) GetTransportInfo() InfoInterface {
return c.TransportInfo
}
// GetDebug debug flag
func (c Config) GetDebug() bool {
return c.Debug
}
// GetAWSConfig AWS configuration
func (c Config) GetAWSConfig() ConfigAWS {
return c.ConfigAWS
}
// GetDBConfig database configuration
func (c Config) GetDBConfig() DatabaseConfig {
return c.Database
}
// GetHTTPConfig server configuration
func (c Config) GetHTTPConfig() HTTPServerConfig {
return c.HTTPServer
}
// GetUpdateInterval user data update interval
func (c Config) GetUpdateInterval() int {
return c.UpdateInterval
}
// IsDebug debug state
func (c Config) IsDebug() bool {
return c.Debug
}
// GetName transport name
func (t Info) GetName() string {
return t.Name
}
// GetCode transport code
func (t Info) GetCode() string {
return t.Code
}
// GetLogoPath transport logo
func (t Info) GetLogoPath() string {
return t.LogoPath
}