Skip to content

Repository files navigation

Task Manager API

A REST API for task management (To-Do List) with user authentication, built with FastAPI and async PostgreSQL.

Tech Stack

  • 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

Features

  • 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

Project Structure

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

Quick Start

Via Docker Compose (recommended)

  1. Create a .env file 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
  1. Start the application:
make run
# or
docker compose up -d

The API will be available at: http://localhost:1245

  1. Stop:
make stop

Local Setup

  1. Create and activate a virtual environment:
python -m venv .venv
source .venv/bin/activate  # Linux/macOS
  1. Install dependencies:
pip install -r requirements.txt
  1. Configure .env (set POSTGRES_HOST=localhost)

  2. Apply migrations:

alembic upgrade head
  1. Run the server:
python main.py

The API will be available at: http://localhost:8000

API Endpoints

Authentication

Method Path Description
POST /auth/register Register a new user
POST /auth/login Login and receive a JWT token

Tasks

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

Pagination & Sorting

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

Interactive Docs

Swagger UI is available at /docs after starting the app.

Migrations

# Create a new migration
make migration msg="description of changes"

# Apply migrations
alembic upgrade head

# Roll back the last migration
alembic downgrade -1

Tests

make tests
# or
python -m pytest -v -s

Makefile Commands

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

Logging

The application uses Loguru. Logs are written to:

  • logs/app.log — general events
  • logs/errors.log — errors

All user actions (task creation, deletion, updates, login) are logged in the background via BackgroundTasks.

About

REST API for task management built with FastAPI, PostgreSQL, SQLAlchemy, JWT authentication, Docker, and CI/CD via GitHub Actions

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages