github.com/retailcrm/mg-bot-helper@v0.0.0-20201229112329-a17255681a84/src/config.go (about)

     1  package main
     2  
     3  import (
     4  	"io/ioutil"
     5  	"path/filepath"
     6  
     7  	"github.com/op/go-logging"
     8  	"gopkg.in/yaml.v2"
     9  )
    10  
    11  // BotConfig struct
    12  type BotConfig struct {
    13  	Version    string           `yaml:"version"`
    14  	LogLevel   logging.Level    `yaml:"log_level"`
    15  	Database   DatabaseConfig   `yaml:"database"`
    16  	SentryDSN  string           `yaml:"sentry_dsn"`
    17  	HTTPServer HTTPServerConfig `yaml:"http_server"`
    18  	Debug      bool             `yaml:"debug"`
    19  	BotInfo    BotInfo          `yaml:"bot_info"`
    20  }
    21  
    22  type BotInfo struct {
    23  	Name     string `yaml:"name"`
    24  	Code     string `yaml:"code"`
    25  	LogoPath string `yaml:"logo_path"`
    26  }
    27  
    28  // DatabaseConfig struct
    29  type DatabaseConfig struct {
    30  	Connection         string `yaml:"connection"`
    31  	Logging            bool   `yaml:"logging"`
    32  	TablePrefix        string `yaml:"table_prefix"`
    33  	MaxOpenConnections int    `yaml:"max_open_connections"`
    34  	MaxIdleConnections int    `yaml:"max_idle_connections"`
    35  	ConnectionLifetime int    `yaml:"connection_lifetime"`
    36  }
    37  
    38  // HTTPServerConfig struct
    39  type HTTPServerConfig struct {
    40  	Host   string `yaml:"host"`
    41  	Listen string `yaml:"listen"`
    42  }
    43  
    44  // LoadConfig read configuration file
    45  func LoadConfig(path string) *BotConfig {
    46  	var err error
    47  
    48  	path, err = filepath.Abs(path)
    49  	if err != nil {
    50  		panic(err)
    51  	}
    52  
    53  	source, err := ioutil.ReadFile(path)
    54  	if err != nil {
    55  		panic(err)
    56  	}
    57  
    58  	var c BotConfig
    59  	if err = yaml.Unmarshal(source, &c); err != nil {
    60  		panic(err)
    61  	}
    62  
    63  	return &c
    64  }