56 lines
1.1 KiB
Go
56 lines
1.1 KiB
Go
package config
|
|
|
|
import (
|
|
"flag"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/ilyakaznacheev/cleanenv"
|
|
)
|
|
|
|
type Config struct {
|
|
Env string `yaml:"env" env-required:"true"`
|
|
StoragePath string `yaml:"storage_path" env-required:"true"`
|
|
TokenTTL time.Duration `yaml:"token_ttl" env-default:"1h"`
|
|
GRPC struct {
|
|
Port int `yaml:"port" env-default:"44044"`
|
|
Timeout time.Duration `yaml:"timeout" env-default:"10s"`
|
|
} `yaml:"grpc"`
|
|
}
|
|
|
|
func MustLoad() *Config {
|
|
path := fetchConfigPath()
|
|
if path == "" {
|
|
panic("config path is empty")
|
|
}
|
|
|
|
return MustLoadByPath(path)
|
|
}
|
|
|
|
func MustLoadByPath(configPath string) *Config {
|
|
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
|
panic("config path does not exist: " + configPath)
|
|
}
|
|
|
|
var cfg Config
|
|
if err := cleanenv.ReadConfig(configPath, &cfg); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return &cfg
|
|
}
|
|
|
|
func fetchConfigPath() string {
|
|
var res string
|
|
|
|
// --config="path/to/config.yaml"
|
|
flag.StringVar(&res, "config", "", "path to config file")
|
|
flag.Parse()
|
|
|
|
if res == "" {
|
|
res = os.Getenv("CONFIG_PATH")
|
|
}
|
|
|
|
return res
|
|
}
|