2024-01-15 14:30:13 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ilyakaznacheev/cleanenv"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2024-01-25 14:35:04 +02:00
|
|
|
Env string `yaml:"env" env-required:"true"`
|
|
|
|
HTTPServerConfig `yaml:"http-server"`
|
|
|
|
Postgresql struct {
|
2024-01-18 18:48:27 +02:00
|
|
|
DBName string `yaml:"db_name" env-required:"true"`
|
|
|
|
User string `yaml:"user" env-required:"true"`
|
|
|
|
Password string `yaml:"password" env-required:"true"`
|
|
|
|
Address string `yaml:"address" env-required:"true"`
|
2024-01-15 14:30:13 +02:00
|
|
|
} `yaml:"postgresql"`
|
|
|
|
Redis struct {
|
2024-01-18 18:48:27 +02:00
|
|
|
Password string `yaml:"password" env-required:"true"`
|
|
|
|
Address string `yaml:"address" env-required:"true"`
|
2024-01-15 14:30:13 +02:00
|
|
|
} `yaml:"redis"`
|
|
|
|
Minio struct {
|
2024-01-18 18:48:27 +02:00
|
|
|
User string `yaml:"user" env-required:"true"`
|
|
|
|
Password string `yaml:"password" env-required:"true"`
|
|
|
|
Address string `yaml:"address" env-required:"true"`
|
2024-01-15 14:30:13 +02:00
|
|
|
} `yaml:"minio"`
|
|
|
|
}
|
|
|
|
|
2024-01-25 14:35:04 +02:00
|
|
|
type HTTPServerConfig struct {
|
|
|
|
Address string `yaml:"address" env-required:"true"`
|
|
|
|
Timeout time.Duration `yaml:"timeout" env-default:"4s"`
|
|
|
|
IdleTimeout time.Duration `yaml:"idle_timeout" env-default:"60s"`
|
|
|
|
}
|
|
|
|
|
2024-01-16 20:44:20 +02:00
|
|
|
// MustLoad returns config or panic.
|
2024-01-15 14:30:13 +02:00
|
|
|
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
|
|
|
|
}
|