changeAPIv1/internal/config/config.go
netscrawler 5b11756af6 v0.1
2024-07-04 00:44:15 +03:00

60 lines
1.0 KiB
Go

package config
import (
"flag"
"github.com/ilyakaznacheev/cleanenv"
"os"
"time"
)
type Config struct {
Env string `yaml:"env" env-default:"local"`
TokenTTL time.Duration `yaml:"token_ttl" env-required:"true"`
GRPC GRPCConfig `yaml:"grpc"`
Redis RedisConfig `yaml:"redis"`
}
type GRPCConfig struct {
Port int `yaml:"port"`
Timeout time.Duration `yaml:"timeout"`
}
type RedisConfig struct {
Addr string `yaml:"addr"`
Password string `yaml:"password"`
DB int `yaml:"db"`
}
func MustLoad() *Config {
path := fetchConfigPath()
if path == "" {
panic("config path is empty")
}
if _, err := os.Stat(path); os.IsNotExist(err) {
panic("config file not found" + path)
}
var cfg Config
if err := cleanenv.ReadConfig(path, &cfg); err != nil {
panic("failed to read config" + err.Error())
}
return &cfg
}
func fetchConfigPath() string {
var res string
flag.StringVar(&res, "config", "", "path to config file")
flag.Parse()
if res == "" {
res = os.Getenv("CONFIG_PATH")
}
return res
}