package config import ( "fmt" "os" "strconv" ) // Config holds all application configuration type Config struct { Server ServerConfig Database DatabaseConfig OAuth OAuthConfig Session SessionConfig } // ServerConfig holds server-related configuration type ServerConfig struct { Port string Host string } // DatabaseConfig holds DynamoDB configuration type DatabaseConfig struct { Region string Endpoint string // For DynamoDB local TablePrefix string UseLocalDB bool } // OAuthConfig holds OAuth provider configurations type OAuthConfig struct { Google GoogleOAuthConfig } // GoogleOAuthConfig holds Google OAuth configuration type GoogleOAuthConfig struct { ClientID string ClientSecret string RedirectURL string } // SessionConfig holds session management configuration type SessionConfig struct { SecretKey string MaxAge int // in seconds } // Load loads configuration from environment variables func Load() (*Config, error) { cfg := &Config{ Server: ServerConfig{ Port: getEnv("PORT", "8080"), Host: getEnv("HOST", "localhost"), }, Database: DatabaseConfig{ Region: getEnv("AWS_REGION", "us-east-1"), Endpoint: getEnv("DYNAMODB_ENDPOINT", "http://localhost:8000"), TablePrefix: getEnv("TABLE_PREFIX", "startpage_"), UseLocalDB: getEnvBool("USE_LOCAL_DB", true), }, OAuth: OAuthConfig{ Google: GoogleOAuthConfig{ ClientID: getEnv("GOOGLE_CLIENT_ID", ""), ClientSecret: getEnv("GOOGLE_CLIENT_SECRET", ""), RedirectURL: getEnv("GOOGLE_REDIRECT_URL", "http://localhost:8080/auth/callback/google"), }, }, Session: SessionConfig{ SecretKey: getEnv("SESSION_SECRET", "change-me-in-production"), MaxAge: getEnvInt("SESSION_MAX_AGE", 86400*7), // 7 days default }, } // Validate required fields if cfg.OAuth.Google.ClientID == "" { return nil, fmt.Errorf("GOOGLE_CLIENT_ID is required") } if cfg.OAuth.Google.ClientSecret == "" { return nil, fmt.Errorf("GOOGLE_CLIENT_SECRET is required") } return cfg, nil } // getEnv gets an environment variable or returns a default value func getEnv(key, defaultValue string) string { if value := os.Getenv(key); value != "" { return value } return defaultValue } // getEnvBool gets a boolean environment variable or returns a default value func getEnvBool(key string, defaultValue bool) bool { if value := os.Getenv(key); value != "" { boolVal, err := strconv.ParseBool(value) if err == nil { return boolVal } } return defaultValue } // getEnvInt gets an integer environment variable or returns a default value func getEnvInt(key string, defaultValue int) int { if value := os.Getenv(key); value != "" { intVal, err := strconv.Atoi(value) if err == nil { return intVal } } return defaultValue }