ashnaali22/phase-3-h2
0
Todo App Backend - Docker Deployment
FastAPI backend with AI chatbot integration using OpenAI Agents SDK and MCP protocol.
๐ Quick Start with Docker
1. Build and Run
# Build the image
docker build -t todo-app-backend .
# Run the container
docker run -p 7860:7860 \
-e DATABASE_URL="postgresql://..." \
-e FRONTEND_URL="http://localhost:3000" \
-e OPENAI_API_KEY="sk-..." \
todo-app-backend2. Using Docker Compose
# Start all services
docker-compose up -d
# View logs
docker-compose logs -f
# Stop services
docker-compose down๐ Environment Variables
Example .env file
DATABASE_URL=postgresql://user:password@host.neon.tech/dbname?sslmode=require
FRONTEND_URL=http://localhost:3000
OPENAI_API_KEY=sk-your-api-key
APP_PORT=7860
DEBUG=False
LOG_LEVEL=INFO๐๏ธ Docker Configuration
Port Mapping
The container exposes port 7860 (Hugging Face Spaces default). Map to desired port:
docker run -p 8000:7860 todo-app-backendHealth Check
The image includes a health check at /api/v1/health:
curl http://localhost:7860/api/v1/healthExpected response:
{
"status": "healthy",
"service": "todo-app-backend"
}๐ Project Structure
backend/
โโโ src/
โ โโโ api/v1/ # API endpoints (chat, tasks, auth, etc.)
โ โโโ models/ # SQLModel database models
โ โโโ services/ # Business logic
โ โโโ main.py # FastAPI application entry point
โโโ alembic/ # Database migrations
โโโ tests/ # Test suite
โโโ Dockerfile # Docker image definition
โโโ docker-compose.yml # Docker Compose configuration
โโโ requirements.txt # Python dependencies
โโโ pyproject.toml # Project metadata
โโโ .env # Environment variables (not committed)๐๏ธ Database Setup
Run Migrations
# Inside container
docker exec -it todo-app-backend-1 alembic upgrade headOr use entrypoint script
docker run todo-app-backend ./start_server.sh๐ง Development
Local Development (without Docker)
# Create virtual environment
python -m venv .venv
source .venv/bin/activate # Linux/Mac
# or
.venv\Scripts\activate # Windows
# Install dependencies
pip install -r requirements.txt
# Run migrations
alembic upgrade head
# Start server
uvicorn main:app --reload --port 8000Running Tests
# Run all tests
pytest tests/ -v
# Run with coverage
pytest tests/ --cov=src --cov-report=html๐ API Documentation
Once running, access:
- Swagger UI: http://localhost:7860/api/docs
- ReDoc: http://localhost:7860/api/redoc
- OpenAPI JSON: http://localhost:7860/api/openapi.json
๐ค AI Chatbot Features
The backend includes an AI chatbot with natural language task management:
Supported Commands
MCP Tools Available
add_task- Create new taskslist_tasks- Retrieve tasks (all/pending/completed)complete_task- Mark tasks as donedelete_task- Remove tasksupdate_task- Modify task details
๐ Authentication
The API uses JWT authentication via Better Auth. Include the token in requests:
curl -X POST http://localhost:7860/api/v1/tasks \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-H "Content-Type: application/json"๐ Monitoring
View Logs
# Docker logs
docker logs -f todo-app-backend
# With timestamps
docker logs -f --timestamps todo-app-backendHealth Endpoints
GET /api/v1/health- Basic health checkGET /api/v1/health/ready- Readiness check (includes DB)
๐จ Troubleshooting
Database Connection Failed
- Verify
DATABASE_URLis correct - Ensure
?sslmode=requireis appended - Check Neon database is not suspended
CORS Errors
- Verify
FRONTEND_URLmatches your frontend origin - Ensure HTTPS in production URLs
Container Won't Start
- Check logs:
docker logs todo-app-backend - Verify all required env vars are set
- Ensure port 7860 is not in use
๐ฆ Deployment Targets
Hugging Face Spaces
The Dockerfile is optimized for Hugging Face Spaces deployment:
# Builds automatically on push to HF
# Uses port 7860 by defaultRailway / Render / Fly.io
# For platforms using PORT env var
docker run -p $PORT:7860 todo-app-backendAWS ECS / GCP Cloud Run
# Push to container registry
docker tag todo-app-backend:latest YOUR_REGISTRY/todo-app-backend
docker push YOUR_REGISTRY/todo-app-backend๐ License
MIT License - see parent repository for details.
๐ค Contributing
See parent repository for contribution guidelines.
