73 lines
1.9 KiB
Go
73 lines
1.9 KiB
Go
package config
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/ilyakaznacheev/cleanenv"
|
|
)
|
|
|
|
type Config struct {
|
|
Server ServerConfig `yaml:"server"`
|
|
PoW PoWConfig `yaml:"pow"`
|
|
Quotes QuotesConfig `yaml:"quotes"`
|
|
Metrics MetricsConfig `yaml:"metrics"`
|
|
Logging LoggingConfig `yaml:"logging"`
|
|
}
|
|
|
|
type ServerConfig struct {
|
|
Address string `yaml:"address" env:"SERVER_ADDRESS" env-default:":8080"`
|
|
Timeouts TimeoutConfig `yaml:"timeouts"`
|
|
}
|
|
|
|
type TimeoutConfig struct {
|
|
Read time.Duration `yaml:"read" env:"SERVER_READ_TIMEOUT" env-default:"5s"`
|
|
Write time.Duration `yaml:"write" env:"SERVER_WRITE_TIMEOUT" env-default:"5s"`
|
|
Connection time.Duration `yaml:"connection" env:"SERVER_CONNECTION_TIMEOUT" env-default:"15s"`
|
|
}
|
|
|
|
type PoWConfig struct {
|
|
Difficulty int `yaml:"difficulty" env:"POW_DIFFICULTY" env-default:"4"`
|
|
MaxDifficulty int `yaml:"max_difficulty" env:"POW_MAX_DIFFICULTY" env-default:"10"`
|
|
TTL time.Duration `yaml:"ttl" env:"POW_TTL" env-default:"5m"`
|
|
HMACSecret string `yaml:"hmac_secret" env:"POW_HMAC_SECRET" env-default:"development-secret-change-in-production"`
|
|
}
|
|
|
|
type QuotesConfig struct {
|
|
Timeout time.Duration `yaml:"timeout" env:"QUOTES_TIMEOUT" env-default:"10s"`
|
|
}
|
|
|
|
type MetricsConfig struct {
|
|
Address string `yaml:"address" env:"METRICS_ADDRESS" env-default:":8081"`
|
|
}
|
|
|
|
type LoggingConfig struct {
|
|
Level string `yaml:"level" env:"LOG_LEVEL" env-default:"info"`
|
|
Format string `yaml:"format" env:"LOG_FORMAT" env-default:"text"`
|
|
}
|
|
|
|
func Load(configPath string) (*Config, error) {
|
|
cfg := &Config{}
|
|
|
|
if configPath != "" {
|
|
err := cleanenv.ReadConfig(configPath, cfg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
} else {
|
|
err := cleanenv.ReadEnv(cfg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return cfg, nil
|
|
}
|
|
|
|
func (c *Config) ToServerConfig() *ServerConfig {
|
|
return &c.Server
|
|
}
|
|
|
|
func (c *Config) ToPoWConfig() *PoWConfig {
|
|
return &c.PoW
|
|
}
|