Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package config
- import (
- "fmt"
- "gopkg.in/yaml.v2"
- "io/ioutil"
- "log"
- "os"
- )
- type Config struct {
- Server ServerConfig `yaml:"server"`
- Steam SteamConfig `yaml:"steam"`
- Telegram TelegramConfig `yaml:"telegram"`
- Postgres PostgresConfig `yaml:"postgres"`
- Watcher WatcherConfig `yaml:"watcher"`
- }
- type WatcherConfig struct {
- Tick int `yaml:"tick"`
- }
- type ServerConfig struct {
- Host string `yaml:"host"`
- Port int `yaml:"port"`
- Protocol string `yaml:"protocol"`
- Timeout int `yaml:"timeout"`
- }
- type SteamConfig struct {
- Key string `yaml:"key"`
- Host string `yaml:"host"`
- }
- type TelegramConfig struct {
- Key string `yaml:"key"`
- }
- type PostgresConfig struct {
- DBname string `yaml:"dbname"`
- Host string `yaml:"host"`
- Port int `yaml:"port"`
- User string `yaml:"user"`
- Password string `yaml:"password"`
- SSLMode string `yaml:"sslmode"`
- }
- func (pg *PostgresConfig) DSN() string {
- sslmode := "disable"
- if pg.SSLMode != "" {
- sslmode = pg.SSLMode
- }
- return fmt.Sprintf(
- "user=%s password=%s port=%d host=%s dbname=%s sslmode=%s",
- pg.User,
- pg.Password,
- pg.Port,
- pg.Host,
- pg.DBname,
- sslmode,
- )
- }
- func LoadConfig(filepath string) *Config {
- config := &Config{
- Server: ServerConfig{
- Host: "localhost",
- Port: 8080,
- Protocol: "http",
- Timeout: 30,
- },
- }
- if _, err := os.Stat(filepath); os.IsNotExist(err) {
- log.Fatalf("Config file not found! %v", err)
- }
- data, err := ioutil.ReadFile(filepath)
- if err != nil {
- log.Fatalf("Can't read config file %v", err)
- }
- err = yaml.Unmarshal(data, config)
- if err != nil {
- log.Fatalf("YAML parse error: %v", err)
- }
- return config
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement