67 lines
1.5 KiB
Go
67 lines
1.5 KiB
Go
|
package config
|
||
|
|
||
|
import (
|
||
|
"flag"
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"time"
|
||
|
|
||
|
"github.com/ilyakaznacheev/cleanenv"
|
||
|
)
|
||
|
|
||
|
type Config struct {
|
||
|
Env string `yaml:"env" env-required:"true"`
|
||
|
HTTPServer struct {
|
||
|
Address string `yaml:"address"`
|
||
|
Timeout time.Duration `yaml:"timeout"`
|
||
|
IdleTimeout time.Duration `yaml:"idle_timeout"`
|
||
|
} `yaml:"http-server"`
|
||
|
Postgresql struct {
|
||
|
DBName string `yaml:"db_name"`
|
||
|
User string `yaml:"user"`
|
||
|
Password string `yaml:"password"`
|
||
|
Address string `yaml:"address"`
|
||
|
} `yaml:"postgresql"`
|
||
|
Redis struct {
|
||
|
User string `yaml:"user"`
|
||
|
Password string `yaml:"password"`
|
||
|
Address string `yaml:"address"`
|
||
|
} `yaml:"redis"`
|
||
|
Minio struct {
|
||
|
User string `yaml:"user"`
|
||
|
Password string `yaml:"password"`
|
||
|
Address string `yaml:"address"`
|
||
|
} `yaml:"minio"`
|
||
|
}
|
||
|
|
||
|
func MustLoad() *Config {
|
||
|
configPath := fetchConfigPath()
|
||
|
if configPath == "" {
|
||
|
panic("CONFIG_PATH is not set")
|
||
|
}
|
||
|
// check if file exists
|
||
|
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
||
|
panic(fmt.Sprintf("config file does not exist: %s", configPath))
|
||
|
}
|
||
|
|
||
|
var cfg Config
|
||
|
// read config
|
||
|
if err := cleanenv.ReadConfig(configPath, &cfg); err != nil {
|
||
|
panic(fmt.Errorf("cannot read config: %w", err))
|
||
|
}
|
||
|
|
||
|
return &cfg
|
||
|
}
|
||
|
|
||
|
// fetchConfigPath returns config path from env or flag.
|
||
|
func fetchConfigPath() string {
|
||
|
var res string = os.Getenv("CONFIG_PATH")
|
||
|
|
||
|
if res == "" {
|
||
|
flag.StringVar(&res, "config", "", "path to config file")
|
||
|
flag.Parse()
|
||
|
}
|
||
|
|
||
|
return res
|
||
|
}
|