54 lines
1.5 KiB
Go
54 lines
1.5 KiB
Go
// Package config loads and validates the environment configuration of the application.
|
|
package config
|
|
|
|
import "fmt"
|
|
|
|
// Config groups settings by the component that consumes them.
|
|
type Config struct {
|
|
// HTTP configures the listener, request limits and shutdown deadline.
|
|
HTTP *Controller
|
|
// Memory configures the capacity of the process-local store.
|
|
Memory *Memory
|
|
// Logger configures structured process logging.
|
|
Logger *Logger
|
|
}
|
|
|
|
// Default returns independent configuration sections with the documented defaults.
|
|
func Default() *Config {
|
|
return &Config{
|
|
HTTP: defaultController(),
|
|
Memory: defaultMemory(),
|
|
Logger: &Logger{Level: "info"},
|
|
}
|
|
}
|
|
|
|
// New loads environment overrides and rejects invalid settings before startup.
|
|
func New() (*Config, error) {
|
|
cfg := Default()
|
|
if err := cfg.HTTP.loadEnvironment(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := cfg.Memory.loadEnvironment(); err != nil {
|
|
return nil, err
|
|
}
|
|
cfg.Logger.loadEnvironment()
|
|
if err := cfg.Validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
return cfg, nil
|
|
}
|
|
|
|
// Validate verifies the presence and invariants of each configuration section.
|
|
func (cfg *Config) Validate() error {
|
|
if cfg == nil || cfg.HTTP == nil || cfg.Memory == nil || cfg.Logger == nil {
|
|
return fmt.Errorf("HTTP, Memory and Logger configuration sections are required")
|
|
}
|
|
if err := cfg.HTTP.Validate(); err != nil {
|
|
return err
|
|
}
|
|
if err := cfg.Memory.Validate(); err != nil {
|
|
return err
|
|
}
|
|
return cfg.Logger.Validate()
|
|
}
|