Refactoring code for modular approach

This commit is contained in:
2025-09-09 23:03:08 +02:00
parent 8fbe64687a
commit 1fc8810242
11 changed files with 74 additions and 84 deletions

View File

@@ -1,26 +1,40 @@
from pydantic_settings import BaseSettings
from typing import List
import os
# app/core/config.py
from pydantic_settings import BaseSettings # This is the correct import for Pydantic v2
from typing import Optional, List
class Settings(BaseSettings):
PROJECT_NAME: str = "Ecommerce Backend API with Marketplace Support"
DESCRIPTION: str = "Advanced product management system with JWT authentication"
VERSION: str = "2.2.0"
# Project information
project_name: str = "Ecommerce Backend API with Marketplace Support"
description: str = "Advanced product management system with JWT authentication"
version: str = "2.2.0"
DATABASE_URL: str = os.getenv("DATABASE_URL", "postgresql://user:password@localhost/ecommerce")
SECRET_KEY: str = os.getenv("SECRET_KEY", "your-secret-key-change-in-production")
ACCESS_TOKEN_EXPIRE_MINUTES: int = 30
# Database
database_url: str = "sqlite:///./ecommerce.db"
ALLOWED_HOSTS: List[str] = ["*"] # Configure for production
# JWT
jwt_secret_key: str = "change-this-in-production"
jwt_expire_hours: int = 24
jwt_expire_minutes: int = 30
# Rate limiting
RATE_LIMIT_REQUESTS: int = 100
RATE_LIMIT_WINDOW: int = 3600
# Middleware
allowed_hosts: List[str] = ["*"] # Configure for production
class Config:
env_file = ".env"
extra = "ignore"
# API
api_host: str = "0.0.0.0"
api_port: int = 8000
debug: bool = False
# Rate Limiting
rate_limit_enabled: bool = True
rate_limit_requests: int = 100
rate_limit_window: int = 3600
# Logging
log_level: str = "INFO"
log_file: Optional[str] = None
model_config = {"env_file": ".env"} # Updated syntax for Pydantic v2
settings = Settings()

View File

@@ -3,7 +3,7 @@ from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, Session
from .config import settings
engine = create_engine(settings.DATABASE_URL)
engine = create_engine(settings.database_url)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()

View File

@@ -9,12 +9,12 @@ def setup_logging():
"""Configure application logging with file and console handlers"""
# Create logs directory if it doesn't exist
log_file = Path(settings.LOG_FILE)
log_file = Path(settings.log_file)
log_file.parent.mkdir(parents=True, exist_ok=True)
# Configure root logger
logger = logging.getLogger()
logger.setLevel(getattr(logging, settings.LOG_LEVEL.upper()))
logger.setLevel(getattr(logging, settings.log_level.upper()))
# Remove existing handlers
for handler in logger.handlers[:]: