RA
rahulsamant37/FASTAPI_clean-architecture
A production-ready Todo API built with FastAPI following clean architecture principles and best practices.
FastAPI Clean Architecture Todo API
A production-ready Todo API built with FastAPI following clean architecture principles and best practices.
๐๏ธ Architecture
This project follows clean architecture principles, with clear separation of:
- Entities - Core business objects (
User,Todo) - Use Cases - Application business rules in services
- Controllers - HTTP request handlers
- Infrastructure - Database, authentication, and external interfaces
๐ Key Features
- Complete CRUD operations for Todos
- User authentication with JWT
- Password hashing with bcrypt
- Rate limiting
- PostgreSQL database with SQLAlchemy ORM
- Comprehensive test suite
- Docker support
- Clean code structure following best practices
๐ Getting Started
Prerequisites
- Python 3.12+
- Docker and Docker Compose (optional)
- PostgreSQL (if running without Docker)
Running with Docker
# Start the application and database
docker-compose upThe API will be available at http://localhost:8000
Local Development Setup
# Create a virtual environment
python -m venv venv
source venv/bin/activate # Linux/macOS
venv\Scripts\activate # Windows
# Install dependencies
pip install -r requirements-dev.txt
# Configure environment variables
cp .env.example .env # Update with your values
# Run the application
uvicorn src.main:app --reload๐ API Documentation
Once running, view the interactive API documentation at:
- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc
Key Endpoints
POST /auth/- Register new userPOST /auth/token- Login and get access tokenGET /todos/- List all todosPOST /todos/- Create new todoGET /todos/{id}- Get single todoPUT /todos/{id}- Update todoDELETE /todos/{id}- Delete todoPUT /todos/{id}/complete- Mark todo as complete
๐งช Testing
# Run all tests
pytest
# Run with coverage
pytest --cov=src๐ ๏ธ Project Structure
src/
โโโ auth/ # Authentication logic
โโโ database/ # Database configuration
โโโ entities/ # Domain entities
โโโ todos/ # Todo feature module
โโโ users/ # User feature module
โโโ exceptions.py # Custom exceptions
โโโ logging.py # Logging configuration
โโโ main.py # Application entry point
๐ Security Features
- JWT authentication
- Password hashing with bcrypt
- Rate limiting on registration endpoint
- CORS protection
- Input validation with Pydantic
๐ค Contributing
- Fork the repository
- Create a new branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
On this page
Languages
Python99.0%Dockerfile1.0%
Contributors
GNU General Public License v3.0
Created May 5, 2025
Updated June 5, 2025