A modern, high-performance Todo API built with FastAPI, PostgreSQL, and Docker. This backend provides a robust REST API for managing todo items with full CRUD operations, data validation, and comprehensive error handling.
- Full CRUD Operations: Create, read, update, and delete todos
- Data Validation: Pydantic models for request/response validation
- Error Handling: Comprehensive HTTP error responses
- Health Monitoring: Built-in health check endpoint
- PostgreSQL Integration: Robust database with SQLAlchemy ORM
- CORS Support: Cross-origin requests enabled for frontend integration
- Docker Containerization: Easy deployment and development setup
- Auto-generated Documentation: Interactive API docs with Swagger UI
- Database Migrations: Automatic table creation on startup
- Connection Pooling: Optimized database connections
- Framework: FastAPI 0.104+
- Database: PostgreSQL 13
- ORM: SQLAlchemy with declarative models
- Validation: Pydantic v2
- Server: Uvicorn ASGI server
- Containerization: Docker & Docker Compose
- Environment: Python 3.9+
- Docker & Docker Compose
- Python 3.9+ (for local development)
# Clone and navigate to backend directory
cd backend
# Start services
docker-compose up --build -d
# View logs
docker-compose logs -f app# Install dependencies
pip install -r requirements.txt
# Set up environment
cp env.example .env
# Edit .env with your database credentials
# Run database migrations
python -c "from app.database import create_tables; create_tables()"
# Start development server
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
- OpenAPI Schema: http://localhost:8000/openapi.json
| Method | Endpoint | Description | Request Body |
|---|---|---|---|
GET |
/ |
API welcome message | - |
GET |
/todos/ |
Get all todos | - |
POST |
/todos/ |
Create new todo | {"title": "string"} |
GET |
/todos/{id} |
Get specific todo | - |
PATCH |
/todos/{id} |
Update todo | {"title": "string", "completed": boolean} |
DELETE |
/todos/{id} |
Delete todo | - |
GET |
/health |
Health check | - |
{
"id": 1,
"title": "Complete project",
"completed": false
}CREATE TABLE todos (
id SERIAL PRIMARY KEY,
title VARCHAR(200) NOT NULL,
completed BOOLEAN DEFAULT FALSE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);# Database Configuration
POSTGRES_USER=user
POSTGRES_PASSWORD=shan
POSTGRES_DB=todo_list
DB_HOST=db
DB_PORT=5432
# Optional
DATABASE_URL=postgresql://user:shan@db:5432/todo_list
DB_ECHO=false # Set to true for SQL query logging- app: FastAPI application (Port 8000)
- db: PostgreSQL database (Port 5432)
# Test API endpoints
curl http://localhost:8000/health
curl http://localhost:8000/todos/
curl -X POST -H "Content-Type: application/json" -d '{"title":"Test todo"}' http://localhost:8000/todos/backend/
├── app/
│ ├── __init__.py
│ ├── main.py # FastAPI application and routes
│ ├── models.py # SQLAlchemy database models
│ └── database.py # Database configuration and connection
├── requirements.txt # Python dependencies
├── Dockerfile # Container configuration
├── docker-compose.yml # Multi-service orchestration
├── .env # Environment variables
└── README.md # This file
- Use environment-specific database credentials
- Enable SSL/TLS for database connections
- Configure proper logging levels
- Set up database backups
- Use a reverse proxy (nginx) for production
- Implement rate limiting and authentication
# Build production image
docker build -t todo-api:latest .
# Run with production settings
docker run -d -p 8000:8000 --env-file .env todo-api:latest- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is open source and available under the MIT License.