A modern RESTful API for task management built with FastAPI, SQLAlchemy 2.x (async), and PostgreSQL. Designed for scalability, testability, and developer experience with advanced monitoring and observability features.
Clone the repo, create .env, and run the app with Docker:
# 1. Clone repository
git clone https://github.com/kmilodenisglez/task-backend.git
cd task-backend
# 2. Copy environment file
cp .env.example .env
# 3. Start dev environment (Docker + hot reload)
make docker-dev
# 4. Open in browser
# API: http://localhost:8000
# Docs: http://localhost:8000/docs
# Health: http://localhost:8000/health/healthπ οΈ For production build:
make docker-prodπ οΈ For Development local (venv + Python):
python -m venv venv && source venv/bin/activate
pip install -e ".[dev]"
make devmake -f Makefile.ci format # Auto-format code
make -f Makefile.ci lint # Check code stylemake -f Makefile.ci test
make -f Makefile.ci coverage- FastAPI backend with Pydantic v2 and async/await support
- Asynchronous database access with
asyncpgand SQLAlchemy (async mode) - PostgreSQL containerized using Docker / Podman
- Database migrations powered by Alembic
- Isolated testing with
pytest,pytest-asyncio,httpx, and SQLite (async mode) - Configurable environments via
.envfiles andpydantic-settings
- API Versioning - v1 and v2 with backward compatibility
- Structured Logging - JSON logging with request tracking and correlation IDs
- Health Checks - Basic and detailed health monitoring with system metrics
- Rate Limiting - Configurable rate limiting with sliding window algorithm
- Monitoring - System metrics, performance monitoring, and observability
- Security - JWT authentication, password validation, and user isolation
- Code quality tools:
mypy,black,isort,flake8,pytest-cov - Developer workflow streamlined with Makefile and
pyproject.toml - Continuous Integration (CI) via GitHub Actions for automated linting, testing, type checking, and coverage reports
- Docker support with development and production configurations
- Python 3.10+ (3.13 recommended)
- Podman or Docker
make(Linux/macOS)pipor asdf (recommended for Python version management)
-
Copy the example env file:
cp .env.example .env
-
Edit
.envwith your secrets and DB credentials.π Do not commit
.envβ only.env.exampleis versioned.
We use a containerized PostgreSQL for dev and test.
mkdir -p ./output/postgres_data
mkdir -p ./output/postgres_runpodman run --name my_postgres \
-e POSTGRES_USER=task_user \
-e POSTGRES_PASSWORD=task_pass \
-e POSTGRES_DB=task_db \
-p 5432:5432 \
-v ./output/postgres_data:/var/lib/postgresql/data:Z \
-v ./output/postgres_run:/var/run/postgresql:Z \
-d postgres:13.6-alpineπ‘ Replace
podmanwithdockerif you prefer Docker.
podman exec -i my_postgres psql -U postgres <<EOF
CREATE USER task_user WITH PASSWORD 'task_pass';
CREATE DATABASE task_db OWNER task_user;
CREATE DATABASE task_test_db OWNER task_user;
GRANT ALL PRIVILEGES ON DATABASE task_db TO task_user;
GRANT ALL PRIVILEGES ON DATABASE task_test_db TO task_user;
EOF-
Generate new migration:
alembic revision --autogenerate -m "create tasks table" -
Apply migrations:
alembic upgrade head
This project provides two Dockerfiles:
Dockerfileβ optimized for production with monitoring and loggingDockerfile.devβ for development (hot reload, dev dependencies)
make docker-devmake docker-prodmake stopmake logs-
Clone the repository:
git clone https://github.com/kmilodenisglez/task-backend.git cd task-backend -
Create virtual environment:
python -m venv venv source venv/bin/activate # Linux/macOS # venv\Scripts\activate # Windows
-
Install dependencies:
pip install -e . pip install -e ".[dev]"
-
Start server (with reload):
make dev
Server available at:
- API β http://localhost:8000
- Docs β http://localhost:8000/docs
- Health β http://localhost:8000/health/health
-
Run tests:
make test -
Run with coverage:
make coverage
-
Open HTML coverage report:
open htmlcov/index.html # macOS xdg-open htmlcov/index.html # Linux
| Command | Description |
|---|---|
make dev |
Run FastAPI locally (reload) |
make run |
Run FastAPI locally (no reload) |
make docker-dev |
Run dev environment with Docker Compose |
make docker-prod |
Run prod environment with Docker Compose |
make stop |
Stop Docker services |
make logs |
Show Docker logs |
make test |
Run all tests |
make coverage |
Run tests with coverage |
make typecheck |
Type checking with mypy |
make format |
Format with black + isort |
make lint |
Lint with flake8 |
make migrate |
Create new Alembic migration |
make upgrade |
Apply Alembic migrations |
make downgrade |
Rollback last migration |
| Command | Description |
|---|---|
make health |
Basic health check |
make health-detailed |
Detailed health check with metrics |
make metrics |
View application metrics |
make logs-tail |
Follow application logs |
make logs-errors |
View error logs only |
make test-rate-limit |
Test rate limiting functionality |
task-backend/
βββ app/
β βββ main.py
β βββ config.py
β βββ database.py
β βββ api/
β β βββ v1/ # API version 1
β β β βββ auth.py
β β β βββ tasks.py
β β βββ v2/ # API version 2 (enhanced)
β β β βββ auth.py
β β β βββ tasks.py
β β βββ health.py # Health check endpoints
β βββ models/
β βββ schemas/
β βββ utils/
β β βββ auth.py
β β βββ logging.py # Structured logging
β β βββ rate_limiting.py
β β βββ validators.py
β βββ tests/
βββ alembic/
β βββ versions/
βββ logs/ # Application logs
βββ .env.example
βββ Dockerfile
βββ Dockerfile.dev
βββ docker-compose.yml
βββ Makefile
βββ Makefile.ci
βββ pyproject.toml
βββ README.md
-
Development/Production: async mode with PostgreSQL +
asyncpg -
Testing:
- β SQLite (fast, isolated)
- β PostgreSQL async (realistic, slower)
-
Controlled via
settings.testingflag
See TESTING.md for detailed testing documentation.
- Linting, type checking, tests, coverage
- Coverage reports uploaded as artifacts (
coverage-html,coverage-xml) - Workflow:
.github/workflows/ci.yml
| Variable | Description | Default |
|---|---|---|
DATABASE_URL |
PostgreSQL connection string | Required |
SECRET_KEY |
JWT secret key | Required |
LOG_LEVEL |
Logging level | INFO |
RATE_LIMIT_CALLS |
Rate limit requests per window | 100 |
RATE_LIMIT_PERIOD |
Rate limit window in seconds | 3600 |
ENABLE_METRICS |
Enable metrics endpoint | true |
The application uses structured JSON logging with:
- Request/response logging with correlation IDs
- Error tracking and monitoring
- Performance metrics
- User activity tracking
Configurable rate limiting with:
- Sliding window algorithm
- Per-IP limiting
- Configurable limits and windows
- Informative response headers
This project is licensed under the MIT License. See the LICENSE file.