config

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 16, 2025 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package config handles loading and validation of application configuration from environment variables and potentially configuration files.

Package config handles loading and validation of application configuration from environment variables and potentially configuration files.

Package config handles loading and validation of application configuration from environment variables and potentially configuration files.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConfigureNeonPostgresPool

func ConfigureNeonPostgresPool(cfg *DatabaseConfig) (*pgxpool.Config, error)

ConfigureNeonPostgresPool creates and configures a pgxpool.Config suitable for connecting to a Neon PostgreSQL database using the provided DatabaseConfig. It sets up the connection string, configures TLS (required for Neon), and sets connection pool parameters, logging non-sensitive details.

func ConfigureUpstashRedisOptions

func ConfigureUpstashRedisOptions(cfg *RedisConfig) *redis.Options

ConfigureUpstashRedisOptions creates and configures a redis.Options suitable for connecting to an Upstash Redis instance using the provided RedisConfig. It sets up connection details, pool parameters, timeouts, retry logic, and enables TLS (required for Upstash), logging non-sensitive details.

func CreateConfigTemplateForEnvironment

func CreateConfigTemplateForEnvironment(env EnvType) error

Create template environment-specific config files

func InitDB

func InitDB(config *DBConfig) (*sql.DB, error)

InitDB initializes a standard sql.DB connection using the provided DBConfig. Deprecated: This function uses database/sql directly and is likely superseded by the pgxpool setup in main.go using ConfigureNeonPostgresPool.

func InitRedis

func InitRedis(config *RedisConfig) (*redis.Client, error)

InitRedis initializes a Redis client connection using the provided RedisConfig. Deprecated: This function is likely superseded by the setup in main.go using ConfigureUpstashRedisOptions and TestRedisConnection.

func TestRedisConnection

func TestRedisConnection(client *redis.Client) error

TestRedisConnection attempts to ping the Redis server using the provided client. It retries the connection up to a maximum number of times with a delay between attempts. Returns nil if the connection is successful, otherwise returns an error.

Types

type Config

type Config struct {
	Server           ServerConfig       `mapstructure:"SERVER" yaml:"server"`
	Database         DatabaseConfig     `mapstructure:"DATABASE" yaml:"database"`
	Redis            RedisConfig        `mapstructure:"REDIS" yaml:"redis"`
	Email            EmailConfig        `mapstructure:"EMAIL" yaml:"email"`
	ExternalServices ExternalServices   `mapstructure:"EXTERNAL_SERVICES" yaml:"external_services"`
	EventService     EventServiceConfig `mapstructure:"EVENT_SERVICE" yaml:"event_service"` // +++ Add EventService config field +++
}

Config aggregates all application configuration sections.

func LoadConfig

func LoadConfig() (*Config, error)

LoadConfig loads configuration from environment variables using Viper, sets default values, binds environment variables to config struct fields, unmarshals the configuration, and validates it.

func LoadConfigForEnv

func LoadConfigForEnv(environment string) (*Config, error)

LoadConfig loads configuration for a specific environment

func LoadConfigFromFile

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

LoadConfigFromFile loads configuration from a specific file using Viper.

func (*Config) IsDevelopment

func (c *Config) IsDevelopment() bool

IsDevelopment returns true if the application is running in development environment.

func (*Config) IsProduction

func (c *Config) IsProduction() bool

IsProduction returns true if the application is running in production environment.

type DBConfig

type DBConfig struct {
	Host         string
	Port         int
	User         string
	Password     string
	DatabaseName string
	SSLMode      string
	MaxOpenConns int
	MaxIdleConns int
	ConnMaxLife  time.Duration
}

DBConfig holds database configuration. Deprecated: This struct is likely superseded by DatabaseConfig in config.go. Consider migrating usage to the main Config struct loaded by LoadConfig.

func GetDBConfig

func GetDBConfig() *DBConfig

GetDBConfig loads database configuration from environment variables. Deprecated: This function is likely superseded by LoadConfig in config.go. Consider using the unified Config struct instead.

type DatabaseConfig

type DatabaseConfig struct {
	Host           string `mapstructure:"HOST" yaml:"host"`
	Port           int    `mapstructure:"PORT" yaml:"port"`
	User           string `mapstructure:"USER" yaml:"user"`
	Password       string `mapstructure:"PASSWORD" yaml:"password"`
	Name           string `mapstructure:"NAME" yaml:"name"`
	MaxConnections int    `mapstructure:"MAX_CONNECTIONS" yaml:"max_connections"`
	SSLMode        string `mapstructure:"SSL_MODE" yaml:"ssl_mode"`
	MaxOpenConns   int    `mapstructure:"MAX_OPEN_CONNS" yaml:"max_open_conns"`
	MaxIdleConns   int    `mapstructure:"MAX_IDLE_CONNS" yaml:"max_idle_conns"`
	ConnMaxLife    string `mapstructure:"CONN_MAX_LIFE" yaml:"conn_max_life"`
}

DatabaseConfig holds PostgreSQL database connection details.

type EmailConfig

type EmailConfig struct {
	FromAddress  string `mapstructure:"FROM_ADDRESS" yaml:"from_address"`
	FromName     string `mapstructure:"FROM_NAME" yaml:"from_name"`
	BaseURL      string `mapstructure:"BASE_URL" yaml:"base_url"`
	ResendAPIKey string `mapstructure:"RESEND_API_KEY" yaml:"resend_api_key"`
}

EmailConfig holds configuration for sending emails.

type EnvType

type EnvType string

Environment represents a deployment environment

const (
	// Development environment
	Development EnvType = "dev"

	// Staging environment
	Staging EnvType = "staging"

	// Production environment
	Production EnvType = "production"
)

type Environment

type Environment string

Environment represents the application's running environment (development or production).

const (
	EnvDevelopment Environment = "development"
	EnvProduction  Environment = "production"
)

type EventServiceConfig

type EventServiceConfig struct {
	// Timeout for publishing a single event to Redis (in seconds)
	PublishTimeoutSeconds int `mapstructure:"PUBLISH_TIMEOUT_SECONDS" yaml:"publish_timeout_seconds"`
	// Timeout for establishing a subscription connection via Redis (in seconds)
	SubscribeTimeoutSeconds int `mapstructure:"SUBSCRIBE_TIMEOUT_SECONDS" yaml:"subscribe_timeout_seconds"`
	// Buffer size for the channel delivering events to a single subscriber
	EventBufferSize int `mapstructure:"EVENT_BUFFER_SIZE" yaml:"event_buffer_size"`
}

EventServiceConfig holds configuration for the Redis-based event service.

type ExternalServices

type ExternalServices struct {
	GeoapifyKey       string `mapstructure:"GEOAPIFY_KEY"`
	PexelsAPIKey      string `mapstructure:"PEXELS_API_KEY"`
	SupabaseAnonKey   string `mapstructure:"SUPABASE_ANON_KEY"`
	SupabaseURL       string `mapstructure:"SUPABASE_URL"`
	SupabaseJWTSecret string `mapstructure:"SUPABASE_JWT_SECRET"`
	EmailFromAddress  string `mapstructure:"EMAIL_FROM_ADDRESS"`
	EmailFromName     string `mapstructure:"EMAIL_FROM_NAME"`
	EmailBaseURL      string `mapstructure:"EMAIL_BASE_URL" default:"https://api.mailchannels.net"`
}

ExternalServices holds API keys and URLs for external services.

type RedisConfig

type RedisConfig struct {
	Address      string `mapstructure:"ADDRESS" yaml:"address"`
	Password     string `mapstructure:"PASSWORD" yaml:"password"`
	DB           int    `mapstructure:"DB" yaml:"db"`
	UseTLS       bool   `mapstructure:"USE_TLS" yaml:"use_tls"`
	PoolSize     int    `mapstructure:"POOL_SIZE" yaml:"pool_size"`
	MinIdleConns int    `mapstructure:"MIN_IDLE_CONNS" yaml:"min_idle_conns"`
}

RedisConfig holds Redis connection details.

func GetRedisConfig

func GetRedisConfig() *RedisConfig

GetRedisConfig loads Redis configuration from environment variables. Deprecated: This function is likely superseded by LoadConfig in config.go. Consider using the unified Config struct instead.

type ServerConfig

type ServerConfig struct {
	Environment    Environment `mapstructure:"ENVIRONMENT" yaml:"environment"`
	Port           string      `mapstructure:"PORT" yaml:"port"`
	AllowedOrigins []string    `mapstructure:"ALLOWED_ORIGINS" yaml:"allowed_origins"`
	Version        string      `mapstructure:"VERSION" yaml:"version"`
	JwtSecretKey   string      `mapstructure:"JWT_SECRET_KEY" yaml:"jwt_secret_key"`
	FrontendURL    string      `mapstructure:"FRONTEND_URL" yaml:"frontend_url"`
}

ServerConfig holds server-specific configuration.

Jump to

Keyboard shortcuts

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