otter-cage/config/config.go
2024-12-10 15:54:22 +01:00

30 lines
846 B
Go

package config
import (
"fmt"
"github.com/caarlos0/env/v10"
"github.com/go-playground/validator/v10"
)
type OtterCage struct {
LogLevel string `validate:"required,eq=FATAL|eq=ERROR|eq=WARN|eq=INFO|eq=DEBUG|eq=TRACE" env:"LOG_LEVEL" envDefault:"INFO" json:"log_level"`
LogFormat string `validate:"required,eq=PLAIN|eq=JSON" env:"LOG_FORMAT" envDefault:"PLAIN" json:"log_format"`
Debug bool `env:"DEBUG" envDefault:"false"`
}
// LoadConfig loads the configuration from environment variables and validates it.
func LoadConfig[T any](cfg T) (T, error) {
if err := env.Parse(&cfg); err != nil {
return cfg, fmt.Errorf("config: error parsing configuration: %w", err)
}
validate := validator.New()
if err := validate.Struct(cfg); err != nil {
return cfg, fmt.Errorf("config: validation error: %w", err)
}
return cfg, nil
}