Modern Python backend built with FastAPI 2.0, SQLAlchemy 2.0, and PostgreSQL.
- 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
- Copy environment variables:
cp .env.example .env-
Fill in the required environment variables in
.env -
Start with Docker Compose:
docker compose up --build- Create and activate virtual environment:
cd backend
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt- Copy environment variables:
cp .env.example .env-
Fill in the required environment variables in
.env -
Run database migrations:
alembic upgrade head- Start the development server:
uvicorn main:app --reload --host 0.0.0.0 --port 8000- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
- Health Check: http://localhost:8000/health
# Create a new migration
alembic revision --autogenerate -m "Description of changes"
# Apply migrations
alembic upgrade head
# Rollback last migration
alembic downgrade -1Uncomment this line in main.py if you don't want to use migrations:
user_models.Base.metadata.create_all(bind=engine)# Run all tests
pytest
# Run with coverage
pytest --cov=.
# Run specific test file
pytest test/test_users.py -vbackend/
├── 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
# 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# Format code
black .
# Sort imports
isort .
# Lint code
flake8 .DATABASE_URL: PostgreSQL connection stringALLOWED_ORIGINS: Comma-separated list of allowed CORS originsJWT_SECRET_KEY: Secret key for JWT tokensJWT_ALGORITHM: JWT algorithm (default: HS256)