A production-ready FastAPI microservice for task management with PostgreSQL, SQLAlchemy ORM, and complete Docker Compose setup. Part of the SFAutomation portfolio demonstrating modern Python backend architecture.
- FastAPI Framework - Fast, modern Python web framework with automatic API documentation
- Task Management - Create and list tasks with simple REST endpoints
- PostgreSQL Integration - Persistent data storage with SQL database
- SQLAlchemy ORM 2.0 - Clean ORM using modern
select()query syntax - Dependency Injection - Clean, testable architecture with dependency management
- Poetry - Deterministic Python dependency management with lockfile
- Docker & Docker Compose - Complete containerized environment (web + database)
- Health Check Endpoint - Ready for orchestration and monitoring
- Development & Production Ready - Works locally and in containerized environments
- Docker and Docker Compose (recommended)
- Python 3.11+ (for local development)
- Poetry (for local dependency management)
- .env file with database credentials
-
Clone the repository
git clone https://github.com/sfautomation-dev/task-tracker.git cd task-tracker -
Create
.envfilecp .env.example .env
Or manually create
.env:DATABASE_URL=postgresql://taskuser:taskpass@db:5432/taskdb POSTGRES_USER=taskuser POSTGRES_PASSWORD=taskpass POSTGRES_DB=taskdb
-
Start services
docker-compose up --build
-
Run migrations (in another terminal)
bash migrate.sh
-
Access the API
- Web: http://localhost:8000
- Interactive Docs: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
-
Install dependencies
poetry install
-
Create
.envfileDATABASE_URL=postgresql://taskuser:taskpass@localhost:5432/taskdb
-
Ensure PostgreSQL is running on localhost:5432
-
Run the application
poetry run uvicorn app.main:app --reload
-
Access the API at http://localhost:8000
GET /healthReturns application status.
Response (200)
{
"status": "ok"
}GET /tasksRetrieve all tasks ordered by ID.
Response (200)
[
{
"id": 1,
"title": "Complete project documentation",
"done": false
},
{
"id": 2,
"title": "Deploy to production",
"done": true
}
]POST /tasks
Content-Type: application/json
{
"title": "Learn FastAPI"
}Response (201)
{
"id": 3,
"title": "Learn FastAPI",
"done": false
}task-tracker/
├── app/
│ ├── main.py # FastAPI application, routes
│ ├── models.py # SQLAlchemy ORM models
│ ├── database.py # Database engine and session factory
│ ├── deps.py # Dependency injection (get_db)
│ └── __pycache__/
├── docker-compose.yml # Multi-container orchestration
├── Dockerfile # Container image definition
├── pyproject.toml # Poetry dependencies and config
├── poetry.lock # Locked dependency versions
├── migrate.sh # Database migration script
├── .env.example # Environment variables template
├── README.md # This file
└── LICENSE
Create a .env file in the project root:
# Database Connection
DATABASE_URL=postgresql://taskuser:taskpass@db:5432/taskdb
POSTGRES_USER=taskuser
POSTGRES_PASSWORD=taskpass
POSTGRES_DB=taskdbFor local development without Docker, use:
DATABASE_URL=postgresql://taskuser:taskpass@localhost:5432/taskdbCore
fastapi^0.100.0 - Web frameworkuvicorn[standard]^0.22.0 - ASGI serversqlalchemy^2.0.0 - ORMpsycopg2-binary^2.9.0 - PostgreSQL driverpython-dotenv^1.0.0 - Environment variable management
Python Version: ^3.11
See pyproject.toml for complete dependency list.
docker build -t task-tracker:latest .docker run -p 8000:8000 --env-file .env task-tracker:latest# Start services
docker-compose up --build
# Stop services
docker-compose down
# View logs
docker-compose logs -f web
# Run migrations
bash migrate.shtasks table
| Column | Type | Constraints |
|---|---|---|
| id | INTEGER | PRIMARY KEY, AUTO INCREMENT |
| title | VARCHAR | NOT NULL |
| done | BOOLEAN | DEFAULT FALSE |
Tables are automatically created on application startup via SQLAlchemy's Base.metadata.create_all().
For more control, run:
bash migrate.shapp/main.py- FastAPI application instance and route handlersapp/models.py- SQLAlchemy ORM model definitionsapp/database.py- Database connection and session managementapp/deps.py- Dependency injection functions
poetry run pytestpoetry run black app/
poetry run isort app/poetry run mypy app/- Database Connection Pooling - Consider
SQLAlchemyconnection pooling for production - CORS Middleware - Add CORS if frontend is on different domain
- Authentication - Implement JWT or OAuth2 for security
- Logging - Use Python's
loggingmodule with centralized aggregation - Monitoring - Add Prometheus metrics and health checks
- Database Migrations - Use Alembic for version-controlled migrations
- Load Balancing - Deploy multiple instances behind nginx/load balancer
# Free port 8000
sudo lsof -ti:8000 | xargs kill -9
# Use different port
poetry run uvicorn app.main:app --port 8001- Verify PostgreSQL is running
- Check DATABASE_URL in
.env - Ensure credentials match PostgreSQL setup
- For Docker:
docker-compose logs db
# Clear cache and rebuild
docker-compose down --volumes
docker-compose build --no-cacheSee LICENSE file for details.
Built by SFAutomation as a demonstration of modern Python backend architecture and best practices.