config

package
v0.16.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 24, 2025 License: MIT Imports: 12 Imported by: 6

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

Constants

This section is empty.

Variables

View Source
var DefaultConfig = &AppConfig{
	Environment: "development",
	Version:     getVersion(),
	Logging:     defaultLoggingConfig,
	HTTP:        defaultHTTPConfig,
	OTEL:        defaultOTELConfig,
	GRPC:        defaultGRPCConfig,
}

DefaultConfig provides a default application configuration.

View Source
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

func AddToContextFor[T any](ctx context.Context, cfg T) context.Context

Add to Ctx for custom type that embeds AppConfig

func FromCtxFor added in v0.16.0

func FromCtxFor[T any](ctx context.Context) (*T, error)

FromCtxFor retrieves custom config that embeds AppConfig from context

func LoadConfig

func LoadConfig(ctx context.Context) (context.Context, error)

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

func MustFromCtxFor[T any](ctx context.Context) *T

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

func FromCtx(ctx context.Context) (*AppConfig, error)

FromCtx retrieves the AppConfig from the context. It handles both direct *AppConfig and custom types that embed *AppConfig.

func MustFromCtx

func MustFromCtx(ctx context.Context) *AppConfig

MustFromCtx retrieves the AppConfig from the context, or panics if not found.

func (*AppConfig) AddToCtx

func (ac *AppConfig) AddToCtx(ctx context.Context) context.Context

AddToCtx adds the AppConfig to the provided context.

func (*AppConfig) GRPCEnabled added in v0.6.2

func (ac *AppConfig) GRPCEnabled() bool

GRPCEnabled returns true if gRPC is enabled in the AppConfig.

func (*AppConfig) HTTPEnabled added in v0.6.2

func (ac *AppConfig) HTTPEnabled() bool

HTTPEnabled returns true if HTTP is enabled in the AppConfig.

func (*AppConfig) OTELEnabled added in v0.6.2

func (ac *AppConfig) OTELEnabled() bool

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

func (*HTTPConfig) Timeouts added in v0.1.3

func (h *HTTPConfig) Timeouts() (*time.Duration, *time.Duration, *time.Duration)

Timeouts returns read timeout, write timeout, and idle timeout, in that order. Returns nil if unset.

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 LogFormat

type LogFormat string
const (
	LogFormatConsole LogFormat = "console"
	LogFormatJSON    LogFormat = "json"
)

type LogOutput

type LogOutput string
const (
	LogOutputStdout LogOutput = "stdout"
	LogOutputStderr LogOutput = "stderr"
)

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"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL