go-app/pkg/config/types.go

97 lines
3.1 KiB
Go

package config
import "time"
// 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",
LogRequests: false,
ReadTimeout: "10s",
WriteTimeout: "10s",
IdleTimeout: "1m",
},
OTEL: &OTELConfig{
Enabled: true,
PrometheusEnabled: true,
PrometheusPath: "/metrics",
StdoutEnabled: false,
MetricIntervalSecs: 30,
},
}
type AppConfig struct {
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,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,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
const (
LogFormatConsole LogFormat = "console"
LogFormatJSON LogFormat = "json"
)
type TimeFormat string
const (
TimeFormatShort TimeFormat = "short"
TimeFormatLong TimeFormat = "long"
TimeFormatUnix TimeFormat = "unix"
TimeFormatRFC3339 TimeFormat = "rfc3339"
TimeFormatOff TimeFormat = "off"
)
type LogOutput string
const (
LogOutputStdout LogOutput = "stdout"
LogOutputStderr LogOutput = "stderr"
)
// HTTP Configuration
type HTTPConfig struct {
Listen string `yaml:"listen,omitempty" env:"APP_HTTP_LISTEN"`
LogRequests bool `yaml:"logRequests" env:"APP_HTTP_LOG_REQUESTS"`
ReadTimeout string `yaml:"readTimeout" env:"APP_HTTP_READ_TIMEOUT"` // Go duration (e.g. 10s)
WriteTimeout string `yaml:"writeTimeout" env:"APP_HTTP_WRITE_TIMEOUT"` // Go duration (e.g. 10s)
IdleTimeout string `yaml:"idleTimeout" env:"APP_HTTP_IDLE_TIMEOUT"` // Go duration (e.g. 10s)
rT *time.Duration
wT *time.Duration
iT *time.Duration
}
// OTEL Configuration
type OTELConfig struct {
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"`
}