diff options
Diffstat (limited to 'backend/internal/config')
-rw-r--r-- | backend/internal/config/config.go | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/backend/internal/config/config.go b/backend/internal/config/config.go new file mode 100644 index 0000000..f61f423 --- /dev/null +++ b/backend/internal/config/config.go @@ -0,0 +1,54 @@ +package config + +import ( + "log" + "os" + + "github.com/joho/godotenv" +) + +// Config holds the application configuration +type Config struct { + DatabaseDSN string + JWTSecret string // Secret key for signing JWTs + // Add other config fields here later (e.g., server port) +} + +// LoadConfig loads configuration from environment variables or a .env file +func LoadConfig() (*Config, error) { + // Attempt to load .env file (useful for development) + // In production, rely on environment variables set directly + err := godotenv.Load() // Load .env file from current directory or parent dirs + if err != nil { + log.Println("No .env file found, relying on environment variables") + } + + dsn := os.Getenv("DATABASE_DSN") + if dsn == "" { + // Set a default for local development if not provided + log.Println("DATABASE_DSN not set, using default local PostgreSQL DSN") + log.Println("IMPORTANT: Ensure PostgreSQL is running with the correct credentials.") + log.Println("Try creating a database 'finance' and setting up a user with the correct permissions.") + log.Println("Example commands:") + log.Println(" createdb finance") + log.Println(" createuser -P -s -e user_name") + + // Use more common/generic default credentials + dsn = "host=localhost user=postgres password=postgres dbname=finance port=5432 sslmode=disable TimeZone=UTC" + // Consider making the default conditional or removing it for stricter environments + } + + jwtSecret := os.Getenv("JWT_SECRET") + if jwtSecret == "" { + log.Println("WARNING: JWT_SECRET environment variable not set. Using an insecure default.") + // !!! IMPORTANT: Use a strong, randomly generated secret in production !!! + // For development only: + jwtSecret = "insecure-default-dev-secret-change-me" + // return nil, errors.New("JWT_SECRET environment variable is required") + } + + return &Config{ + DatabaseDSN: dsn, + JWTSecret: jwtSecret, + }, nil +} |