Fix forced overrides

This commit is contained in:
2025-01-05 13:09:59 -05:00
parent 208e31c3d4
commit 4a99f06987
4 changed files with 74 additions and 39 deletions

View File

@ -7,7 +7,7 @@ import (
"os"
"runtime/debug"
"github.com/caarlos0/env/v9"
"github.com/caarlos0/env/v11"
"gopkg.in/yaml.v3"
)
@ -39,7 +39,7 @@ func LoadConfig(ctx context.Context) (context.Context, error) {
}
func loadConfig(configPath string) (*AppConfig, error) {
cfg := newAppConfig()
cfg := *DefaultConfig
if configPath != "" {
file, err := os.Open(configPath)
@ -49,16 +49,16 @@ func loadConfig(configPath string) (*AppConfig, error) {
defer file.Close()
decoder := yaml.NewDecoder(file)
if err := decoder.Decode(cfg); err != nil {
if err := decoder.Decode(&cfg); err != nil {
return nil, fmt.Errorf("could not decode config file: %w", err)
}
}
if err := env.Parse(cfg); err != nil {
if err := env.Parse(&cfg); err != nil {
return nil, fmt.Errorf("could not parse environment variables: %w", err)
}
return cfg, nil
return &cfg, nil
}
func getVersion() string {

View File

@ -1,33 +1,47 @@
package config
func newAppConfig() *AppConfig {
return &AppConfig{
Version: getVersion(),
Logging: &LogConfig{},
HTTP: &HTTPConfig{},
OTEL: &OTELConfig{},
}
// Default Settings
var DefaultConfig = &AppConfig{
Environment: "development",
Version: getVersion(),
Logging: &LogConfig{
Enabled: true,
Level: "info",
Format: LogFormatJSON,
Output: "stderr",
TimeFormat: TimeFormatLong,
},
HTTP: &HTTPConfig{
Listen: "127.0.0.1:8080",
},
OTEL: &OTELConfig{
Enabled: true,
PrometheusEnabled: true,
PrometheusPath: "/metrics",
StdoutEnabled: false,
MetricIntervalSecs: 30,
},
}
type AppConfig struct {
Name string `yaml:"name" env:"APP_NAME" envDefault:"go-http-server-with-otel"`
Environment string `yaml:"environment" env:"APP_ENVIRONMENT" envDefault:"development"`
Name string `yaml:"name,omitempty" env:"APP_NAME"`
Environment string `yaml:"environment,omitempty" env:"APP_ENVIRONMENT"`
// This should either be set by ldflags, such as with
// go build -ldflags "-X gitea.libretechconsulting.com/rmcguire/go-app/pkg/config.Version=$(VERSION)"
// or allow this to use build meta. Will default to (devel)
Version string `yaml:"version" env:"APP_VERSION"`
Logging *LogConfig `yaml:"logging"`
HTTP *HTTPConfig `yaml:"http"`
OTEL *OTELConfig `yaml:"otel"`
Version string `yaml:"version,omitempty" env:"APP_VERSION"`
Logging *LogConfig `yaml:"logging,omitempty"`
HTTP *HTTPConfig `yaml:"http,omitempty"`
OTEL *OTELConfig `yaml:"otel,omitempty"`
}
// Logging Configuration
type LogConfig struct {
Enabled bool `yaml:"enabled" env:"APP_LOG_ENABLED" envDefault:"true"`
Level string `yaml:"level" env:"APP_LOG_LEVEL" envDefault:"info"`
Format LogFormat `yaml:"format" env:"APP_LOG_FORMAT" envDefault:"json"`
Output LogOutput `yaml:"output" env:"APP_LOG_OUTPUT" envDefault:"stderr"`
TimeFormat TimeFormat `yaml:"timeFormat" env:"APP_LOG_TIME_FORMAT" envDefault:"short"`
Enabled bool `yaml:"enabled,omitempty" env:"APP_LOG_ENABLED"`
Level string `yaml:"level,omitempty" env:"APP_LOG_LEVEL"`
Format LogFormat `yaml:"format,omitempty" env:"APP_LOG_FORMAT"`
Output LogOutput `yaml:"output,omitempty" env:"APP_LOG_OUTPUT"`
TimeFormat TimeFormat `yaml:"timeFormat,omitempty" env:"APP_LOG_TIME_FORMAT"`
}
type LogFormat string
@ -56,15 +70,15 @@ const (
// HTTP Configuration
type HTTPConfig struct {
Listen string `yaml:"listen" env:"APP_HTTP_LISTEN" envDefault:"127.0.0.1:8080"`
RequestTimeout int `yaml:"requestTimeout" env:"APP_HTTP_REQUEST_TIMEOUT" envDefault:"30"`
Listen string `yaml:"listen,omitempty" env:"APP_HTTP_LISTEN"`
RequestTimeout int `yaml:"requestTimeout,omitempty" env:"APP_HTTP_REQUEST_TIMEOUT"`
}
// OTEL Configuration
type OTELConfig struct {
Enabled bool `yaml:"enabled" env:"APP_OTEL_ENABLED" envDefault:"true"`
PrometheusEnabled bool `yaml:"prometheusEnabled" env:"APP_OTEL_PROMETHEUS_ENABLED" envDefault:"true"`
PrometheusPath string `yaml:"prometheusPath" env:"APP_OTEL_PROMETHEUS_PATH" envDefault:"/metrics"`
StdoutEnabled bool `yaml:"stdoutEnabled" env:"APP_OTEL_STDOUT_ENABLED" envDefault:"false"`
MetricIntervalSecs int `yaml:"metricIntervalSecs" env:"APP_OTEL_METRIC_INTERVAL_SECS" envDefault:"15"`
Enabled bool `yaml:"enabled,omitempty" env:"APP_OTEL_ENABLED"`
PrometheusEnabled bool `yaml:"prometheusEnabled,omitempty" env:"APP_OTEL_PROMETHEUS_ENABLED"`
PrometheusPath string `yaml:"prometheusPath,omitempty" env:"APP_OTEL_PROMETHEUS_PATH"`
StdoutEnabled bool `yaml:"stdoutEnabled,omitempty" env:"APP_OTEL_STDOUT_ENABLED"`
MetricIntervalSecs int `yaml:"metricIntervalSecs,omitempty" env:"APP_OTEL_METRIC_INTERVAL_SECS"`
}