Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetServerPort ¶
GetServerPort returns the port for the main service. Priority is given to the port set from the environment variable. Then it checks the default value.
func ParseConfigDir ¶
Types ¶
type AppConfig ¶
type AppConfig struct { // App settings AppName string `mapstructure:"APP_NAME"` AppEnv AppEnvironment `mapstructure:"APP_ENV"` AppLogPath string `mapstructure:"APP_LOG_PATH"` AppLogLevel string `mapstructure:"APP_LOG_LEVEL"` // Databases Database PostgresConfig `mapstructure:"DATABASE"` MySQL *MySQLConfig `mapstructure:"MYSQL"` Redis RedisConfig `mapstructure:"REDIS"` MongoDB *MongoDBConfig `mapstructure:"MONGODB"` // Messaging Telegram TelegramConfig `mapstructure:"TELEGRAM"` Email EmailConfig `mapstructure:"EMAIL"` // Server settings ServerPort int `mapstructure:"SERVER_PORT"` ServerHost string `mapstructure:"SERVER_HOST"` ServerTimeout int `mapstructure:"SERVER_TIMEOUT"` // JWT settings JWTSecretKey string `mapstructure:"JWT_SECRET_KEY"` JWTExpiration time.Duration `mapstructure:"JWT_EXPIRATION"` JWTRefreshExpiry time.Duration `mapstructure:"JWT_REFRESH_EXPIRY"` }
AppConfig represents the main application configuration.
type AppEnvironment ¶
type AppEnvironment string
AppEnvironment represents the application environment.
const ( AppEnvironmentProd AppEnvironment = "prod" AppEnvironmentDev AppEnvironment = "dev" AppEnvironmentTest AppEnvironment = "test" AppEnvironmentIntegration AppEnvironment = "integration" )
func GetEnv ¶
func GetEnv(key, defaultValue string) AppEnvironment
GetEnv returns the value of an environment variable or the default value if not set. Typical the environment variable is "APP_ENV".
func (AppEnvironment) IsDevelopment ¶
func (e AppEnvironment) IsDevelopment() bool
func (AppEnvironment) IsIntegration ¶
func (e AppEnvironment) IsIntegration() bool
func (AppEnvironment) IsProduction ¶
func (e AppEnvironment) IsProduction() bool
func (AppEnvironment) IsTest ¶
func (e AppEnvironment) IsTest() bool
type ConfigLoader ¶
type ConfigLoader[T any] struct { // contains filtered or unexported fields }
ConfigLoader is a generic configuration loader that provides a fluent API for loading configuration files with defaults, validation, and merging.
func New ¶
func New[T any](cfg *T) *ConfigLoader[T]
New creates a new ConfigLoader for the given config struct.
func (*ConfigLoader[T]) Load ¶
func (cl *ConfigLoader[T]) Load(appEnv AppEnvironment, configPath string) error
Load loads the configuration from files and environment variables. Simplified flow: 1. Load config.example.yaml (base) 2. Merge config.yaml (overrides) 3. Merge config.{env}.yaml (environment-specific) 4. Merge environment variables 5. Unmarshal into config struct 6. Run validation callback if provided
func (*ConfigLoader[T]) WithValidation ¶
func (cl *ConfigLoader[T]) WithValidation(validator func(*T) error) *ConfigLoader[T]
WithValidation sets a validation callback that will be called after the config is loaded. The callback should return an error if the configuration is invalid.
Example:
cfg := &MyConfig{} err := New(cfg).WithValidation(func(cfg *MyConfig) error { if cfg.Port <= 0 { return fmt.Errorf("port must be positive") } if cfg.Database.Host == "" { return fmt.Errorf("database host is required") } return nil }).Load(AppEnvironmentDev, "./configs")
func (*ConfigLoader[T]) WithViper ¶
func (cl *ConfigLoader[T]) WithViper(v *viper.Viper) *ConfigLoader[T]
WithViper sets a custom viper instance for the loader.
type EmailConfig ¶
type EmailConfig struct { SMTPHost string `mapstructure:"SMTP_HOST"` SMTPPort int `mapstructure:"SMTP_PORT"` From string `mapstructure:"FROM"` Password string `mapstructure:"PASSWORD"` UseTLS bool `mapstructure:"USE_TLS"` Enabled bool `mapstructure:"ENABLED"` }
EmailConfig represents email/SMTP configuration.
type MongoDBConfig ¶
type MongoDBConfig struct { Host string `mapstructure:"HOST"` Port int `mapstructure:"PORT"` Database string `mapstructure:"DATABASE"` AuthSource string `mapstructure:"AUTH_SOURCE"` ReplicaSet string `mapstructure:"REPLICA_SET"` MaxPoolSize uint64 `mapstructure:"MAX_POOL_SIZE"` MinPoolSize uint64 `mapstructure:"MIN_POOL_SIZE"` Timeout time.Duration `mapstructure:"TIMEOUT"` Enabled bool `mapstructure:"ENABLED"` }
MongoDBConfig represents MongoDB configuration.
func (*MongoDBConfig) GetURI ¶
func (c *MongoDBConfig) GetURI() string
GetURI returns the MongoDB connection URI.
type MySQLConfig ¶
type MySQLConfig struct { Host string `mapstructure:"HOST"` Port int `mapstructure:"PORT"` User string `mapstructure:"USER"` Password string `mapstructure:"PASSWORD"` Database string `mapstructure:"DATABASE"` Charset string `mapstructure:"CHARSET"` ParseTime bool `mapstructure:"PARSE_TIME"` Loc string `mapstructure:"LOC"` MaxOpenConns int `mapstructure:"MAX_OPEN_CONNS"` MaxIdleConns int `mapstructure:"MAX_IDLE_CONNS"` ConnMaxLifetime time.Duration `mapstructure:"CONN_MAX_LIFETIME"` ConnMaxIdleTime time.Duration `mapstructure:"CONN_MAX_IDLE_TIME"` Enabled bool `mapstructure:"ENABLED"` }
MySQLConfig represents MySQL database configuration.
func (*MySQLConfig) GetDSN ¶
func (c *MySQLConfig) GetDSN() string
GetDSN returns the MySQL DSN string.
type PostgresConfig ¶
type PostgresConfig struct { Host string `mapstructure:"HOST"` Port int `mapstructure:"PORT"` User string `mapstructure:"USER"` Password string `mapstructure:"PASSWORD"` Database string `mapstructure:"DATABASE"` SSLMode string `mapstructure:"SSL_MODE"` Timezone string `mapstructure:"TIMEZONE"` MaxOpenConns int `mapstructure:"MAX_OPEN_CONNS"` MaxIdleConns int `mapstructure:"MAX_IDLE_CONNS"` ConnMaxLifetime time.Duration `mapstructure:"CONN_MAX_LIFETIME"` ConnMaxIdleTime time.Duration `mapstructure:"CONN_MAX_IDLE_TIME"` Enabled bool `mapstructure:"ENABLED"` }
PostgresConfig represents PostgreSQL database configuration.
func (*PostgresConfig) GetDSN ¶
func (c *PostgresConfig) GetDSN() string
GetDSN returns the PostgreSQL DSN string.
type RedisConfig ¶
type RedisConfig struct { Host string `mapstructure:"HOST"` Port int `mapstructure:"PORT"` DB int `mapstructure:"DB"` Password string `mapstructure:"PASSWORD"` PoolSize int `mapstructure:"POOL_SIZE"` MaxRetries int `mapstructure:"MAX_RETRIES"` DialTimeout time.Duration `mapstructure:"DIAL_TIMEOUT"` ReadTimeout time.Duration `mapstructure:"READ_TIMEOUT"` WriteTimeout time.Duration `mapstructure:"WRITE_TIMEOUT"` Enabled bool `mapstructure:"ENABLED"` }
RedisConfig represents Redis configuration.
func (*RedisConfig) GetAddr ¶
func (c *RedisConfig) GetAddr() string
GetAddr returns the Redis address string.
type TelegramConfig ¶
type TelegramConfig struct { BotToken string `mapstructure:"BOT_TOKEN"` ChannelID int64 `mapstructure:"CHANNEL_ID"` MessageThreadID int64 `mapstructure:"MESSAGE_THREAD_ID"` Enabled bool `mapstructure:"ENABLED"` }
TelegramConfig represents Telegram bot configuration.
type TelegramConfigs ¶
type TelegramConfigs []*TelegramConfig