package config import ( "fmt" "os" "github.com/joho/godotenv" ) // Config holds application configuration type Config struct { // Database configuration DatabaseDSN string // JWT configuration JWTSecret string JWTExpiry int // in hours // Server configuration ServerPort int ServerHost string // Environment (development, staging, production) Environment string } // LoadConfig loads the application configuration from environment variables func LoadConfig() (*Config, error) { // Load .env file if it exists _ = godotenv.Load() // Ignore error if .env file doesn't exist // Set defaults and override with environment variables config := &Config{ DatabaseDSN: getEnv("DATABASE_DSN", "postgresql://postgres:postgres@localhost:5432/finance?sslmode=disable"), JWTSecret: getEnv("JWT_SECRET", "supersecretkey"), // This should be changed in production JWTExpiry: getEnvInt("JWT_EXPIRY", 24), // Default to 24 hours ServerPort: getEnvInt("SERVER_PORT", 8080), // Default to 8080 ServerHost: getEnv("SERVER_HOST", "0.0.0.0"), // Default to all interfaces Environment: getEnv("APP_ENV", "development"), // Default to development } return config, nil } // getEnv gets an environment variable or returns a default value func getEnv(key, defaultValue string) string { value := os.Getenv(key) if value == "" { return defaultValue } return value } // getEnvInt gets an environment variable as an integer or returns a default value func getEnvInt(key string, defaultValue int) int { value := os.Getenv(key) if value == "" { return defaultValue } intValue, err := parseInt(value) if err != nil { return defaultValue } return intValue } // parseInt parses a string to an int func parseInt(value string) (int, error) { intValue := 0 _, err := fmt.Sscanf(value, "%d", &intValue) return intValue, err }