mirror of
https://github.com/netscrawler/changeAPI.git
synced 2026-09-05 08:29:36 +00:00
v0.0.6
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user