69 lines
2.2 KiB
Go
69 lines
2.2 KiB
Go
package config
|
|
|
|
func newAppConfig() *AppConfig {
|
|
return &AppConfig{
|
|
Version: getVersion(),
|
|
Logging: &LogConfig{},
|
|
HTTP: &HTTPConfig{},
|
|
OTEL: &OTELConfig{},
|
|
}
|
|
}
|
|
|
|
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"`
|
|
// This should either be set by ldflags, such as with
|
|
// go build -ldflags "-X gitea.libretechconsulting.com/rmcguire/go-http-server-with-otel/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"`
|
|
}
|
|
|
|
// 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"`
|
|
}
|
|
|
|
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" env:"APP_HTTP_LISTEN" envDefault:"127.0.0.1:8080"`
|
|
RequestTimeout int `yaml:"request_timeout" env:"APP_HTTP_REQUEST_TIMEOUT" envDefault:"30"`
|
|
}
|
|
|
|
// OTEL Configuration
|
|
type OTELConfig struct {
|
|
Enabled bool `yaml:"enabled" env:"APP_OTEL_ENABLED" envDefault:"true"`
|
|
PrometheusEnabled bool `yaml:"prometheus_enabled" env:"APP_OTEL_PROMETHEUS_ENABLED" envDefault:"true"`
|
|
PrometheusPath string `yaml:"prometheus_path" env:"APP_OTEL_PROMETHEUS_PATH" envDefault:"/metrics"`
|
|
}
|