config

package
v1.3.3 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package config provides application configuration loaded from environment variables via cleanenv struct tags. Supports env prefixes, default values, and .env file loading through Load/LoadFromEnvFile.

Index

Constants

View Source
const Mask = "xxxxxx"

Variables

This section is empty.

Functions

func LoadFromEnvFileInto

func LoadFromEnvFileInto[T any](path string, target *T) (*T, error)

func LoadInto

func LoadInto[T any](cfg *T) (*T, error)

Types

type APIConfig

type APIConfig struct {
	Host string `env:"HOST" env-default:"0.0.0.0"`
	Port int    `env:"PORT" env-default:"8080"`
}

type AppConfig

type AppConfig struct {
	Name        string `env:"NAME" env-default:"my-app"`
	Version     string `env:"VERSION" env-default:"0.1.0"`
	Environment string `env:"ENVIRONMENT" env-default:"development"`
}

type Config

type Config struct {
	App       AppConfig       `env-prefix:"APP_"`
	API       APIConfig       `env-prefix:"API_"`
	Metrics   MetricsConfig   `env-prefix:"METRICS_"`
	Logging   LoggingConfig   `env-prefix:"LOGGING_"`
	Telemetry TelemetryConfig `env-prefix:"TELEMETRY_"`
	Sentry    SentryConfig    `env-prefix:"SENTRY_"`
	Debug     bool            `env:"DEBUG" env-default:"false"`

	Postgres PostgresConfig `env-prefix:"POSTGRES_"`
}

func Load

func Load() (*Config, error)

func LoadFromEnvFile

func LoadFromEnvFile(path string) (*Config, error)

type LoggingConfig

type LoggingConfig struct {
	Level     string `env:"LEVEL" env-default:"info"`
	AddCaller bool   `env:"ADD_CALLER" env-default:"false"`
}

type MetricsConfig

type MetricsConfig struct {
	Enabled bool   `env:"ENABLED" env-default:"true"`
	Host    string `env:"HOST" env-default:"0.0.0.0"`
	Port    int    `env:"PORT" env-default:"8081"`
	Path    string `env:"PATH" env-default:"/metrics"`
}

type PostgresConfig

type PostgresConfig struct {
	DSN SecretStr `env:"DSN" env-default:""`

	// Hosts is a comma-separated list of host:port pars:
	//
	// Example: "localhost:15432,localhost:15433"
	//
	// If empty then ConnString() uses Postgres default: "localhost:5432"
	Hosts    []string  `env:"HOSTS" env-default:""`
	User     string    `env:"USER" env-default:"postgres"`
	Password SecretStr `env:"PASSWORD" env-default:"postgres"`
	Database string    `env:"DATABASE" env-default:"postgres"`
	// SSLMode
	//
	// Values:
	//   - "disable" - only try a non-SSL connection
	//   - "allow" - first try a non-SSL connection; if that fails, try an SSL connection
	//   - "prefer" - first try an SSL connection; if that fails, try a non-SSL connection
	//   - "require" - only try an SSL connection. If a root CA file is present, verify the certificate in the same way as if verify-ca was specified
	//   - "verify-ca" - only try an SSL connection, and verify that the server certificate is issued by a trusted certificate authority (CA)
	//   - "verify-full" - only try an SSL connection, verify that the server certificate is issued by a trusted CA and that the requested server host name matches that in the certificate
	SSLMode string `env:"SSLMODE" env-default:"prefer"`
	// TargetSessionAttrs used to specify the required state of a server before a connection is established.
	//
	// Useful values:
	//   - "primary" - server must not be in hot standby mode (master)
	//   - "standby" - server must be in hot standby mode (replica)
	//   - "prefer-standby" - first try to find a standby server, but if none of the listed hosts is a standby server, try again in any mode
	//
	// https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-TARGET-SESSION-ATTRS
	TargetSessionAttrs string `env:"TARGET_SESSION_ATTRS" env-default:"primary"`

	MaxConns          int32         `env:"MAX_CONNS" env-default:"10"`
	MinConns          int32         `env:"MIN_CONNS" env-default:"0"`
	MaxConnLifetime   time.Duration `env:"MAX_CONN_LIFETIME" env-default:"1h"`
	MaxConnIdleTime   time.Duration `env:"MAX_CONN_IDLE_TIME" env-default:"30m"`
	HealthCheckPeriod time.Duration `env:"HEALTH_CHECK_PERIOD" env-default:"1m"`

	PingTimeout    time.Duration `env:"PING_TIMEOUT" env-default:"30s"`
	ConnectTimeout time.Duration `env:"CONNECT_TIMEOUT" env-default:"30s"`

	PoolName           string        `env:"POOL_NAME" env-default:""`
	SlowQueryThreshold time.Duration `env:"SLOW_QUERY_THRESHOLD" env-default:"0"`
}

func (*PostgresConfig) ConnString

func (c *PostgresConfig) ConnString() string

type SecretStr

type SecretStr string

SecretStr is a string type that masks its value in String() and MarshalJSON/MarshalText to prevent accidental leakage in logs, error messages, or serialization. Use SecretValue() to access the actual string.

func (SecretStr) MarshalJSON

func (s SecretStr) MarshalJSON() ([]byte, error)

func (SecretStr) MarshalText

func (s SecretStr) MarshalText() ([]byte, error)

func (SecretStr) SecretValue

func (s SecretStr) SecretValue() string

func (SecretStr) String

func (s SecretStr) String() string

type SecretURL

type SecretURL string

SecretURL is a string type that masks the userinfo part (user:password) of a URL in String(), MarshalJSON(), and MarshalText() to prevent accidental leakage in logs, error messages, or serialization. Use SecretValue() to access the actual URL string.

func (SecretURL) MarshalJSON

func (u SecretURL) MarshalJSON() ([]byte, error)

func (SecretURL) MarshalText

func (u SecretURL) MarshalText() ([]byte, error)

func (SecretURL) SecretValue

func (u SecretURL) SecretValue() string

func (SecretURL) String

func (u SecretURL) String() string

type SentryConfig

type SentryConfig struct {
	DSN          SecretURL     `env:"DSN" env-default:""`
	Levels       []string      `env:"LEVELS" env-default:"warn,error"`
	SampleRate   float64       `env:"SAMPLE_RATE" env-default:"1.0"`
	FlushTimeout time.Duration `env:"FLUSH_TIMEOUT" env-default:"5s"`
	// EnableTracing enables OpenTelemetry tracing via Sentry's OTLP exporter.
	// Requires TelemetryConfig.Enabled=true and InitTelemetry called before InitSentry.
	// TracingEndpoint may be empty — traces will be sent to Sentry only.
	EnableTracing bool `env:"ENABLE_TRACING" env-default:"false"`
	// Debug Sentry SDK
	Debug bool `env:"DEBUG" env-default:"false"`
}

type TelemetryConfig

type TelemetryConfig struct {
	Enabled         bool          `env:"ENABLED" env-default:"false"`
	TracingEndpoint string        `env:"TRACING_ENDPOINT" env-default:"localhost:4317"`
	SampleRate      float64       `env:"SAMPLE_RATE" env-default:"1.0"`
	UseTLS          bool          `env:"USE_TLS" env-default:"false"`
	SkipVerifyCA    bool          `env:"SKIP_VERIFY_CA" env-default:"false"`
	CACertPath      string        `env:"CA_CERT_PATH" env-default:""`
	ConnectTimeout  time.Duration `env:"CONNECT_TIMEOUT" env-default:"10s"`
}

Jump to

Keyboard shortcuts

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