A REST API for task management (To-Do List) with user authentication, built with FastAPI and async PostgreSQL.
- FastAPI — web framework
- SQLAlchemy 2.0 (async) + asyncpg — ORM and database access
- PostgreSQL 15 — database
- Alembic — migrations
- JWT (PyJWT) — authentication
- bcrypt / passlib — password hashing
- Pydantic v2 — data validation
- Loguru — logging
- Docker / Docker Compose — containerization
- pytest — testing
- User registration and login (JWT Bearer)
- Full CRUD for tasks (create, read, update, delete)
- Browse all tasks with pagination and sorting
- Retrieve only the current user's tasks (protected endpoint)
- Background logging of user actions
fastapi_project/
├── main.py # Routes and application entry point
├── models.py # SQLAlchemy models (Users, Tasks)
├── schemas.py # Pydantic schemas
├── services.py # Business logic
├── security.py # JWT and password hashing
├── dependencies.py # FastAPI dependencies (session, pagination, current user)
├── database.py # Database connection setup
├── config.py # Configuration via pydantic-settings
├── logging_config.py # Loguru setup
├── migrations/ # Alembic migrations
├── tests/ # Tests
├── Dockerfile
├── docker-compose.yml
├── Makefile
└── requirements.txt
- Create a
.envfile in the project root:
POSTGRES_USER=postgres
POSTGRES_PASSWORD=your_password
POSTGRES_DB=taskdb
POSTGRES_HOST=db
POSTGRES_PORT=5432
SECRET_KEY=your_secret_key
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30- Start the application:
make run
# or
docker compose up -dThe API will be available at: http://localhost:1245
- Stop:
make stop- Create and activate a virtual environment:
python -m venv .venv
source .venv/bin/activate # Linux/macOS- Install dependencies:
pip install -r requirements.txt-
Configure
.env(setPOSTGRES_HOST=localhost) -
Apply migrations:
alembic upgrade head- Run the server:
python main.pyThe API will be available at: http://localhost:8000
| Method | Path | Description |
|---|---|---|
POST |
/auth/register |
Register a new user |
POST |
/auth/login |
Login and receive a JWT token |
| Method | Path | Auth | Description |
|---|---|---|---|
GET |
/tasks |
— | Get all tasks (with pagination) |
GET |
/tasks/my |
JWT | Get current user's tasks |
POST |
/tasks |
JWT | Create a new task |
GET |
/tasks/{id} |
JWT | Get a task by ID |
PUT |
/tasks/{id} |
JWT | Update a task |
DELETE |
/tasks/{id} |
JWT | Delete a task |
List endpoints support the following query parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
page |
int | 1 | Page number |
limit |
int | 10 | Items per page |
order |
asc / desc |
asc |
Sort order by ID |
Example: GET /tasks?page=2&limit=5&order=desc
Swagger UI is available at /docs after starting the app.
# Create a new migration
make migration msg="description of changes"
# Apply migrations
alembic upgrade head
# Roll back the last migration
alembic downgrade -1make tests
# or
python -m pytest -v -s| Command | Description |
|---|---|
make run |
Start the database and app in Docker |
make stop |
Stop the containers |
make logs |
Tail application logs |
make pull |
Pull the latest image from Docker Hub |
make migration msg="..." |
Create an Alembic migration |
make tests |
Run tests |
The application uses Loguru. Logs are written to:
logs/app.log— general eventslogs/errors.log— errors
All user actions (task creation, deletion, updates, login) are logged in the background via BackgroundTasks.