config

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PostgreSQL = "postgresql"
	Oracle     = "oracle"
)

Database type constants

View Source
const (
	EnvDevelopment = "development"
	EnvStaging     = "staging"
	EnvProduction  = "production"
)

Environment constants

Variables

This section is empty.

Functions

func IsDatabaseConfigured added in v0.4.1

func IsDatabaseConfigured(cfg *DatabaseConfig) bool

IsDatabaseConfigured determines if database is intentionally configured. This mirrors the logic used in app.isDatabaseEnabled() for consistency.

func Validate

func Validate(cfg *Config) error

Types

type AppConfig

type AppConfig struct {
	Name      string `koanf:"name"`
	Version   string `koanf:"version"`
	Env       string `koanf:"env"`
	Debug     bool   `koanf:"debug"`
	RateLimit int    `koanf:"rate_limit"`
	Namespace string `koanf:"namespace"`
}

type Config

type Config struct {
	App       AppConfig       `koanf:"app"`
	Server    ServerConfig    `koanf:"server"`
	Database  DatabaseConfig  `koanf:"database"`
	Log       LogConfig       `koanf:"log"`
	Messaging MessagingConfig `koanf:"messaging"`
	// contains filtered or unexported fields
}

func Load

func Load() (*Config, error)

Load loads configuration from multiple sources with priority: 1. Environment variables (highest priority) 2. YAML configuration files 3. Default values (lowest priority)

func (*Config) All added in v0.4.0

func (c *Config) All() map[string]any

All returns all configuration as a flattened map.

func (*Config) Custom added in v0.4.0

func (c *Config) Custom() map[string]any

Custom returns the values under the `custom` namespace.

func (*Config) Exists added in v0.4.0

func (c *Config) Exists(key string) bool

Exists checks if a configuration key exists.

func (*Config) GetBool added in v0.4.0

func (c *Config) GetBool(key string, defaultVal ...bool) bool

GetBool retrieves a bool value from the configuration or the provided default.

func (*Config) GetFloat64 added in v0.4.0

func (c *Config) GetFloat64(key string, defaultVal ...float64) float64

GetFloat64 retrieves a float64 value from the configuration or the provided default.

func (*Config) GetInt added in v0.4.0

func (c *Config) GetInt(key string, defaultVal ...int) int

GetInt retrieves an int value from the configuration or the provided default.

func (*Config) GetInt64 added in v0.4.0

func (c *Config) GetInt64(key string, defaultVal ...int64) int64

GetInt64 retrieves an int64 value from the configuration or the provided default.

func (*Config) GetRequiredBool added in v0.4.0

func (c *Config) GetRequiredBool(key string) (bool, error)

GetRequiredBool retrieves a required bool value from the configuration.

func (*Config) GetRequiredFloat64 added in v0.4.0

func (c *Config) GetRequiredFloat64(key string) (float64, error)

GetRequiredFloat64 retrieves a required float64 value from the configuration.

func (*Config) GetRequiredInt added in v0.4.0

func (c *Config) GetRequiredInt(key string) (int, error)

GetRequiredInt retrieves a required int value from the configuration.

func (*Config) GetRequiredInt64 added in v0.4.0

func (c *Config) GetRequiredInt64(key string) (int64, error)

GetRequiredInt64 retrieves a required int64 value from the configuration.

func (*Config) GetRequiredString added in v0.4.0

func (c *Config) GetRequiredString(key string) (string, error)

GetRequiredString retrieves a required string value from the configuration.

func (*Config) GetString added in v0.4.0

func (c *Config) GetString(key string, defaultVal ...string) string

GetString retrieves a string value from the configuration or the provided default.

func (*Config) InjectInto added in v0.6.0

func (c *Config) InjectInto(target any) error

InjectInto populates a struct with configuration values based on struct tags. It supports the following struct tags:

  • `config:"key.path"` - specifies the configuration key to use
  • `required:"true"` - marks the field as required (default: false)
  • `default:"value"` - provides a default value if the config key is missing

Supported field types: string, int, int64, float64, bool, time.Duration

func (*Config) Unmarshal added in v0.4.0

func (c *Config) Unmarshal(key string, out any) error

Unmarshal unmarshals a configuration section into the provided struct.

type DatabaseConfig

type DatabaseConfig struct {
	Type               string        `koanf:"type"` // "postgresql" or "oracle"
	Host               string        `koanf:"host"`
	Port               int           `koanf:"port"`
	Database           string        `koanf:"database"`
	Username           string        `koanf:"username"`
	Password           string        `koanf:"password"`
	SSLMode            string        `koanf:"ssl_mode"`
	MaxConns           int32         `koanf:"max_conns"`
	MaxIdleConns       int32         `koanf:"max_idle_conns"`
	ConnMaxLifetime    time.Duration `koanf:"conn_max_lifetime"`
	ConnMaxIdleTime    time.Duration `koanf:"conn_max_idle_time"`
	SlowQueryThreshold time.Duration `koanf:"slow_query_threshold"`
	MaxQueryLength     int           `koanf:"max_query_length"`
	LogQueryParameters bool          `koanf:"log_query_parameters"`

	// Oracle-specific settings
	ServiceName string `koanf:"service_name"` // Oracle service name
	SID         string `koanf:"sid"`          // Oracle SID

	// Connection string override (if needed)
	ConnectionString string `koanf:"connection_string"`
}

type LogConfig

type LogConfig struct {
	Level  string `koanf:"level"`
	Pretty bool   `koanf:"pretty"`
}

type MessagingConfig

type MessagingConfig struct {
	BrokerURL   string            `koanf:"broker_url"`
	Exchange    string            `koanf:"exchange"`
	RoutingKey  string            `koanf:"routing_key"`
	VirtualHost string            `koanf:"virtual_host"`
	Headers     map[string]string `koanf:"headers"`
}

type ServerConfig

type ServerConfig struct {
	Host              string        `koanf:"host"`
	Port              int           `koanf:"port"`
	ReadTimeout       time.Duration `koanf:"read_timeout"`
	WriteTimeout      time.Duration `koanf:"write_timeout"`
	MiddlewareTimeout time.Duration `koanf:"middleware_timeout"`
	ShutdownTimeout   time.Duration `koanf:"shutdown_timeout"`
	BasePath          string        `koanf:"base_path"`    // Base path for all routes, should start with "/"
	HealthRoute       string        `koanf:"health_route"` // e.g. "/health"
	ReadyRoute        string        `koanf:"ready_route"`  // e.g. "/ready"
}

Jump to

Keyboard shortcuts

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