Skip to content

FASTAPI-TODO-LIST/backend

Repository files navigation

🚀 FastAPI Todo Backend

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.

✨ Features

Core Functionality

  • 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

Technical Features

  • 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

🛠️ Tech Stack

  • 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+

🚀 Quick Start

Prerequisites

  • Docker & Docker Compose
  • Python 3.9+ (for local development)

Docker Setup (Recommended)

# Clone and navigate to backend directory
cd backend

# Start services
docker-compose up --build -d

# View logs
docker-compose logs -f app

Local Development Setup

# 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

📚 API Documentation

Interactive Documentation

Core Endpoints

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 -

Response Models

{
  "id": 1,
  "title": "Complete project",
  "completed": false
}

🗄️ Database Schema

Todo Model

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()
);

🔧 Configuration

Environment Variables

# 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

Docker Services

  • app: FastAPI application (Port 8000)
  • db: PostgreSQL database (Port 5432)

🧪 Testing

# 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/

📁 Project Structure

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

🚀 Deployment

Production Considerations

  • 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

Docker Production

# 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

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

📄 License

This project is open source and available under the MIT License.

About

fast api todo list backend repo

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published