mirror of
https://github.com/netscrawler/changeAPI.git
synced 2025-05-06 15:29:53 +00:00
53 lines
907 B
Go
53 lines
907 B
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"`
|
|
}
|
|
|
|
type GRPCConfig struct {
|
|
Port int `yaml:"port"`
|
|
Timeout time.Duration `yaml:"timeout"`
|
|
}
|
|
|
|
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
|
|
}
|