Skip to content

Latest commit

 

History

History

README.md

Backend - FastAPI 2.0

Modern Python backend built with FastAPI 2.0, SQLAlchemy 2.0, and PostgreSQL.

🚀 Features

  • FastAPI 2.0 with async/await support
  • SQLAlchemy 2.0 with async database operations
  • PostgreSQL database with Alembic migrations
  • JWT Authentication with Authlib
  • Pydantic 2.0 for data validation
  • Comprehensive testing with pytest
  • Code quality tools (Black, isort, flake8)
  • Health checks and monitoring endpoints
  • CORS and security middleware

🛠️ Setup

🐳 Docker Setup (Recommended)

  1. Copy environment variables:
cp .env.example .env
  1. Fill in the required environment variables in .env

  2. Start with Docker Compose:

docker compose up --build

💻 Local Development Setup

  1. Create and activate virtual environment:
cd backend
python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate
  1. Install dependencies:
pip install -r requirements.txt
  1. Copy environment variables:
cp .env.example .env
  1. Fill in the required environment variables in .env

  2. Run database migrations:

alembic upgrade head
  1. Start the development server:
uvicorn main:app --reload --host 0.0.0.0 --port 8000

📚 API Documentation

🗄️ Database

Migrations

# Create a new migration
alembic revision --autogenerate -m "Description of changes"

# Apply migrations
alembic upgrade head

# Rollback last migration
alembic downgrade -1

Alternative (Development Only)

Uncomment this line in main.py if you don't want to use migrations:

user_models.Base.metadata.create_all(bind=engine)

🧪 Testing

# Run all tests
pytest

# Run with coverage
pytest --cov=.

# Run specific test file
pytest test/test_users.py -v

📁 Project Structure

backend/
├── alembic/           # Database migrations
├── users/            # User management module
│   ├── models.py     # SQLAlchemy models
│   ├── schemas.py    # Pydantic schemas
│   └── routers.py    # API routes
├── utils/            # Utility functions
│   ├── auth.py       # Authentication utilities
│   └── logger.py     # Logging configuration
├── test/             # Test files
├── main.py           # FastAPI application
├── database.py       # Database configuration
└── requirements.txt  # Dependencies

🔧 Development

Virtual Environment Management

# Create virtual environment
python -m venv .venv

# Activate virtual environment
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

# Deactivate virtual environment
deactivate

# Install dependencies
pip install -r requirements.txt

# Update requirements.txt
pip freeze > requirements.txt

Code Quality

# Format code
black .

# Sort imports
isort .

# Lint code
flake8 .

Environment Variables

  • DATABASE_URL: PostgreSQL connection string
  • ALLOWED_ORIGINS: Comma-separated list of allowed CORS origins
  • JWT_SECRET_KEY: Secret key for JWT tokens
  • JWT_ALGORITHM: JWT algorithm (default: HS256)

📖 Additional Documentation