Documentation
¶
Overview ¶
Package config provides types and methods for managing app config. A go-app configuration can be extended by any type that embeds AppConfig, and has helpers to generate json schema merged with the custom type's fields
Index ¶
- Variables
- func AddToContextFor[T any](ctx context.Context, cfg T) context.Context
- func FromCtxFor[T any](ctx context.Context) (*T, error)
- func LoadConfig(ctx context.Context) (context.Context, error)
- func MustFromCtxFor[T any](ctx context.Context) *T
- type AppConfig
- type GRPCConfig
- type HTTPConfig
- type LogConfig
- type LogFormat
- type LogOutput
- type OTELConfig
- type TimeFormat
Constants ¶
This section is empty.
Variables ¶
var DefaultConfig = &AppConfig{
Environment: "development",
Version: getVersion(),
Logging: defaultLoggingConfig,
HTTP: defaultHTTPConfig,
OTEL: defaultOTELConfig,
GRPC: defaultGRPCConfig,
}
DefaultConfig provides a default application configuration.
var Version = "(devel)"
Version is to be set by ldflags in go build command or retrieved from build meta below.
Functions ¶
func AddToContextFor ¶ added in v0.16.0
Add to Ctx for custom type that embeds AppConfig
func FromCtxFor ¶ added in v0.16.0
FromCtxFor retrieves custom config that embeds AppConfig from context
func LoadConfig ¶
LoadConfig will try to load from config if -config is provided as a file, and will apply any environment overrides on-top of configuration defaults. Config is stored in returned context, and can be retrieved using config.FromCtx(ctx).
func MustFromCtxFor ¶ added in v0.16.0
MustFromCtxFor retrieves custom config that embeds AppConfig from context, or panics if not found.
Types ¶
type AppConfig ¶
type AppConfig struct {
Name string `yaml:"name,omitempty" env:"APP_NAME" json:"name,omitempty"`
Environment string `yaml:"environment,omitempty" env:"APP_ENVIRONMENT" json:"environment,omitempty"`
// 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" json:"version,omitempty"`
Logging *LogConfig `yaml:"logging,omitempty" json:"logging,omitempty"`
HTTP *HTTPConfig `yaml:"http,omitempty" json:"http,omitempty"`
OTEL *OTELConfig `yaml:"otel,omitempty" json:"otel,omitempty"`
GRPC *GRPCConfig `yaml:"grpc,omitempty" json:"grpc,omitempty"`
}
func FromCtx ¶
FromCtx retrieves the AppConfig from the context. It handles both direct *AppConfig and custom types that embed *AppConfig.
func MustFromCtx ¶
MustFromCtx retrieves the AppConfig from the context, or panics if not found.
func (*AppConfig) GRPCEnabled ¶ added in v0.6.2
GRPCEnabled returns true if gRPC is enabled in the AppConfig.
func (*AppConfig) HTTPEnabled ¶ added in v0.6.2
HTTPEnabled returns true if HTTP is enabled in the AppConfig.
func (*AppConfig) OTELEnabled ¶ added in v0.6.2
OTELEnabled returns true if OpenTelemetry is enabled in the AppConfig.
type GRPCConfig ¶ added in v0.6.0
type GRPCConfig struct {
Enabled bool `yaml:"enabled" env:"APP_GRPC_ENABLED" json:"enabled,omitempty"`
Listen string `yaml:"listen" env:"APP_GRPC_LISTEN" json:"listen,omitempty"`
LogRequests bool `yaml:"logRequests" env:"APP_GRPC_LOG_REQUESTS" json:"logRequests,omitempty"`
EnableProtovalidate bool `` /* 204-byte string literal not displayed */
EnableReflection bool `yaml:"enableReflection" env:"APP_GRPC_ENABLE_REFLECTION" json:"enableReflection,omitempty"`
EnableInstrumentation bool `yaml:"enableInstrumentation" env:"APP_GRPC_ENABLE_INSTRUMENTATION" json:"enableInstrumentation,omitempty"` // requires OTEL
EnableGRPCGateway bool `yaml:"enableGRPCGateway" json:"enableGRPCGateway,omitempty" env:"APP_GRPC_ENABLE_GATEWAY" default:"true"`
GRPCGatewayPath string `yaml:"grpcGatewayPath" json:"grpcGatewayPath,omitempty" env:"APP_GRPC_GATEWAY_PATH" default:"/grpc-api"`
GRPCGatewayPathStrip bool `yaml:"grpcGatewayPathStrip" json:"grpcGatewayPathStrip,omitempty" env:"APP_GRPC_GATEWAY_PATH_STRIP" default:"false"`
}
type HTTPConfig ¶
type HTTPConfig struct {
Enabled bool `yaml:"enabled" env:"APP_HTTP_ENABLED" json:"enabled,omitempty"`
Listen string `yaml:"listen,omitempty" env:"APP_HTTP_LISTEN" json:"listen,omitempty"`
LogRequests bool `yaml:"logRequests" env:"APP_HTTP_LOG_REQUESTS" json:"logRequests,omitempty"`
LogExcludePathRegexps []string `yaml:"logExcludePathRegexps" env:"APP_HTTP_LOG_EXCLUDE_PATH_REGEXPS" json:"logExcludePathRegexps,omitempty"`
ReadTimeout string `yaml:"readTimeout" env:"APP_HTTP_READ_TIMEOUT" json:"readTimeout,omitempty"` // Go duration (e.g. 10s)
WriteTimeout string `yaml:"writeTimeout" env:"APP_HTTP_WRITE_TIMEOUT" json:"writeTimeout,omitempty"` // Go duration (e.g. 10s)
IdleTimeout string `yaml:"idleTimeout" env:"APP_HTTP_IDLE_TIMEOUT" json:"idleTimeout,omitempty"` // Go duration (e.g. 10s)
// contains filtered or unexported fields
}
HTTPConfig provides HTTP server Configuration
func (*HTTPConfig) GetExcludeRegexps ¶ added in v0.12.0
func (h *HTTPConfig) GetExcludeRegexps() []*regexp.Regexp
type LogConfig ¶
type LogConfig struct {
Enabled bool `yaml:"enabled,omitempty" env:"APP_LOG_ENABLED" json:"enabled,omitempty"`
Level string `yaml:"level,omitempty" env:"APP_LOG_LEVEL" json:"level,omitempty"`
Format LogFormat `yaml:"format,omitempty" env:"APP_LOG_FORMAT" json:"format,omitempty"`
Output LogOutput `yaml:"output,omitempty" env:"APP_LOG_OUTPUT" json:"output,omitempty"`
TimeFormat TimeFormat `yaml:"timeFormat,omitempty" env:"APP_LOG_TIME_FORMAT" json:"timeFormat,omitempty"`
}
LogConfig provides Logging Configuration
type OTELConfig ¶
type OTELConfig struct {
Enabled bool `yaml:"enabled,omitempty" env:"APP_OTEL_ENABLED" json:"enabled,omitempty"`
PrometheusEnabled bool `yaml:"prometheusEnabled,omitempty" env:"APP_OTEL_PROMETHEUS_ENABLED" json:"prometheusEnabled,omitempty"`
PrometheusPath string `yaml:"prometheusPath,omitempty" env:"APP_OTEL_PROMETHEUS_PATH" json:"prometheusPath,omitempty"`
StdoutEnabled bool `yaml:"stdoutEnabled,omitempty" env:"APP_OTEL_STDOUT_ENABLED" json:"stdoutEnabled,omitempty"`
MetricIntervalSecs int `yaml:"metricIntervalSecs,omitempty" env:"APP_OTEL_METRIC_INTERVAL_SECS" json:"metricIntervalSecs,omitempty"`
}
OtelConfig provides OTEL Configuration
type TimeFormat ¶
type TimeFormat string
const ( TimeFormatShort TimeFormat = "short" TimeFormatLong TimeFormat = "long" TimeFormatUnix TimeFormat = "unix" TimeFormatRFC3339 TimeFormat = "rfc3339" TimeFormatOff TimeFormat = "off" )