changeAPIv1/internal/config/config.go
netscrawler 3085eb993a Squashed commit of the following:
commit 5b11756af6
Author: netscrawler <mariarthyjamesoo@gmail.com>
Date:   Thu Jul 4 00:44:15 2024 +0300

    v0.1

commit c99f477e35
Author: netscrawler <mariarthyjamesoo@gmail.com>
Date:   Sun Jun 30 20:44:53 2024 +0300

    v0.0.6
2024-07-04 00:45:11 +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
}