77 lines
2.5 KiB
Go
77 lines
2.5 KiB
Go
package config
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/ilyakaznacheev/cleanenv"
|
|
)
|
|
|
|
type Config struct {
|
|
Enviroment string `yaml:"enviroment" env:"ENVIROMENT" env-default:"local"`
|
|
Server Server `yaml:"server"`
|
|
Database Database `yaml:"database"`
|
|
SteamRequester SteamRequester `yaml:"steam_requester"`
|
|
SteamWorker SteamWorker `yaml:"steam_worker"`
|
|
}
|
|
|
|
type (
|
|
Server struct {
|
|
Host string `yaml:"host" env-default:"localhost"`
|
|
Port string `yaml:"port" env-default:"8080"`
|
|
IdleTimeout time.Duration `yaml:"idle_timeout" env-default:"30s"`
|
|
WriteTimeout time.Duration `yaml:"write_timeout" env-default:"1s"`
|
|
ReadTimeout time.Duration `yaml:"read_timeout" env-default:"1s"`
|
|
Origins []string `yaml:"origins" env-separator:"," env-default:"http://localhost:3000,"`
|
|
}
|
|
Database struct {
|
|
Host string `yaml:"host" env:"DATABASE_HOST" env-description:"Database host" env-default:"localhost"`
|
|
Port string `yaml:"port" env:"DATABASE_PORT" env-description:"Database port" env-default:"5432"`
|
|
Sslmode string `yaml:"sslmode" env-description:"Database ssl mode" env-default:"disable"`
|
|
Name string `yaml:"name" env-description:"Database name"`
|
|
User string `yaml:"user" env-description:"Database user"`
|
|
Password string `yaml:"password" env-description:"Database user password"`
|
|
|
|
MaxConnAttempts int `yaml:"max_conn_attempt" env-default:"5"`
|
|
ConnTimeout time.Duration `yaml:"conn_timeout" env-default:"10s"`
|
|
|
|
MigrationPath string `yaml:"migration_path" env:"DATABASE_MIGRATION_PATH" env-description:"Migration path"`
|
|
}
|
|
SteamRequester struct {
|
|
RequestPerTime time.Duration `yaml:"request_per_time" env-description:"request limit per time" env-default:"5s"`
|
|
CountPerTime int `yaml:"count_per_time" env-description:"count limit per time" env-default:"1"`
|
|
}
|
|
SteamWorker struct {
|
|
RequestPerTime time.Duration `yaml:"request_per_time" env-description:"update prices time" env-default:"5m"`
|
|
}
|
|
)
|
|
|
|
func MustLoadYaml(configPath string) *Config {
|
|
if configPath == "" {
|
|
log.Fatal("config path is not set")
|
|
}
|
|
|
|
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
|
log.Fatalf("config path doesn't exist: %s", configPath)
|
|
}
|
|
|
|
var cfg Config
|
|
|
|
if err := cleanenv.ReadConfig(configPath, &cfg); err != nil {
|
|
log.Fatalf("cannot read config: %s", err)
|
|
}
|
|
|
|
return &cfg
|
|
}
|
|
|
|
func MustLoadEnv() *Config {
|
|
var cfg Config
|
|
|
|
if err := cleanenv.ReadEnv(&cfg); err != nil {
|
|
log.Fatalf("cannot read config: %s", err)
|
|
}
|
|
|
|
return &cfg
|
|
}
|